如何使用apache poi从excel表读取和写入非常大的数据?

时间:2017-07-04 11:32:02

标签: java excel apache out-of-memory large-data

我有一项任务是读取每个包含一张纸的2个excel文件,并生成一个包含2张纸的单个输出工作簿。我正在使用.xlsx文件和Apache POI,我当前能够将数据从一个输入文件写入输出文件,当我尝试将第二个输入文件写入输出文件的第二个页面时,它显示了堆外记忆错误。第二张包含162100行,包含4列,我尝试使用列表从第二个文件中读取所有数据。所有数据都成功存储在List中但是当我尝试从列表中写入第二张表时它正在显示..

const prpl = require('prpl-server');
const express = require('express');
const config = require('./build/polymer.json');

const app = express();

app.get('/*', prpl.makeHandler('./build/', config));

app.listen(process.env.PORT || 80);

这是代码:

"Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at org.apache.xmlbeans.impl.store.Xobj.ensureParent(Xobj.java:614)
at org.apache.xmlbeans.impl.store.Xobj.getNormal(Xobj.java:661)
at org.apache.xmlbeans.impl.store.Cur.getNormal(Cur.java:2464)
at org.apache.xmlbeans.impl.store.Cur.skip(Cur.java:1269)
at org.apache.xmlbeans.impl.store.Cur.moveNode(Cur.java:1840)
at org.apache.xmlbeans.impl.store.Cur.createHelper(Cur.java:287)
at org.apache.xmlbeans.impl.store.Cur.createElement(Cur.java:231)
at org.apache.xmlbeans.impl.store.Cur.createElement(Cur.java:226)
at org.apache.xmlbeans.impl.store.Xobj.insertElement(Xobj.java:2116)
at org.apache.xmlbeans.impl.store.Xobj.add_element_user(Xobj.java:2197)
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTCellImpl.setV(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFCell.setCellValue(XSSFCell.java:333)
at cgi.AMSdump.<init>(AMSdump.java:224)
at cgi.AMSdump.main(AMSdump.java:451)"

//写入文件

public class AMSdump {
//Array list to store  

java.util.List<DataStorer> data = new ArrayList<DataStorer>();

//to open first file

 FileInputStream myStream = new FileInputStream(output1);
 OPCPackage pkg = OPCPackage.open(myStream);
 XSSFWorkbook wbread = new XSSFWorkbook(pkg);


 CreationHelper createHelper = wb.getCreationHelper();
 XSSFSheet sheetx  = wbread.getSheetAt(0);
 XSSFRow row;
 XSSFCell cell;

//getting the first and last row of the input Sheet 1

 int rowStart = sheetx.getFirstRowNum() ;
 int rowEnd = sheetx.getLastRowNum() ;int count = 7;
 int fCell,lCell;
 Row rowwrite[] =new Row[rowEnd+1];


 for(int i=rowStart;i<=rowEnd;i++){
     row=sheetx.getRow(i);
     if(row==null){
     System.out.println("empty accessed");
     continue;}
     if(row!=null){
     rowwrite[i]=sheet1.createRow((short)i);



     fCell = row.getFirstCellNum(); 
     lCell = row.getLastCellNum();  

//iterating over the cells of a particular row and writing it one by one in the workbook

     for(int iCell = fCell; iCell < lCell; iCell++) {
         cell = row.getCell(iCell);


         if(cell==null){
             continue;}


         else{


                Cell currentCell = cell;


 //determining the type of cell being read and writing that to the new workbook

                 if (currentCell.getCellTypeEnum() == CellType.NUMERIC) { 
                       rowwrite[i].createCell(iCell).setCellValue(currentCell.getNumericCellValue());}
                 else if(currentCell.getCellTypeEnum() == CellType.STRING) {
                         rowwrite[i].createCell(iCell).setCellValue(currentCell.getStringCellValue());}
                 else if(currentCell.getCellTypeEnum() == CellType.FORMULA){
                         rowwrite[i].createCell(iCell).setCellValue(currentCell.getCellFormula());}
                 else if (currentCell.getCellTypeEnum() == CellType.ERROR){
                         rowwrite[i].createCell(iCell).setCellValue(currentCell.getErrorCellValue());}
            }//else part of cell ends

         }//inner cell for loop ends
        }
    }//row not null ends
     System.out.println("WorkBook has been created");

}//outer for loop ends

//由于堆错误导致写入失败!

    FileOutputStream fileOut = new FileOutputStream("AmsDumpOutput"+open+".xlsx");
    wb.write(fileOut);; 

    fileOut.close();
    wbread.close();
    System.out.println("Sheet1 of WorkBook has been created");

//calling the function to read from second file and store them into list

storeIntoList(output2);

//to check if the lists contains values 
for(int i = 0;i<data.size();i++){
    System.out.println("Email : "+ data.get(i).getEmail() + "Pan : "+data.get(i).getPan());
}

//writing to second sheet of the output file
int size = data.size();
Row rowwrite2[] =new Row[size+1];
rowwrite2[0] = sheet2.createRow(0);
rowwrite2[0].createCell(0).setCellValue("Mobile No.");
sheet2.setColumnWidth(0, 1300*3);
rowwrite2[0].createCell(1).setCellValue("Email ID");
rowwrite2[0].createCell(2).setCellValue("Candidate ID");
rowwrite2[0].createCell(3).setCellValue("PAN Number");
for(int counter = 1;counter<size;counter++){
    rowwrite2[counter] = sheet2.createRow(counter);
    rowwrite2[counter].createCell(0).setCellValue(data.get(counter).getMobile());
    //rowwrite2[counter].createCell(1).setCellValue(data.get(counter).getEmail());
    rowwrite2[counter].createCell(2).setCellValue(data.get(counter).getID());
    //rowwrite2[counter].createCell(3).setCellValue(data.get(counter).getPan());
}

}

//这是存储数据的自定义类//

FileOutputStream fileOuts = new FileOutputStream("AmsDumpOutput"+open+".xlsx");
**wb.write(fileOuts);;wb.close();
fileOuts.close();**


//closing the output workbook

System.out.println("Sheet 2 of Output WorkBook has been created");
wb = null;
myStream = null;
//field2(output2);
System.out.println("Finale done");

0 个答案:

没有答案