用POI创build单元格时出错
我做一个从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; }
我在这个方法中调用了一个For,我有这个消息错误:
Echec de l'export:已超出单元格样式的最大数量。 您可以在.xls工作簿中定义最多4000个样式
如何重用我的单元格,而不必重新创build每个迭代?
谢谢
你不能重复使用同一个单元格的多行。 而不是那些新创build的单元格应用相同的值。 但是你可以使用相同的风格来多个单元格。
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); }
使用,
newCellStyle = oldCell.getCellStyle(); newCell.setCellStyle(newCellStyle);
代替,
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());