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. 【HDOJ】4317 Unfair Nim

    基本的状态压缩,想明白怎么dp还是挺简单的.显然对n个数字进行状态压缩,dp[i][j]表示第i位状态j表示的位向高位产生了进位. /* 4317 */ #include <iostream&g ...

  2. 1701. Ostap and Partners(并查集-关系)

    1701 又是类似食物链的这一类题 这题是找与根节点的和差关系 因为0节点是已知的 为0  那么所有的都可以转换为与0的和差关系 可以规定合并的两节点 由大的指向小的 然后再更新和差关系 有可能最后有 ...

  3. 各种html5 的 polyfill

    https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills 配合 Modernizr

  4. POJ3691DNA repair

    题解: 构建出trie图,令f[i][j]表示到第i个字符走到j号节点最少需要修改的字符数,然后枚举后继节点转移即可. 代码:没写caseWA了n发... #include<cstdio> ...

  5. [scu 4423] Necklace

    4423: Necklace Description baihacker bought a necklace for his wife on their wedding anniversary. A ...

  6. Android使用Webview加载网页

    安卓使用Webview来加载和显示网页内容,首先在layout文件中定义Webview <?xml version="1.0" encoding="utf-8&qu ...

  7. 【众秒之门 JavaScript与jQuery技术精粹 #BOOK#】第1章 初学JavaScript需知的七件事

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  8. 剑指Offer:打印从1到最大的n位数

    题目:输入数值n,按顺序打印从1到最大的n位数,例如输入n=3,则从1,2,3,一直打印到999 陷阱:若使用循环遍历 1- 999...9 并依次输出,当位数n过大时,无论将其存入int或long或 ...

  9. Android样式——Styles

    说明 样式(style)是属性的集合,用来指定View或者Window的外观和格式. 这些属性可以是height(高度).padding(内边距).font size(字体颜色)等. 样式定义在另一个 ...

  10. CF 560e Gerald and Giant Chess

    题意:在h×w的棋盘中从左上角走到右下角,只能向右或向下走,有n个点不可以经过,一共有多少种方案. 解法:dp.先对点按横坐标排序(横坐标相等按纵坐标,也可以反过来)dp[i]表示不经过其他非法点走到 ...