Generating Excel tables with JSON

Source: Internet
Author: User

Cause:

Before using reflection to generate Excel export, this component is very useful, the results, the local development of the pit Dad no problem, the production environment is problematic. Do not know what causes the service to restart directly, but also reload the class, leading directly to the JVM's persistent memory overflow.

exception: java.lang.OutOfMemoryError:PermGen space

Description

Perm space is occupied. The exception that is thrown when storage space cannot be allocated for the new class. This exception was not previously available, but today's exception is more common in Java reflection-heavy use. The main reason is that a large number of dynamic reflection generated classes are constantly being loaded, resulting in the perm area being fully occupied.

Even more frightening is that different classloader even use the same class, but will load it, equivalent to the same thing, if there are n ClassLoader then he will be loaded n times. Therefore, in some cases, the problem is basically considered as no solution. Of course, there are a lot of classloader and a lot of reflective classes in fact there is not much.

Solution: You can write a build of an Excel table for a single data object. Cons: Not universal, I did this when I was on a mission.

Later, the task was not so heavy, I thought I could use JSON data in my spare time. The use of JSON to value, can achieve universal

Maven Reference

<Dependency>                <groupId>Net.sf.json-lib</groupId>                <Artifactid>Json-lib</Artifactid>                <version>2.4</version>                <classifier>Jdk15</classifier>            </Dependency>                <Dependency>            <groupId>Org.apache.poi</groupId>            <Artifactid>Poi-ooxml</Artifactid>            <version>3.16-beta2</version>        </Dependency>

Basic code:

 Packagecom.example;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject;ImportOrg.apache.poi.hssf.usermodel.HSSFCellStyle;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.hssf.util.HSSFColor; Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stublist<testboby> list =NewArraylist<testboby>(); Testboby a=NewTestboby (); Testboby b=NewTestboby (); A.setname ("AA"); A.setage (3); B.setage (4); B.setname ("NN");        List.add (b);        List.add (a); Jsonarray JSON= Jsonarray.fromobject (list);//first turn the string into a Jsonarray objectstring[] Heads={"Name", "Age"};    Export2excel (Heads,heads,json); }     Public Static voidExport2excel (string[] heads,string[] Names,jsonarray jsonarray) {Hssfworkbook workbook=NewHssfworkbook ();//Create an Excel fileHssfsheet Sheet= Workbook.createsheet ();//Create a sheet for ExcelHssfcellstyle Style=Workbook.createcellstyle ();        Style.setfillbackgroundcolor (HSSFColor.BLUE_GREY.index); Hssfrow Titlerow= Sheet.createrow (0);  for(inti=0;i) {Titlerow.createcell (i). Setcellvalue (Heads[i]);            } titlerow.setrowstyle (style); if(Jsonarray.size () > 0) {             for(inti = 0; I < jsonarray.size (); i++) {Hssfrow row= Sheet.createrow (i + 1); Jsonobject JSON= Jsonarray.getjsonobject (i);//Traverse Jsonarray                 for(intj=0;j<names.length;j++) {Row.createcell (j). Setcellvalue (Json.get (Names[j]). toString ()); }                                                                    }        }                Try{FileOutputStream fos=NewFileOutputStream ("A.xls");            Workbook.write (FOS);        Fos.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }        }
View Code

Generating Excel tables with JSON

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.