Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Column width in Apache POI

I am writing a tool in Java using Apache POI API to convert an XML to MS Excel. In my XML input, I receive the column width in points. But the Apache POI API has a slightly queer logic for setting column width based on font size etc. (refer API docs)

Is there a formula for converting points to the width as expected by Excel? Has anyone done this before?

There is a setRowHeightInPoints() method though :( but none for column.

P.S.: The input XML is in ExcelML format which I have to convert to MS Excel.

like image 460
Arun Avatar asked Jul 20 '12 06:07

Arun


People also ask

How do I change the width of a column in Excel using poi?

Also, you should create all your rows and fill them with content first, before you call autoSizeColumn(so the column gets the width of the value with the broadest width). (If you want to set the column width to a fixed value, use HSSFSheet. setColumnWidth(int,int) instead.)

How do I change the row height in Apache POI?

row. setHeightInPoints((2 * sheet. getDefaultRowHeightInPoints())); to set it (for example) to 2 characters high.


1 Answers

Unfortunately there is only the function setColumnWidth(int columnIndex, int width) from class Sheet; in which width is a number of characters in the standard font (first font in the workbook) if your fonts are changing you cannot use it. There is explained how to calculate the width in function of a font size. The formula is:

width = Truncate([{NumOfVisibleChar} * {MaxDigitWidth} + {5PixelPadding}] / {MaxDigitWidth}*256) / 256 

You can always use autoSizeColumn(int column, boolean useMergedCells) after inputting the data in your Sheet.

like image 118
ArtiBucco Avatar answered Sep 19 '22 17:09

ArtiBucco