使用POI向xlsx写入大量记录

时间:2014-03-11 10:14:09

标签: java apache-poi

我想写出数以千计的记录来excel。 我的代码=

public static void updateMetadataExcel(List<Metadata> metaDataList, String excelPath, int maxRiskArea) {


    FileOutputStream fileOut = null;
    FileInputStream file = null;
    Workbook workbook = null;
    int cellCount = 0 ;
        try {

            for(Metadata metaDataIterator : metaDataList){
                cellCount = 0;
                file = new FileInputStream(new File(excelPath));
                workbook = WorkbookFactory.create(file);

                int rowCount = 0;
                Sheet sheet = workbook.getSheetAt(0);
                rowCount = sheet.getPhysicalNumberOfRows();
            //  System.out.println("last =" + rowCount);

                rowCount = sheet.getPhysicalNumberOfRows();
                Row row = sheet.createRow(rowCount);
                Cell cell = row.createCell(cellCount);
                createCell(cell, row, metaDataIterator.getCourseContentName(), cellCount);
                createCell(cell, row, metaDataIterator.getLang(), ++cellCount);
                createCell(cell, row, metaDataIterator.getPalette(), ++cellCount);
                createCell(cell, row, metaDataIterator.getWordToGLE(), ++cellCount);
                createCell(cell, row, metaDataIterator.getCourseTitleAttr(), ++cellCount);
                createCell(cell, row, metaDataIterator.getAudience(), ++cellCount);
                createCell(cell, row, metaDataIterator.getClientCode(), ++cellCount);
                createCell(cell, row, metaDataIterator.getContentRegion(), ++cellCount);
                createCell(cell, row, metaDataIterator.getCourseStatus(), ++cellCount);
                createCell(cell, row, metaDataIterator.getCourseTitleTag(), ++cellCount);
                createCell(cell, row, metaDataIterator.getDateCreated(), ++cellCount);
                createCell(cell, row, metaDataIterator.getDateReleased(), ++cellCount);
                createCell(cell, row, metaDataIterator.getDateRetired(), ++cellCount);
                createCell(cell, row, metaDataIterator.getDeprecatedId(), ++cellCount);
                createCell(cell, row, metaDataIterator.getDeprecatedProductCode(), ++cellCount);
                createCell(cell, row, metaDataIterator.getDivision(), ++cellCount);
                createCell(cell, row, metaDataIterator.getDuration(), ++cellCount);
                createCell(cell, row, metaDataIterator.getEditorialReview(), ++cellCount);
                createCell(cell, row, metaDataIterator.getFormatChangeDate(), ++cellCount);
                createCell(cell, row, metaDataIterator.getGleCode(), ++cellCount);
                createCell(cell, row, metaDataIterator.getGuid(), ++cellCount);
                createCell(cell, row, metaDataIterator.getIndustry(), ++cellCount);
                createCell(cell, row, metaDataIterator.getInternalCourseNotes(), ++cellCount);
                createCell(cell, row, metaDataIterator.getKeyLearningPoints(), ++cellCount);
                createCell(cell, row, metaDataIterator.getKeywords(), ++cellCount);
                createCell(cell, row, metaDataIterator.getLanguage(), ++cellCount);
                createCell(cell, row, metaDataIterator.getLastModified(), ++cellCount);
                createCell(cell, row, metaDataIterator.getLawsCovered(), ++cellCount);
                createCell(cell, row, metaDataIterator.getLearningFormat(), ++cellCount);
                createCell(cell, row, metaDataIterator.getLearningObjectives(), ++cellCount);
                createCell(cell, row, metaDataIterator.getLegalReview(), ++cellCount);
                createCell(cell, row, metaDataIterator.getOriginalLanguage(), ++cellCount);
                createCell(cell, row, metaDataIterator.getProductDescription(), ++cellCount);
                createCell(cell, row, metaDataIterator.getProductType(), ++cellCount);
                createCell(cell, row, metaDataIterator.getProjectType(), ++cellCount);
                createCell(cell, row, "", ++cellCount);

                for(int i = 0 ; i < maxRiskArea ;  ++i){
                    if(metaDataIterator.getRiskAreas().size() >= maxRiskArea)
                        createCell(cell, row, metaDataIterator.getRiskAreas().get(i), ++cellCount);
                    else
                        createCell(cell, row, "", ++cellCount);
                }

                createCell(cell, row, "", ++cellCount);
                createCell(cell, row, metaDataIterator.getRole(), ++cellCount);
                createCell(cell, row, metaDataIterator.getSalesforceId(), ++cellCount);
                createCell(cell, row, metaDataIterator.getScriptFilename(), ++cellCount);
                createCell(cell, row, metaDataIterator.getScriptPath(), ++cellCount);
                createCell(cell, row, metaDataIterator.getSetting(), ++cellCount);
                createCell(cell, row, metaDataIterator.getSmsCode(), ++cellCount);
                createCell(cell, row, metaDataIterator.getSponsoringRegion(), ++cellCount);
                createCell(cell, row, metaDataIterator.getTargetAudience(), ++cellCount);
                createCell(cell, row, metaDataIterator.getTopic(), ++cellCount);
                createCell(cell, row, metaDataIterator.getTranslationFormat(), ++cellCount);
                createCell(cell, row, metaDataIterator.getVersion(), ++cellCount);
                createCell(cell, row, metaDataIterator.getVisualFormat(), ++cellCount);

                for(int colNum = 0; colNum<row.getLastCellNum();colNum++)   
                    workbook.getSheetAt(0).autoSizeColumn(colNum);
                fileOut = new FileOutputStream(excelPath);
                workbook.write(fileOut);
            }

            System.out.println("\nUpdated Excel For Metadata.");
        }
        catch (Exception e) {
            errorLog.error("Excel Read/Write Error =" + e.getMessage());
            throw new GLEException(e);
    }
}

private static void createCell(Cell cell , Row row, String name, int cellCount ){
    cell = row.createCell(cellCount);
    cell.setCellValue(name);
}

但它占用了大量的TIME,内存和内存!

每个有52列的2k记录花了大约一个小时

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

从apache POI站点的文档中可以看出,从版本3.8.beta3开始,有一个名为SXSSF的新API。它占地面积小得多,应该用于生成非常大的ex​​cel文件。 请检查以下链接: http://poi.apache.org/spreadsheet/

此外,您的问题可能与Socket Exception while genrating bulk excel file using Apache Poi

重复