简体   繁体   English

我如何使用jxl定期刷新Excel中的行,同时最后关闭

[英]How do i periodically flush the rows in excel using jxl while do the close at the last

Below is my code. 下面是我的代码。 I first create headers in init method. 我首先在init方法中创建标头。 Then in pushData I fill the rows. 然后在pushData中填充行。 The problem is once I do write and flush in init method nothing else comes in my excel sheet. 问题是,一旦我在init方法中编写并刷新了,Excel表格中就没有其他内容了。

The rows that I would be writing to excel could be huge. 我要写给excel的行可能很大。 The idea of using flush is not free the memory periodically. 使用刷新的想法不是定期释放内存。

Please tell me what mistake I am doing here. 请告诉我我在这里犯什么错误。 How do i achieve what I intent?l 我如何实现我的意图?l

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();
        }

    }
}

As suggested here 这里的建议

To get around the memory problem, you can signal jxl to use temporary files when writing. 要解决内存问题,可以向jxl发信号,使其在写入时使用临时文件。 This will write data to a temporary file during execution rather than storing it in memory. 这将在执行期间将数据写入临时文件,而不是将其存储在内存中。

You need to adjust your WorkbookSettings: 您需要调整您的WorkbookSettings:

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

Replace your_temporary_directory above with a temporary directory you prefer 将上面的your_temporary_directory替换为您喜欢的临时目录

Also note that this feature is available in jxl version >= 2.6.9 另请注意,此功能在jxl版本> = 2.6.9中可用

I think I have found the problem with my code. 我想我的代码发现了问题。 This is just out of hit and trial and I would certainly need someone to tell if I am correct. 这只是一时的尝试,我当然需要有人告诉我我是否正确。

In the init method, if I only do outputStream.flush() the I don't see a problem. 在init方法中,如果仅执行outputStream.flush(),则不会出现问题。 I think doing a workbook.write() kind of closes the stream for any further writing. 我认为做一个workbook.write()会关闭流以进行进一步的编写。

So basically do outputStream.flush() everytime you want to flush out of memory. 所以基本上,每次要从内存中清空时,都要做outputStream.flush()。

Do workbook.write() and workbook.close() in the last 最后做workbook.write()和workbook.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM