当前位置: 代码迷 >> java >> 使用poi sxssfworkbook附加到工作簿
  详细解决方案

使用poi sxssfworkbook附加到工作簿

热度:18   发布时间:2023-07-26 14:15:38.0

我需要将行追加到工作簿的一张纸上。 我正在使用org.apache.poi.xssf.streaming.SXSSFWorkbook,但是我无法实现低内存占用。 以下是代码:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelHelper {
    public static void createExcelFileWithLowMemFootprint(
            ArrayList<HashMap<String, Object>> data,
            ArrayList<String> fieldNames, String fileName, int rowNum) {
        try {
            if (rowNum == 0) {
                // Creating a new workbook and writing the top heading here
                SXSSFWorkbook workbook = new SXSSFWorkbook(1000);
                Sheet worksheet = workbook.createSheet("Sheet 1");
                int i = 0;
                Iterator<String> it0 = fieldNames.iterator();
                Row row = worksheet.createRow(i);
                int j = 0;
                while (it0.hasNext()) {
                    Cell cell = row.createCell(j);
                    String fieldName = it0.next();
                    cell.setCellValue(fieldName);
                    j++;
                }
                rowNum++;
                FileOutputStream fileOut = new FileOutputStream(fileName);
                workbook.write(fileOut);
                fileOut.flush();
                fileOut.close();
            }
            InputStream fileIn = new BufferedInputStream(new FileInputStream(
                    fileName), 1000);
            SXSSFWorkbook workbook = new SXSSFWorkbook(
                    new XSSFWorkbook(fileIn), 1000);
            Sheet worksheet = workbook.getSheetAt(0);
            Iterator<HashMap<String, Object>> it = data.iterator();
            int i = rowNum;
            while (it.hasNext()) {
                Row row = worksheet.createRow(i);
                int j = 0;
                HashMap<String, Object> rowContent = it.next();
                Iterator<String> it1 = fieldNames.iterator();
                while (it1.hasNext()) {
                    Cell cell = row.createCell(j);
                    String key = it1.next();
                    Object o = rowContent.get(key);
                    if (o instanceof String) {
                        cell.setCellValue((String) o);
                    } else if (o instanceof Double) {
                        cell.setCellType(cell.CELL_TYPE_NUMERIC);
                        cell.setCellValue((Double) o);
                    }
                    j++;
                }
                i++;
            }
            fileIn.close();
            FileOutputStream fileOut = new FileOutputStream(fileName);
            workbook.write(fileOut);
            fileOut.flush();
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我通过批量传递内容(以便保存在jvm内存中)并增加变量rowNum来追加文件。

据我了解,当我用

SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook(fileIn),1000);

XSSWorkbook的构造函数将整个文件重新加载到内存中,导致超出gc限制。

我浏览了但找不到适合我的用例的解决方案。

你们能否建议解决此问题的方法,以减少将行添加到工作簿的内存占用量?

无需输出SXSSFWorkbook ,然后再加载回即可进行良好的内存管理。 只需一次写入所有数据。 如果尝试加载整个工作簿,它将存储在内存中,而当立即写入时,它将使用存储空间。 在某些计算机上,构造函数中还要放入1000 如果需要,请尝试在构造函数中放入100或其他较低的数字,而不是1000