使用 POI 创建单元格时出错 [英] Error creating cell with POI

查看:53
本文介绍了使用 POI 创建单元格时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的 createCell 方法:

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

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

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

Echec de l'export:超出单元格样式的最大数量.你可以在 .xls 工作簿中定义多达 4000 种样式

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 ?

谢谢

推荐答案

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

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

这篇关于使用 POI 创建单元格时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