반응형

Apache POI 사용중에 아래 예외가 발생했습니다.


Exception - The maximum column width for an individual cell is 255 characters.


문제가 발생한 코드입니다.

sheet.setColumnWidth(i, sheet.getColumnWidth(i) + 1500);


setColumnWidth 의 주석을 살펴 보았습니다.


Open Declaration void org.apache.poi.ss.usermodel.Sheet.setColumnWidth(int columnIndex, int width)
 
 
Set the width (in units of 1/256th of a character width) 
 
The maximum column width for an individual cell is 255 characters. This value represents the number of characters that can be displayed in a cell that is formatted with the standard font (first font in the workbook). 
 
Character width is defined as the maximum digit width of the numbers 012, ... 9 as rendered using the default font (first font in the workbook). 
Unless you are using a very special font, the default character is '0' (zero), this is true for Arial (default font font in HSSF) and Calibri (default font in XSSF) 
 
Please note, that the width set by this method includes 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines (Section 3.3.1.12 of the OOXML spec). This results is a slightly less value of visible characters than passed to this method (approx. 1/2 of a character). 
 
To compute the actual number of visible characters, Excel uses the following formula (Section 3.3.1.12 of the OOXML spec): 
 
width = Truncate([{Number of Visible Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256 
Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi). If you set a column width to be eight characters wide, e.g. setColumnWidth(columnIndex, 8*256), then the actual value of visible characters (the value shown in Excel) is derived from the following equation: Truncate([numChars*7+5]/7*256)/256 = 8; which gives 7.29.
 
Parameters:
columnIndex - the column to set (0-based)
width - the width in units of 1/256th of a character width
Throws:
IllegalArgumentException - if width > 255*256 (the maximum column width in Excel is 255 characters)


수정된 코드 입니다.

sheet.setColumnWidth(i, Math.min(255 * 256, sheet.getColumnWidth(i) + 1500));


반응형

+ Recent posts