Error creating cell with POI

随声附和 提交于 2019-12-12 10:48:16

问题


I do an export from Java to xls, i use POI library.

My createCell Method:

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;
}

i call this methods in a For, i have this message error:

Echec de l'export: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

How to reuse my cell without having to recreate each iteration ?

Thx


回答1:


You can not re-use the same cell for multiple rows. Instead, apply same values to a newly created cell. But you can use the same style to multiple cells.

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);
}



回答2:


Use,

newCellStyle = oldCell.getCellStyle();
newCell.setCellStyle(newCellStyle); 

instead of,

newCellStyle.cloneStyleFrom(oldCell.getCellStyle());



来源:https://stackoverflow.com/questions/20143596/error-creating-cell-with-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!