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 void outputexcel (string querydate, string[] headers,             List<List<String>> diaochas,  httpservletrequest request,             Httpservletresponse response)  throws IOException {         hssfworkbook workbook = new hssfworkbook ();         //createsheet (Excel worksheet name)         hssfsheet sheet  = workbook.createsheet (querydate);         // Here's how to set the style of headings in an Excel table         HSSFCellStyle title_style =  Workbook.createcellstyle ();         title_style.setfillforegroundcolor ( HSSFColor.SKY_BLUE.index);     &nbsP;   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 content         HSSFCellStyle 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);    &nbsP;    content_style.setverticalalignment (Hssfcellstyle.vertical_center);         hssffont content_font = workbook.createfont ();         content_font.setboldweight (Hssffont.boldweight_normal);         content_style.setfont (Content_font);         // Fill header content         hssfrow row = sheet.createrow (0);         for  (int i = 0; i <  headers.length; i++)  {            // Set the width of the caption adaptive             sheet.setcolumnwidth (i,  headers[i].getbytes (). length * 2 * 256);             hssfcEll cell = row.createcell (i);             cell.setcellstyle (Title_style);             Hssfrichtextstring text = new hssfrichtextstring (Headers[i]);             cell.setcellvalue (text);         }        //filling content   embarrassing ... Lazy not set up objects, directly with the data stored in the list.         for  (int i = 0; i <  Diaochas.size ();  i++)  {            row  = sheet.createrow (i + 1);             list<string> diaocha = diaochas.get (i);             for&nBSP; (Int j = 0; j < diaocha.size ();  j++)  {                 HSSFCell cell =  Row.createcell (j);                 cell.setcellstyle (Content_style);                 hssfrichtextstring richstring = new hssfrichtextstring (                          diaocha.get (j));                 cell.setcellvalue (richstring);             }        }         //here call Reset () becauseCall Response.getwriter () in another code for me;         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.