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
- CellStyle style = Workbook.createcellstyle ();
- Font font = Workbook.createfont ();
- Font.setboldweight (Font.boldweight_bold);
- Style.setfont (font);
- for (int i = 0; i < 10000; i++) {
- Row row = Sheet.createrow (i);
- Cell cell = Row.createcell ((short) 0);
- Cell.setcellstyle (style);
- }
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.
- Public Hssfcellstyle Createcellstyle ()
- {
- if (workbook.getnumexformats () = = Max_styles) {
- throw new IllegalStateException ("The maximum number of cell styles was exceeded." +
- "Can define up to 4000 styles in a. xls workbook");
- }
- Extendedformatrecord XFR = WORKBOOK.CREATECELLXF ();
- Short index = (short) (Getnumcellstyles ()-1);
- Hssfcellstyle style = new Hssfcellstyle (index, XFR, this);
- Workbook.createcellxfext (index);
- return style;
- }
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
- public static Cell Writenumericvalue (Sheet Sheet, int row, int column,
- Double value) {
- Row Poirow = Sheet.getrow (row);
- if (Poirow = = null) {
- Poirow = Sheet.createrow (row);
- }
- Cell Poicell = Poirow.getcell (column);
- if (Poicell! = null) {
- Poirow.removecell (Poicell);
- }
- Poicell = Poirow.createcell (column);
- Poicell.setcelltype (Cell.cell_type_numeric);
- Poicell.setcellvalue (value);
- return Poicell;
- }
Write Date Code
- public static Cell Writedatevalue (Workbook book, Sheet Sheet, int row,
- int column, Date value) {
- Row Poirow = Sheet.getrow (row);
- Creationhelper createhelper = Book.getcreationhelper ();
- if (Poirow = = null) {
- Poirow = Sheet.createrow (row);
- }
- Cell Poicell = Poirow.getcell (column);
- if (Poicell = = null) {
- Poicell = Poirow.createcell (column);
- }
- CellStyle CellStyle = Book.createcellstyle ();
- Cellstyle.setdataformat (Createhelper.createdataformat (). GetFormat (
- "Yyyy-mm-dd"));
- if (value = null) {
- Poicell.setcellvalue (value);
- } else {
- Poicell.setcellvalue (New Date ());
- }
- Poicell.setcellstyle (CellStyle);
- return Poicell;
- }
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