如何使用POI加载大型xls文件时解决java堆空间错误

时间:2018-02-13 17:05:24

标签: java excel apache-poi large-files

我正在尝试加载和解析一个11mb的xls文件,我正在使用WorkbookFactory.create(myFile)来加载它。但是我无法加载这个文件并解析它。任何人都可以帮我解决这是我在我的程序中使用的代码,

public class LargeExcelManager {

    private Workbook wb ;

    public Workbook getWb() {
        return wb;
    }

    public void setWb(Workbook wb) {
        this.wb = wb;
    }

    public LargeExcelManager(String fileName)throws Exception {

        openWorkbook(fileName);
    }

    private void openWorkbook(String fileName)throws Exception {
        try {
            System.out.println("Can be added now");
            File myFile = new File(fileName);
            this.setWb(WorkbookFactory.create(myFile)); // Here i am trying to set the large file 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我试图从中获取一些信息,

@Override
public List<String> start() {
    ActivityLauncher.logConsoleMsg("Activity number " + activityId + " started");
    List<String> result = new ArrayList<String>();
    Workbook wb  = excelManager.getWb();

    for (int i=0; i<wb.getNumberOfSheets(); i++) {
        if(wb.getSheetName(i).contains("Loan information")) {
            //sheetNames.add( wb.getSheetName(i) );
            result.addAll(processSheet(wb.getSheetAt(i)));
        }
    }
    ActivityLauncher.logConsoleMsg("Activity number " + activityId + " completed");
    return result;
}

但是每次我尝试加载它都会让我失望。任何人都能以某种方式帮助我吗?

我使用POI 3.16

收到此错误
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at 
org.apache.xmlbeans.impl.store.Cur$CurLoadContext.attr(Cur.java:3044) 

0 个答案:

没有答案