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. 移动端调试 weinre

    weinre 是基于 Node 的工具,因此使用如下命令安装 weinre $ npm install -g weinre 用上面的命令将 weinre 安装到全局,然后就可以使用 weinre的命令 ...

  2. get mac 20150202

    getmac.sh #!/bin/sh cat oui.txt|sed -e :a -e '$!N;s/\n\s/=/;ta' -e 'P;D' | sed 's/(hex)\+/=/g' | sed ...

  3. MediaPlayer中创建AudioTrack的过程

    使用MediaPlayer播放音视频时,会创建AudioTrack对象用于播放音频数据.下面就来看看MediaPlayer创建AudioTrack的过程: 1.创建AudioTrack对象MediaP ...

  4. poj 3414 Pots ( bfs )

    题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i)        fill the ...

  5. 大四实习准备5_android广播机制

    2015-5-1 android 广播机制 5.1简介 分为标准广播(Normal broadcasts)(无先后顺序,几乎同时接收,不可截断)和有序广播(Ordered broadcasts)(有先 ...

  6. UVa 11971 (概率) Polygon

    题意: 有一根绳子,在上面随机选取k个切点,将其切成k+1段,求这些线段能够成k+1边形的概率. 分析: 要构成k+1边形,必须最长的线段小于其他k个线段之和才行. 紫书上给出了一种解法,但是感觉理解 ...

  7. PhpStorm快捷键设置/个性化设置,

    #常用快捷键 设置快捷键:File -> Settings -> IDE Settings -> Keymap -> 选择“Eclipse” -> 然后“Copy”一份 ...

  8. 【转】Windows搭建Eclipse+JDK+SDK的Android

    原文网址:http://blog.csdn.net/sunboy_2050/article/details/6336480 一 相关下载 (1) Java JDK下载: 进入该网页: http://j ...

  9. Kettle简介

    ETL和Kettle简介     ETL即数据抽取(Extract).转换(Transform).装载(Load)的过程.它是构建数据仓库的重要环节.数据仓库是面向主题的.集成的.稳定的且随时间不断变 ...

  10. HDU5692 Snacks DFS+线段树

    分析:一棵以1为根的有根树,然后每个点维护从根到当前节点的路径和,当修改一个点时 只会影响的子树的和,最优值也是子树最大的值 #include <cstdio> #include < ...