POI操作Excel中,导出的数据不是很大时,则不会有问题,而数据很多或者比较多时,

就会报以下的错误,是由于cell styles太多create造成,故一般可以把cellstyle设置放到循环外面

报错如下:

Caused by: java.lang.IllegalStateException: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook
at org.apache.poi.hssf.usermodel.HSSFWorkbook.createCellStyle(HSSFWorkbook.java:1144)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.createCellStyle(HSSFWorkbook.java:88)
at com.trendmicro.util.toExcel.ExcelExporter.addWorkbook(ExcelExporter.java:612)
at com.trendmicro.util.toExcel.ExcelExporter.exportToExcel(ExcelExporter.java:112)
at com.trendmicro.util.toExcel.ReportExporter.exportAutomationReport(ReportExporter.java:190)
at com.trendmicro.view.reports.TestCaseAutomationBean.exportAutoReport(TestCaseAutomationBean.java:856)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 33 more

-------------示例--------------

错误示例

改正后正确代码

  1. CellStyle style = workbook.createCellStyle();
  2. Font font = workbook.createFont();
  3. font.setBoldweight(Font.BOLDWEIGHT_BOLD);
  4. style.setFont(font);
  5. for (int i = 0; i < 10000; i++) {
  6. Row row = sheet.createRow(i);
  7. Cell cell = row.createCell((short) 0);
  8. cell.setCellStyle(style);
  9. }

以上方法原地址:http://blog.csdn.net/hoking_in/article/details/7919530

方法二(不推荐,影响性能):

1.4000最大样式错误

java.lang.IllegalStateException: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook错误

找到zpoi.jar中org.zkoss.poi.hssf.usermodel.HSSFWorkbook修改createCellStyle函数内的最大样式数量即可。重新打zpoi.jar即可。

  1. public HSSFCellStyle createCellStyle()
  2. {
  3. if(workbook.getNumExFormats() == MAX_STYLES) {
  4. throw new IllegalStateException("The maximum number of cell styles was exceeded. " +
  5. "You can define up to 4000 styles in a .xls workbook");
  6. }
  7. ExtendedFormatRecord xfr = workbook.createCellXF();
  8. short index = (short) (getNumCellStyles() - 1);
  9. HSSFCellStyle style = new HSSFCellStyle(index, xfr, this);
  10. workbook.createCellXFExt(index);
  11. return style;
  12. }

IE兼容问题

如果IE系列的浏览器出现了兼容问题(具体就是显示不出报表来),我已知的可能出现的问题有,Excel模板里有数字已字符串形式写入,或者是写日期格式的单元格出现了写入错误。具体的解决如下。

数字形式写入代码

  1. public static Cell writeNumericValue(Sheet sheet, int row, int column,
  2. Double value) {
  3. Row poiRow = sheet.getRow(row);
  4. if (poiRow == null) {
  5. poiRow = sheet.createRow(row);
  6. }
  7. Cell poiCell = poiRow.getCell(column);
  8. if (poiCell != null) {
  9. poiRow.removeCell(poiCell);
  10. }
  11. poiCell = poiRow.createCell(column);
  12. poiCell.setCellType(Cell.CELL_TYPE_NUMERIC);
  13. poiCell.setCellValue(value);
  14. return poiCell;
  15. }

写入日期代码

  1. public static Cell writeDateValue(Workbook book, Sheet sheet, int row,
  2. int column, Date value) {
  3. Row poiRow = sheet.getRow(row);
  4. CreationHelper createHelper = book.getCreationHelper();
  5. if (poiRow == null) {
  6. poiRow = sheet.createRow(row);
  7. }
  8. Cell poiCell = poiRow.getCell(column);
  9. if (poiCell == null) {
  10. poiCell = poiRow.createCell(column);
  11. }
  12. CellStyle cellStyle = book.createCellStyle();
  13. cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(
  14. "yyyy-mm-dd"));
  15. if (value != null) {
  16. poiCell.setCellValue(value);
  17. } else {
  18. poiCell.setCellValue(new Date());
  19. }
  20. poiCell.setCellStyle(cellStyle);
  21. return poiCell;
  22. }

以上方法原地址:http://realgodo.iteye.com/blog/1105529

