简体   繁体   中英

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. Then in pushData I fill the rows. The problem is once I do write and flush in init method nothing else comes in my excel sheet.

The rows that I would be writing to excel could be huge. 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

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. This will write data to a temporary file during execution rather than storing it in memory.

You need to adjust your WorkbookSettings:

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

Replace your_temporary_directory above with a temporary directory you prefer

Also note that this feature is available in jxl version >= 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. I think doing a workbook.write() kind of closes the stream for any further writing.

So basically do outputStream.flush() everytime you want to flush out of memory.

Do workbook.write() and workbook.close() in the last

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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