Java POI Export Excel

Source: Internet
Author: User

jar:http://poi.apache.org/of POI

There will be a lot of jars after the download, but if it's just a simple Excel report, import a poi-version number-the date. Jar is ready.

Export code:

Private voidOutputexcel (String querydate, string[] headers, List<List<String>>Diaochas, httpservletrequest request, httpservletresponse response)throwsIOException {hssfworkbook workbook=NewHssfworkbook (); //Createsheet (Excel sheet name)Hssfsheet sheet =Workbook.createsheet (querydate); //here's how to set the headings in an Excel tableHssfcellstyle Title_style =Workbook.createcellstyle ();        Title_style.setfillforegroundcolor (HSSFColor.SKY_BLUE.index);        Title_style.setfillpattern (Hssfcellstyle.solid_foreground);        Title_style.setborderbottom (Hssfcellstyle.border_thin);        Title_style.setborderleft (Hssfcellstyle.border_thin);        Title_style.setborderright (Hssfcellstyle.border_thin);        Title_style.setbordertop (Hssfcellstyle.border_thin);        Title_style.setalignment (Hssfcellstyle.align_center); Hssffont Title_font=Workbook.createfont ();        Title_font.setcolor (HSSFColor.VIOLET.index); Title_font.setfontheightinpoints (( Short) 12);        Title_font.setboldweight (Hssffont.boldweight_bold);        Title_style.setfont (Title_font); //style of the contentHssfcellstyle Content_style =Workbook.createcellstyle ();        Content_style.setfillforegroundcolor (HSSFColor.LIGHT_YELLOW.index);        Content_style.setfillpattern (Hssfcellstyle.solid_foreground);        Content_style.setborderbottom (Hssfcellstyle.border_thin);        Content_style.setborderleft (Hssfcellstyle.border_thin);        Content_style.setborderright (Hssfcellstyle.border_thin);        Content_style.setbordertop (Hssfcellstyle.border_thin);        Content_style.setalignment (Hssfcellstyle.align_center);        Content_style.setverticalalignment (Hssfcellstyle.vertical_center); Hssffont Content_font=Workbook.createfont ();        Content_font.setboldweight (Hssffont.boldweight_normal);        Content_style.setfont (Content_font); //Fill Header ContentHssfrow row = Sheet.createrow (0);  for(inti = 0; i < headers.length; i++) {            //set the width of the caption to AdaptiveSheet.setcolumnwidth (i, headers[i].getbytes (). Length * 2 * 256); Hssfcell Cell=Row.createcell (i);            Cell.setcellstyle (Title_style); hssfrichtextstring text=Newhssfrichtextstring (Headers[i]);        Cell.setcellvalue (text); }        //filling content is embarrassing ... Lazy not set up objects, directly with the data stored in the list.          for(inti = 0; I < diaochas.size (); i++) {row= Sheet.createrow (i + 1); List<String> Diaocha =Diaochas.get (i);  for(intj = 0; J < Diaocha.size (); J + +) {Hssfcell cell=Row.createcell (j);                Cell.setcellstyle (Content_style); Hssfrichtextstring richstring=Newhssfrichtextstring (Diaocha.get (j));            Cell.setcellvalue (richstring); }        }        //call reset here () because I called Response.getwriter () in another code;Response.reset (); Response.setcontenttype ("Application/vnd.ms-excel"); Response.setheader ("Content-disposition", "attachment;filename=" + querydate + ". xls"); OutputStream Ouputstream=Response.getoutputstream ();        Workbook.write (Ouputstream);        Ouputstream.flush ();    Ouputstream.close (); }

Uh, add a MySQL function to get the date

Date_format (formatdate,'%y-%m')

Finally, the point is that when the request is exported, the AJAX request cannot be used and will not be reflected.

Java POI Export Excel

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.