JAVA + POI API Excel-需要增加列的宽度

smr*_*iti 9 java apache-poi

我想增加Excel工作表列的宽度.因为我写的代码很长.我需要手动拖动列以查看全文.

我这样做了 -

HSSFRow dataRow = sampleDataSheet.createRow(0);

HSSFCellStyle cellStyle = setHeaderStyle(sampleWorkbook);

cellStyle.setWrapText(true);

***sampleDataSheet.autoSizeColumn(1000000);***
Run Code Online (Sandbox Code Playgroud)

但它没有改变任何东西..

Int*_*uff 18

这应该工作.然而,

sampleDataSheet.autoSizeColumn(1000000);
Run Code Online (Sandbox Code Playgroud)

自动扩展列1000000.

如果要自动展开第0列(第一列),请使用:

sampleDataSheet.autoSizeColumn(0);
Run Code Online (Sandbox Code Playgroud)

要将第0列自动展开到第9列(前10列):

for (int i=0; i<10; i++){
   sampleDataSheet.autoSizeColumn(i);
}
Run Code Online (Sandbox Code Playgroud)

此外,在调用autoSizeColumn之前,应首先创建所有行并用内容填充它们(因此列获取具有最宽宽度的值的宽度).

(如果要将列宽设置为固定值,请改用HSSFSheet.setColumnWidth(int,int).)


小智 12

// We can set column width for each cell in the sheet        
sheet.setColumnWidth(0, 1000);
sheet.setColumnWidth(1, 7500);
sheet.setColumnWidth(2, 7500);

// By applying style for cells we can see the total text in the cell for specified width
HSSFCellStyle cellStyle = workBook.createCellStyle();
cell.setCellStyle(cellStyle );
cellStyle.setWrapText(true);
Run Code Online (Sandbox Code Playgroud)