The maximum number of cell styles was exceeded. You can define up to 4000 styles的更多相关文章

  1. 【POI】导出xls文件报错:The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

    使用POI导出xls文件,由于数据过多,导致导出xls报错如下: The maximum number of cell styles was exceeded. You can define up t ...

  2. Failed to connect to database. Maximum number of conections to instance exceeded

    我们大体都知道ArcSDE的连接数有 48 的限制,很多人也知道这个参数可以修改,并且每种操作系统能支持的最大连接数是不同的. 如果应用报错:超出系统最大连接数 该如何处理? 两种解决办法: 第一,首 ...

  3. Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance

    Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance

  4. Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance 7d90eb80-29e2-4238-b658-ade407ff9456. Last exception: [u'Traceback (most recent call last):\n', u' File "/usr/lib/py

    Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance 7d90eb80-29e2-42 ...

  5. openstack-HTTP exception thrown: Maximum number of ports exceeded错误解决方案

    最近几天什么都没动无法创建云主机了,经过一番查询 1.查日志 /data/jumpserver/logs 得到错误 HTTP exception thrown: Maximum number of p ...

  6. Error: 实例 "ddd" 执行所请求操作失败,实例处于错误状态。: 请稍后再试 [错误: Exceeded maximum number of retries. Exhausted all hosts available for retrying build failures for instance 6f60bc06-fcb6-4758-a46f-22120ca35a71.].

    Error: 实例 "ddd" 执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: Exceeded maximum number of retries. Exhaus ...

  7. "Your computer could not be joined to the domain. You have exceeded the maximum number of computer accounts you are allowed to create in this domain. Contact your system administrator to have this limit reset or increased."

    用一个普通的域帐号玩私有云的时候,遇到了如下的报错. "Your computer could not be joined to the domain. You have exceeded ...

  8. 错误: 实例 "ahwater-linux-core" 执行所请求操作失败,实例处于错误状态。: 请稍后再试 [错误: Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance 7c1609c9-9d0f-4836-85b3-cefd45f942a7. Last exception: [u

    错误: 实例 "ahwater-linux-core" 执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: Exceeded maximum number of ret ...

  9. ORA-00019: maximum number of session licenses exceeded 超出最大会话许可数

    ORA-00019: maximum number of session licenses exceededORA-00019: 超出最大会话许可数 Cause:       All licenses ...

随机推荐

  1. mysql运算符的优先级

    Operator precedences are shown in the following list, from highest precedence to the lowest. Operato ...

  2. Notepad++加上xml格式化的功能

    工作中需要用代码创建一个XML文件,创建完发现XML内容都处在同一行,导致非常不容易查看清楚XML代码.于是习惯性地用 Notepad++查看,想用它来对XML代码格式化一下. 于是找到了TextFX ...

  3. 函数 buf_chunk_init

    http://www.tuicool.com/articles/3QbYfm http://www.360doc.com/content/13/1216/17/12904276_337644353.s ...

  4. C# Process.Kill() 拒绝访问(Access Denied) 的解决方案

    需求:很多时候我们需要后台运行几个Console来不停的计算数据,那么部署到客户服务器后,如果出现突发异常,程序挂掉了,那...? 解决方案:封装了一个对后台运行程序不停监测的功能,如果发现程序有异常 ...

  5. 【转载】LVS+MYCAT+读写分离+MYSQL主备同步部署手册(邢锋)

    LVS+MYCAT+读写分离+MYSQL主备同步部署手册 1          配置MYSQL主备同步…. 2 1.1       测试环境… 2 1.2       配置主数据库… 2 1.2.1  ...

  6. python auto send email

    /*************************************************************************** * python auto send emai ...

  7. C++学习笔记:不用sizeof判断int类型占用几个字节

    #include <stdio.h> #include <string.h> char *change(int val, int base, char *retbuf) { s ...

  8. HDU 5607 graph 矩阵快速幂 + 快速幂

    这道题得到了学长的助攻,其实就是一个马尔科夫链,算出一步转移矩阵进行矩阵快速幂就行了,无奈手残 这是我第一回写矩阵快速幂,写的各种毛病,等到调完了已经8点44了,交了一发,返回PE,(发现是少了换行) ...

  9. mac usb转串口 连接树莓PI

    USB 转串口是淘宝买的 http://item.taobao.com/item.htm?spm=a1z09.2.9.50.YOJBwG&id=38963495468&_u=4m1dr ...

  10. shell管道和重定向

    看了前面一节:linux shell数据重定向(输入重定向与输出重定向)详细分析 估计还有一些朋友是头晕晕的,好复杂的重定向了.这次我们看下管道命令了.shell管道,可以说用法就简单多了. 管道命令 ...