写入Excel文件大约需要30分钟才能写入5万条记录

时间:2018-09-06 17:10:35

标签: java excel apache-poi

只写5万条记录大约需要30分钟。谁能建议可能是问题所在?

这是代码:

public static File writeInvalidInputExcelFile(final List<RawInputData> invalidInputRecords,
                                              @NonNull final List<String> headers,
                                              @NonNull final String invalidInputFilePath) throws IOException, IllegalAccessException {
    OutputStream outStream = null;
    final File invalidRecordFile = new File(invalidInputFilePath);
    try {
        final SXSSFSheet invalidRecordsSheet = ExcelUtility.createWorkBookWithHeader("invalid records",
                0, headers);
        int rowCount = 1;
        for (final RawInputData invalidinputRecord : invalidInputRecords) {

            ExcelUtility.writeEntriesForEachCellInTheGivenRow(invalidinputRecord,
                    rowCount++, invalidRecordsSheet);
        }
        outStream = new FileOutputStream(invalidInputFilePath, true);
        invalidRecordsSheet.getWorkbook().write(outStream);
        invalidRecordsSheet.getWorkbook().close();
        outStream.flush();
        return invalidRecordFile;
    } catch (RuntimeException e) {
        /////// check where to handle (SEV2 or SEV3.)
        log.error("failed to generate invalid recod files");
        throw e;
    }
    finally {
        if(outStream!=null)
        {
            outStream.close();
        }
    }
}

/**
 * Method to write the entry in each cell for the given row.
 *
 * @throws IllegalArgumentException -IllegalArgumentException
 */
public static void writeEntriesForEachCellInTheGivenRow(final List<String> invalidInput, final int rowCount,
                                                        @NonNull final SXSSFSheet xssfSheet) throws IllegalArgumentException {
    //log.info("writing record to excel file {} invalidInput.toString()");
    final SXSSFRow xssfRow = xssfSheet.createRow(rowCount);
    int cell = 0;
    for (final String fieldValue : invalidInput) {
        final SXSSFCell xssfCell =xssfRow.createCell(cell);
        xssfCell.setCellValue(fieldValue);
        xssfSheet.autoSizeColumn(cell);
        cell++;
    }
    //log.info("end writing record to excel file");
}

0 个答案:

没有答案