简体   繁体   English

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

[英]JAVA+POI API Excel- Need to increase width of the column

I want to increase the width of the column of excel sheet. 我想增加Excel工作表列的宽度。 as the i am writing trough code is long. 因为我写的代码很长。 and I need to drag the column manually to see the full text. 我需要手动拖动列以查看全文。

I did this – 我这样做了 -

HSSFRow dataRow = sampleDataSheet.createRow(0);

HSSFCellStyle cellStyle = setHeaderStyle(sampleWorkbook);

cellStyle.setWrapText(true);

***sampleDataSheet.autoSizeColumn(1000000);***

But its not changing anything.. 但它没有改变任何东西..

This should work. 这应该工作。 However, 然而,

sampleDataSheet.autoSizeColumn(1000000);

auto-expands column 1000000. 自动扩展列1000000。

If you want to auto-expand column 0(the first column), use: 如果要自动展开第0列(第一列),请使用:

sampleDataSheet.autoSizeColumn(0);

To auto-expand column 0 to 9(the first 10 columns): 要将第0列自动展开到第9列(前10列):

for (int i=0; i<10; i++){
   sampleDataSheet.autoSizeColumn(i);
}

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

(If you want to set the column width to a fixed value, use HSSFSheet.setColumnWidth(int,int) instead.) (如果要将列宽设置为固定值,请改用HSSFSheet.setColumnWidth(int,int)。)

// 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);

sheet.autoSizeColumn(columnNumber) works. sheet.autoSizeColumn(columnNumber)有效。 this will resize the column to the largest cell length. 这会将列调整为最大单元格长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM