使用POI创建单元格时出错

Mer*_*cer 5 java excel apache-poi

我从Java导出到xls,我使用POI库.

我的createCell方法:

private Cell createCell(Row ligne, int col, String value, CellStyle style, HSSFWorkbook classeur) {        
    //org.apache.poi.hssf.usermodel.HSSFOptimiser.optimiseCellStyles(classeur);
    CellStyle styleCell = classeur.createCellStyle();
    styleCell.cloneStyleFrom(style);
    return createCell(ligne, col, value, styleCell);
}


protected Cell createCell(Row ligne, int col, String value, CellStyle style) {
    Cell cell = createCell(ligne, col, value);
    cell.setCellStyle(style);
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

我在For中调用此方法,我有此消息错误:

Echec de l'export:超出了最大单元格样式数.您可以在.xls工作簿中定义最多4000个样式

如何重用我的单元格而不必重新创建每次迭代?

谢谢

小智 12

您不能为多行重复使用相同的单元格.而不是将相同的值应用于新创建的单元格.但是您可以对多个单元格使用相同的样式.

CellStyle cellStyle = workSheet.getWorkbook().createCellStyle();
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
cellStyle.setWrapText(true);

for (int i = 0; i <= records.size(); i++) {
    // Create a new row
    Row row = workSheet.createRow((short) i);
    Cell cell001 = row.createCell(columnIndex);
    cell001.setCellValue("some value");
    cell001.setCellStyle(cellStyle);
}
Run Code Online (Sandbox Code Playgroud)