Setting Column width in Apache POI

前端 未结 5 1223
闹比i
闹比i 2020-12-13 11:56

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

5条回答
  •  天涯浪人
    2020-12-13 12:50

    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.

提交回复
热议问题