santafebound santafebound - 1 year ago 115
Java Question

How can I stretch columns to fit text programatically with POI Excel?

This is what I have:

enter image description here

This is what I want:

enter image description here

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

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!

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(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);
System.out.println("Your excel file has been generated!");

Answer Source

run it after filling in all the data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download