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