Search code examples
javaandroidexcelxlsx

FontRenderContext error when using autoSizeColumn method in Android


everyone, I created a method which generates an xlsx file and now I would like to auto-resize its columns. I tried this function:

//Auto Size columns
for (int i=0; i<columns.length; i++) sheet.autoSizeColumn(i);

The problem is that I get the error:

Failed resolution of: Ljava/awt/font/FontRenderContext;"

In this question I found out the exists a method which can I use, but I don't understand the mechanism. I know the exactly number of the character for each columns, but if I try to insert them manually as well nothing changes:

sheet.setColumnWidth(0, 8);

0: number of the column.
8: length of the word in the column


Solution

  • https://stackoverflow.com/a/59718764/8646362

    Kindly check the link above. Column Width needs to be multiplied by 256. The width parameter is in units of 1/256th of a character width. Also it will throw an exception for java.lang.IllegalArgumentException - if width > 255*256 (the maximum column width in Excel is 255 characters)