2016-11-29 88 views
0

這是我有:如何使用POI Excel以編程方式拉伸列以適應文本?

enter image description here

這就是我想要的:

enter image description here

我使用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!"); 

回答

3
sheet.autoSizeColumn(colNumber); 

中的所有數據填充後運行它。

+0

謝謝。這工作。我想大量的列你想循環通過它們。 (int colNumber = 0; colNumber <= 7; colNumber ++){sheet.autoSizeColumn(colNumber); }'' – santafebound

+0

是的。你必須循環逐列。大數據集可能會很慢。 – arturro

相關問題