Java implementation Export Excel (Java generates Excel and exports files)
There are often some data that need to be exported to Excel format, so you need to implement
Begin:
1. Add Jar
Poi-3.6-20091214.jar
Commons-logging-1.1.jar
Junit-3.8.1.jar
Log4j-1.2.13.jar
2. Implementation code:
1 /**2 * Create Excel3 * 4 * @paramList5 * @paramRequest6 * @paramResponse7 */8 Private voidExcel (list<mybrowselog>list, httpservletrequest request, httpservletresponse response) {9OutputStream OutputStream =NULL;TenResponse.setcharacterencoding ("Utf-8"); OneResponse.setcontenttype ("Application/x-msdownload"); A Try { -Response.setheader ("Content-disposition", -"Attachment;filename=" +NewString ("Personal user Statistics". GetBytes ("Utf-8"), "iso8859-1") + ". xls"); the}Catch(unsupportedencodingexception E2) { - e2.printstacktrace (); - } - Try { +OutputStream =Response.getoutputstream (); -}Catch(IOException E1) { + e1.printstacktrace (); A } at -Sxssfworkbook Workbook =NewSxssfworkbook (List.size () + 1); -Sheet Sheet = Workbook.createsheet ("Personal User statistics"); -Row Headrow = Sheet.createrow (0); -Cell cell0 = Headrow.createcell (0); -Cell0.setcellvalue ("User name"); inCell cell1 = Headrow.createcell (1); -Cell1.setcellvalue ("View Amount"); toCell cell2 = Headrow.createcell (2); +Cell2.setcellvalue ("Download Volume"); - //Create Rows the for(inti = 0; I < list.size (); i++) { *Row DataRow = Sheet.createrow (i + 1); $Cell cell_0 = Datarow.createcell (0);Panax NotoginsengCell_0.setcellvalue (List.get (i) = =NULL? "": List.get (i). Getuserloginname ()); -Cell Cell_1 = Datarow.createcell (1); theCell_1.setcellvalue (List.get (i) = =NULL? "": List.get (i). Getbrowsecount ()); +Cell cell_2 = Datarow.createcell (2); ACell_2.setcellvalue (List.get (i) = =NULL? "": List.get (i). Getdownloadcount ()); the } + Try { - Workbook.write (outputstream); $}Catch(IOException E1) { $ e1.printstacktrace (); - } - Try { the Outputstream.flush (); - outputstream.close ();Wuyi}Catch(IOException e) { the e.printstacktrace (); - } Wu}
3. Detailed parameters
HSSF (the component used to manipulate Excel) provides users with objects in the Rg.apache.poi.hssf.usermodel package, including Excel objects, styles and formatting, and auxiliary operations. There are several objects:
Common components: Hssfworkbook Excel's Document Object hssfsheet Excel's form Hssfrow Excel's line Hssfcell Excel grid cell hssffont Excel font hssfdataformat date format hssfheader sheet Header Hssffooter sheet tail (only visible when printing) style: hssfcellstyle cell style helper operations include: hssfdateutil Date hssfprintsetup Printing hssferrorconstants error Information table
4. Basic Operation steps
1. Use Hssfworkbook to open or create "Excel file Object" 2, return with Hssfworkbook object or create sheet object 3, return Row object with sheet object, Get Cell object 4 with Row object, Read and write to the Cell object.
Personal references to others ' blogs and their own projects:
53213130
Java implementation Export Excel file