如何通过POI Excel以编程方式拉伸列以适合文本?

时间:2021-03-15 20:23:38

This is what I have:

这就是我所拥有的:

如何通过POI Excel以编程方式拉伸列以适合文本?

This is what I want:

这就是我要的:

如何通过POI Excel以编程方式拉伸列以适合文本?

I'm using POIExcel to create an Excel document and set the appropriate headers programatically, as you can see below. I then add additional rows below in my printSubnetInfo() method. What I want to do is make sure the columns are stretched to fit the text when I open the document. Ignore the bolding!

我正在使用POIExcel创建Excel文档并以编程方式设置相应的标题,如下所示。然后我在printSubnetInfo()方法中添加下面的其他行。我想要做的是确保在打开文档时拉伸列以适合文本。忽略粗体!

String outputs = "\\outputs\\";
String filename = homepath + outputs + customer + ".xls";
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Subnet Information"); 

// Create Excel document header row
HSSFRow rowhead = sheet.createRow((short)0);
rowhead.createCell(0).setCellValue("Default Gateway");
rowhead.createCell(1).setCellValue("CIDR Signature");
rowhead.createCell(2).setCellValue("Netmask");
rowhead.createCell(3).setCellValue("Network Address");
rowhead.createCell(4).setCellValue("Broadcast Address");
rowhead.createCell(5).setCellValue("Lowest Address");
rowhead.createCell(6).setCellValue("Highest Address");
rowhead.createCell(7).setCellValue("# of Subnet Addresses");

// Add rows to Excel document
for (int rowNum = 1; rowNum < (defaultGWsList).size(); rowNum++) {
    String defaultGWAddr = defaultGWsList.get(i);
    printSubnetInfo(subnetInfo, defaultGWAddr, homepath, customer, sheet, rowNum);
}

// Create Excel file
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");

1 个解决方案

#1


3  

sheet.autoSizeColumn(colNumber);

run it after filling in all the data.

在填写所有数据后运行它。

#1


3  

sheet.autoSizeColumn(colNumber);

run it after filling in all the data.

在填写所有数据后运行它。