Hi,
I am writing a function to export an SWT table to Excel. For most
tables this works fine, but on some of them the Excel sheet gets
corrupted.
First the table is converted to an Object[][], and an array is
generated with the column names. These arrays are passed to the
following function.
Most of the times I create 5 sheets with each 1000 rows of 20 columns
on them. Does anyone see an obvious error I am making? There is no
exception generated in java, but Excel says the file is too heavily
damaged to read it.
Regards,
Leen Toelen
public static void addToSheet(HSSFSheet sheet, Object[][] values,
String[] colnames) {
// Create the Title row
HSSFRow tr = sheet.getRow(0);
if (tr == null)
tr = sheet.createRow(0);
for (int j = 0; j < colnames.length; j++) {
HSSFCell c = tr.getCell((short) j);
if (c == null)
c = tr.createCell((short) j);
String output = "";
if (colnames[j] != null && colnames[j] != null)
output = colnames[j].toString();
c.setCellType(HSSFCell.CELL_TYPE_STRING);
c.setCellValue(output);
}
// Loop all rows and copy them to Excel
for (int i = 0; i < values.length; i++) {
HSSFRow r = sheet.getRow(i + 2);
if (r == null)
r = sheet.createRow(i + 2);
for (int j = 0; j < values[i].length; j++) {
Object value = values[i][j];
if (value == null || value.toString().equals("")) {
continue;
}
HSSFCell c = r.getCell((short) j);
if (c == null)
c = r.createCell((short) j);
double asdouble = -9999;
try {
asdouble = Double.parseDouble(value.toString());
} catch (Exception e) {
}
if (value instanceof Date) {
c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
c.setCellValue((Date) value);
} else if (asdouble != -9999) {
c.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
c.setCellValue(asdouble);
} else if (value.toString().startsWith("http://")) {
String url = value.toString();
if(url.length()>255)
url = url.substring(0,250)+"...";
c.setCellType(HSSFCell.CELL_TYPE_FORMULA);
c.setCellFormula("HYPERLINK(\"" + url + "\")");
} else {
c.setCellType(HSSFCell.CELL_TYPE_STRING);
c.setCellValue(value.toString());
}
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List: http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project: http://jakarta.apache.org/poi/