The maximum number of cell styles that the exported data exceeds. You can define up to 4000 styles

Source: Internet
Author: User

The maximum number of cell styles was exceeded. You can define up to 4000 styles POI operations Excel, when the exported data is not very large, then there is no problem, and the data is many or more,

will report the following error, is due to the cell styles too many create, it is generally possible to put CellStyle settings outside the loop

The error is as follows:

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)
... More

-------------Example--------------

Error example

Correct code after correction

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

Above method original address: http://blog.csdn.net/hoking_in/article/details/7919530


Method Two (not recommended, affect performance):

1.4000 Maximum style error

Java.lang.IllegalStateException:The Maximum number of cell styles was exceeded. You can define-4000 styles in a. xls workbook error

Find Org.zkoss.poi.hssf.usermodel.HSSFWorkbook in Zpoi.jar to modify the maximum number of styles within the Createcellstyle function. Re-Zpoi.jar can be played.

  1. Public Hssfcellstyle Createcellstyle ()
  2. {
  3. if (workbook.getnumexformats () = = Max_styles) {
  4. throw new IllegalStateException ("The maximum number of cell styles was exceeded." +
  5. "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 compatibility issues

If there is a compatibility problem with the IE series browser (specifically, the report does not appear), I know that there may be problems with the Excel template, the number is written in string form, or a write date format of the cell has a write error. The specific solution is as follows.

Write code in digital form

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


Write Date Code

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


Above method original address: http://realgodo.iteye.com/blog/1105529

The maximum number of cell styles that the exported data exceeds. You can define up to 4000 styles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.