Excel需要更多时间来打开> 10k记录:apache poi

时间:2019-02-07 13:32:12

标签: excel apache apache-poi

我已经从Jtable中读取了数据,并使用apache poi将日期写入了excel。

我的代码: 初始化工作表:  公共XlsxVisitorFactory(字符串标题)引发IOException {

    tempFile = File.createTempFile("Test", ".xlsx");
    tempFile.deleteOnExit();
    wb = new SXSSFWorkbook();        
    out = new FileOutputStream(tempFile);
   rowIndex = 0;
    sh = wb.createSheet("Sheet 1");
    if (title != null) {
        Row row = sh.createRow(++rowIndex);
        Cell cell = row.createCell(0);
        cell.setCellValue(title);
    }
    createHeaderCellStyle();
    createHyperLinkCellStyle();
    createWrapTextCellStyle(); 
}

... 一旦数据添加到单元格中。

在页脚中,我编写了以下代码以打开文件。

            wb.write(out);
            out.close();
            Desktop.getDesktop().open(tempFile);

请帮助。

公共类XlsxVisitorFactory实现TableVisitorFactory {

private File tempFile;
private FileOutputStream out;
private Workbook wb;
private Sheet sh;
private Row currentRow;
private int rowIndex;
private static Logger log = LoggerFactory.getLogger(XlsxVisitorFactory.class);
private  CellStyle headerStyle;
private Font headerFont;
private XSSFCellStyle hyperlinkStyle;
private CellStyle wrapStyle;

public XlsxVisitorFactory() {

}

// Create a tempFile to use
public XlsxVisitorFactory(String title) throws IOException {
    tempFile = File.createTempFile("Test", ".xlsx");
    tempFile.deleteOnExit();
    wb = new XSSFWorkbook();
    out = new FileOutputStream(tempFile);
    rowIndex = 0;
    sh = wb.createSheet("Sheet 1");
    if (title != null) {
        Row row = sh.createRow(++rowIndex);
        Cell cell = row.createCell(0);
        cell.setCellValue(title);
    }

}

private class TableGroupVisitor implements TableVisitor {
    @Override
    public void visit(String tableName, int columnCount) {
        if (tableName != null) {
            Row row = sh.createRow(++rowIndex);
            Cell cell = row.createCell(0);
            cell.setCellValue(tableName);
        }
    }
}

private class DocumentFooterVisitor implements NameVisitor {
    @Override
    public void visit(String name) {
        try {
            Row row = sh.createRow(++rowIndex);
            Cell cell = row.createCell(0);
            cell.setCellValue(name);
            wb.write(out);
            out.close();
            //Desktop.getDesktop().open(tempFile);
            //InputStream is = new FileInputStream(tempFile);
            //StreamingReader.builder().rowCacheSize(100).bufferSize(4096).open(tempFile.g//etAbsoluteFile());

        } catch (IOException e) {
            throw new RuntimeException("Unable to start Excel: " + e.getMessage());
        }
    }
}



private class TableCellVisitor implements CellVisitor {
    @Override
    public void visit(String name, int index) {
        if (index == 0) {
            currentRow = sh.createRow(++rowIndex);
        }

        Cell cell = currentRow.createCell(index);
            String nameNoHtml = name.toString().replaceAll("\\<.*?>", "");
            cell.setCellStyle(wrapStyle);
            if (isDouble(name)) {
                cell.setCellValue(Double.parseDouble(nameNoHtml));
            } else {
                cell.setCellValue(nameNoHtml);
            }
        }
    }

    @Override
    public void visitForColumnWidth(final int actualRowCount) {

        for (int x = 0; x < actualRowCount; x++) {
            sh.autoSizeColumn(x);
          }

}

@Override
public void visitForColumnWidth(int actualRowCount) {
    // TODO Auto-generated method stub

}

public XSSFSheet readFromExcel(File file) throws IOException {
    FileInputStream ins = new FileInputStream(file);
    // Create Workbook instance holding reference to .xlsx file
    XSSFWorkbook workbook = new XSSFWorkbook(ins);

    // Get first/desired sheet from the workbook
    XSSFSheet sheet = workbook.getSheetAt(0);
    ins.close();
    return sheet;
}

public void setRowIndex(int rowIndex) {
    this.rowIndex = rowIndex;
}
public TableVisitor createTablePageBreakVistor() {
    // TODO add logic for page break
    return null;
}

@Override
public boolean isPageBreakAllowed() {
    return false;
}

}

0 个答案:

没有答案