我如何定期使用jxl在Excel中刷新行,同时在最后closures

以下是我的代码。 我首先在init方法中创build标题。 然后在pushData中填充行。 问题是我一旦写入和刷新init方法,没有其他的东西在我的Excel表中。

我会写的excel的行可能是巨大的。 使用刷新的想法不是周期性地释放内存。

请告诉我我在这里做了什么错误。 我如何达到我的意图?

import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; public class ExportTranscriptDetailsToExcel implements IExportToFormat { private static final ILogger logger = ILabsPlatform.getLogger(ExportTranscriptDetailsToExcel.class.getName()); OutputStream outputStream; List<String> labels; WritableWorkbook workbook; WritableSheet sheet0; public ExportTranscriptDetailsToExcel() { this.outputStream = null; workbook = null; sheet0 = null; } @Override public void init(String sheetName, List<String> labels, OutputStream outputStream) throws IOException, RowsExceededException, WriteException { this.outputStream = outputStream; this.labels = labels; WorkbookSettings workbookSettings = new WorkbookSettings(); workbookSettings.setEncoding("Cp1252"); workbook = Workbook.createWorkbook(outputStream, workbookSettings); sheet0 = workbook.createSheet(sheetName, 0); for (int i = 0; i < labels.size(); ++i) { Label label = new Label(i, 0, labels.get(i)); sheet0.addCell(label); } workbook.write(); outputStream.flush(); } @Override public void pushDataRows(Object listOfResultRow) { if ((listOfResultRow == null)) { return; } String fieldName = null; String fieldValue = null; @SuppressWarnings("unchecked") ArrayList<Map<String, Object>> interactionMap = (ArrayList<Map<String, Object>>) listOfResultRow; try { int i = 1;// the data rows starts from row1 for (Map<String, Object> element : interactionMap) { for (int j = 0; j < labels.size(); j++) { fieldName = labels.get(j); Object ob = element.get(fieldName); if (ob != null) { fieldValue = ob.toString(); } if (fieldValue == null) { fieldValue = "-"; } System.out.println("***********************fieldName:" + fieldName); System.out.println("***********************fieldValue:" + fieldValue); Label label1 = new Label(j, i, fieldValue); fieldValue = null; sheet0.addCell(label1); } i++; } } catch (Exception e) { System.out.println(e.getMessage()); } try { workbook.write(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { outputStream.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void done() { try { workbook.close(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

如这里所build议的

为了避免内存问题,您可以在写入时指示jxl使用临时文件。 这将在执行期间将数据写入临时文件,而不是将其存储在内存中。

您需要调整您的WorkbookSettings:

 workbookSettings.setUseTemporaryFileDuringWrite(true); workbookSettings.setTemporaryFileDuringWriteDirectory(new File("your_temporary_directory")); 

用你喜欢的临时目录replace上面的your_temporary_directory

另请注意,此function在jxl版本> = 2.6.9中可用

我想我已经find了我的代码的问题。 这只是没有命中和审判,我肯定需要有人告诉我是否正确。

在init方法中,如果我只做outputStream.flush(),我没有看到问题。 我认为做一个workbook.write()种closuresstream进一步写作。

所以基本上做outputStream.flush()每次你想刷新内存。

在最后执行workbook.write()和workbook.close()