I am creating a program that writes information to an excel file using apache poi. After entering all my data into a file, I call the autoSizeColumn method for each column of the file. But it resizes the columns to the width of the last entered cell, which was once not as large as the other cells in the column. I know that I use it correctly, and, unfortunately, I do not have the Internet right now to publish any code, but I will update it when I can.
Ok, I hope that I use code tags correctly, but here it is:
public void writeFile() {
Workbook wb = new HSSFWorkbook();
Sheet s = wb.createSheet();
try {
FileOutputStream out = new FileOutputStream(
this.getSelectedFile());
Row r = null;
Cell c = null;
wb.setSheetName(0, "Street Light Report");
String[] colName = { "Light Id", "Flagged",
"Malfunction", "Comments", "Location", "Date" };
Connections.connect();
String[][] data = Connections.searchForFlagged();
for (int i = 0; i < data.length; i++) {
r = s.createRow(i);
}
r.setRowNum(0);
for (int j = 0; j < 6; j++) {
c = r.createCell(j);
switch (j) {
case 0:
c.setCellValue(colName[j]);
break;
case 1:
c.setCellValue(colName[j]);
break;
case 2:
c.setCellValue(colName[j]);
break;
case 3:
c.setCellValue(colName[j]);
break;
case 4:
c.setCellValue(colName[j]);
break;
case 5:
c.setCellValue(colName[j]);
break;
}
}
for (int i = 0; i < data.length; i++) {
r.setRowNum(i + 1);
for (int j = 0; j < data[i].length; j++) {
c = r.createCell(j);
switch (j) {
case 0:
c.setCellType(Cell.CELL_TYPE_NUMERIC);
c.setCellValue(Integer.parseInt(data[i][j]));
break;
case 1:
if (data[i][j].equals("true"))
c.setCellValue("Yes");
else
c.setCellValue("No");
break;
case 2:
if (data[i][j].equals("I"))
c.setCellValue("Intermittent");
else
c.setCellValue("Not Functional");
break;
case 3:
c.setCellValue(data[i][j]);
break;
case 4:
c.setCellValue(data[i][j]);
break;
case 5:
c.setCellValue(data[i][j]);
break;
}
}
}
wb.getSheetAt(0).setPrintGridlines(true);
for (int j = 0; j < 6; j++) {
s.autoSizeColumn(j);
}
wb.write(out);
out.close();
} catch (Exception ex) {
}
};
I would post the image here, but I need a rep. and I just started here.
** Note that the Connections class is a JDBC class, so java can retrieve information from the mySQL database, it returns a 2D array of strings **