Java 使用 POI 创建单元格时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20143596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error creating cell with POI
提问by Mercer
I do an export from Java to xls, i use POI library.
我从 Java 导出到 xls,我使用 POI 库。
My createCell Method:
我的 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;
}
i call this methods in a For, i have this message error:
我在 For 中调用此方法,出现此消息错误:
Echec de l'export: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook
Echec de l'export:超出单元格样式的最大数量。您最多可以在 .xls 工作簿中定义 4000 种样式
How to reuse my cell without having to recreate each iteration ?
如何重用我的单元而不必重新创建每次迭代?
Thx
谢谢
回答by Md Ismail
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);
}
回答by Xdeveloper
Use,
用,
newCellStyle = oldCell.getCellStyle();
newCell.setCellStyle(newCellStyle);
instead of,
代替,
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());