簡體   English   中英

Apache POI autoColumnWidth java.lang.OutOfMemoryError:超出GC開銷限制

[英]Apache POI autoColumnWidth java.lang.OutOfMemoryError: GC overhead limit exceeded

當在我的 excel 中包含 1,28,237 行和 20 列的所有列上調用autoColumnWidth函數時,我收到OutOfMemoryError 我正在使用SXSSF api 並將-Xms2048m -Xmx4096m -XX:PermSize=1024m -XX:MaxPermSize=2048m給 JVM,但仍然沒有成功。 CPU 利用率高達 100%,我的系統 RAM 為 6gb,在此過程中最多使用 5.8gb,然后我得到OutOfMemoryError 有什么解決辦法嗎?

有了這個,我可以想到一個解決方案,即為每列取一個變量,找到最大長度,最后在輸入所有數據后將列設置為相應的變量。

還有其他解決辦法嗎?

我的代碼來創建 Excel

public static void createVideodataExcelFile(String excelPath, int maxRiskArea){

    FileOutputStream fileOut = null;
    XSSFWorkbook workbook = new XSSFWorkbook();

    CellStyle style = workbook.createCellStyle();
    XSSFFont font = workbook.createFont();
    font.setColor(HSSFColor.BLACK.index);
    font.setBold(true);
    style.setFont(font);
    int cellCount = 0;

    try {
        File file = new File(excelPath);

        if(file.createNewFile()){
            SXSSFWorkbook wb = new SXSSFWorkbook(workbook); 
            wb.setCompressTempFiles(true);
            Sheet sheet = wb.createSheet("GLE Video Data");
            int rowCount = sheet.getPhysicalNumberOfRows();
            //System.out.println("Row Count ="+rowCount);
            Row row = sheet.createRow(rowCount);

            Cell cell1 = row.createCell(cellCount);
            createCell(cell1, row, "CourseContent name", style, cellCount);
            createCell(cell1, row, "lang", style, ++cellCount);
            createCell(cell1, row, "CourseTitle", style, ++cellCount);
            createCell(cell1, row, "Audience", style, ++cellCount);
            createCell(cell1, row, "ContentRegion", style, ++cellCount);
            createCell(cell1, row, "CourseTitle", style, ++cellCount);
            createCell(cell1, row, "DeprecatedId", style, ++cellCount);
            createCell(cell1, row, "GleCode", style, ++cellCount);
            createCell(cell1, row, "Guid", style, ++cellCount);
            createCell(cell1, row, "LearningFormat", style, ++cellCount);

            for(int i = 0 ; i < maxRiskArea ; ++i){
                createCell(cell1, row, "RiskArea", style, ++cellCount);
            }

            createCell(cell1, row, "SalesforceId", style, ++cellCount);
            createCell(cell1, row, "Setting", style, ++cellCount);
            createCell(cell1, row, "SmsCode", style, ++cellCount);
            createCell(cell1, row, "pageID", style, ++cellCount);
            createCell(cell1, row, "title", style, ++cellCount);
            createCell(cell1, row, "ID", style, ++cellCount);
            createCell(cell1, row, "media_src", style, ++cellCount);
            createCell(cell1, row, "cuePoint", style, ++cellCount);
            createCell(cell1, row, "character", style, ++cellCount);
            createCell(cell1, row, "line", style, ++cellCount);

            fileOut = new FileOutputStream(excelPath);
            wb.write(fileOut);
            System.out.println("Excel Of GLE Data Created.");
        }


    } catch (IOException e) {
        errorLog.error("Excel Read/Write Error ="+e.getMessage());
        throw new GLEException(e);
    }
    finally{
        try {
            if(fileOut!=null)
            fileOut.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            errorLog.error("Excel Read/Write Error ="+e.getMessage());
            throw new GLEException(e);      

        }
    }
}

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

嘗試在調用 autoSizeColumn(columnIndex) 之前在 SXSSFSheet 對象上調用 trackAllColumnsForAutoSizing() 它將被修復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM