Set wb, sheet, cell style, font to null when you're done.
Firouz Bharthania wrote:
Hi,
It seems that the memory does not get released after the excel document is created and written into the file!!!
I highly appreciate is anybody can help me with this issue.
Here it is the code I used to create the document.
public class ExcelWriter {
protected String file = null;
protected FileOutputStream fileOut = null;
protected HSSFWorkbook wb = new HSSFWorkbook();
protected HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle dateCellStyle = wb.createCellStyle();
HSSFCellStyle timeCellStyle = wb.createCellStyle();
HSSFCellStyle decimalCellStyle = wb.createCellStyle();
HSSFCellStyle currencyCellStyle = wb.createCellStyle();
HSSFCellStyle stringCellStyle = wb.createCellStyle();
HSSFFont cellFont = wb.createFont();
public void close() throws IOException {
try {
wb.write(fileOut);
fileOut.close();
}catch(Exception e) {
e.printStackTrace();
}
}
public ExcelWriter(String file) throws IOException{
this.file = file;
fileOut = new FileOutputStream(file);
currencyCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00")
);
decimalCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0"));
dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
timeCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm
AM/PM"));
cellFont.setFontHeightInPoints((short)10);
cellFont.setFontName("Courier New");
currencyCellStyle.setFont(cellFont);
decimalCellStyle.setFont(cellFont);
stringCellStyle.setFont(cellFont);
dateCellStyle.setFont(cellFont);
timeCellStyle.setFont(cellFont);
}
protected HSSFCell createNewCell(int row, int column) {
HSSFRow sheetRow = null;
if((sheetRow = sheet.getRow(row)) == null) {
sheetRow = sheet.createRow((short)row);
}
HSSFCell cell = sheetRow.createCell((short)column);
return cell;
}
public void writeString(String string, int row, int column) throws
IOException{
HSSFCell cell = createNewCell(row, column);
cell.setCellValue(string);
cell.setCellStyle(stringCellStyle);
}
public void writeTimestamp(java.util.Date date, int row, int
column) throws IOException{
HSSFCell cell = createNewCell(row, column);
cell.setCellValue(date);
cell.setCellStyle(dateCellStyle);
}
public void writeNumber(BigDecimal value, int row, int column)
throws IOException{
HSSFCell cell = createNewCell(row, column);
cell.setCellValue(value.doubleValue());
cell.setCellStyle(currencyCellStyle);
}
}
--------------------------------------------------------------------- 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/
