我如何使用POI Excel以编程方式拉伸列以适应文本?

这是我的:

在这里输入图像说明

这就是我要的:

在这里输入图像说明

我正在使用POIExcel创build一个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!"); 

 sheet.autoSizeColumn(colNumber); 

填写完所有数据后运行。