In actual projects, the function of exporting data to excel is often available. In the previous stage, I studied how to export data from poi to excel and organized an export tool class. I will paste the code below, learn from each other!
Tool
Import Java. io. outputstream; import java.net. urlencoder; import javax. servlet. HTTP. httpservletresponse; import Org. apache. poi. hssf. usermodel. hssfcell; import Org. apache. poi. hssf. usermodel. hssfcellstyle; import Org. apache. poi. hssf. usermodel. hssffont; import Org. apache. poi. hssf. usermodel. hssfrow; import Org. apache. poi. hssf. usermodel. hssfsheet; import Org. apache. poi. hssf. usermodel. hssfworkbook;/*** export Excel tool class * @ author goodboyloveyy **/public abstract class excelutils {/*** generate a general excel template * @ Param response * response, set the generated file type, file header encoding method, and file name, and output * @ Param firstline * Title String Array string [] * @ Param sheetname * worksheet name * @ Param filename * file name */@ suppresswarnings ("deprecation ") public void Excel (httpservletresponse response, string [] firstline, string sheetname, string filename) throws exception {hssfworkbook WB = new hssfworkbook (); // Excel file, an Excel file contains multiple tables. createsheet (); // table. A table contains multiple rows. setsheetname (0, sheetname, hssfworkbook. encoding_utf_16); // sets sheet Chinese encoding // sets fonts and other styles hssffont font = WB. createfont (); font. setfontheightinpoints (short) 12); font. setfontname ("Courier New"); hssfcellstyle style = WB. createcellstyle (); style. setfont (font); style. setwraptext (true); style. setalignment (hssfcellstyle. align_left); style. setverticalignment (hssfcellstyle. vertical_center); hssfrow row; // row. A row contains multiple hssfcell cells. // cell ROW = sheet. createrow (0); // row generated by hssfsheet. setheightinpoints (float) 15); // set the Row Height // generate the first line title for (short J = 0; j <firstline. length; j ++) {Cell = row. createcell (j); // The cell is generated by the row. setcellstyle (style); // set the cell style cell. setencoding (hssfcell. encoding_utf_16); // sets the Chinese cell encoding. Cell. setcellvalue (firstline [J]); sheet. setcolumnwidth (J, (short) (5000); // set the column width} filldata (sheet, style ); // This method is implemented when a specific call is made. // export outputstream out = NULL; response. setcontenttype ("application/X-msdownload"); // set the generated file type response. setheader ("content-disposition", "attachment; filename =" + urlencoder. encode (filename, "UTF-8"); out = response. getoutputstream (); // gets the output flow WB. write (out); // write excelout. close ();}/*** this method is implemented when a specific table is generated (data is filled in the method body) ** @ Param sheet * @ Param style */public abstract void filldata (hssfsheet sheet, hssfcellstyle style );}
Call the tool class for export
Public String exportcardtypelist () {user = (User) actioncontext. getcontext (). getsession (). get ("user"); If (user = NULL) {return login;} else {string [] cloumnname = {"card type number", "card type name "}; // export the column name string sheetname = "Product Information"; // Sheet Name string filename = "cardtypelist.xls"; // file name httpservletresponse response = servletactioncontext. getresponse (); try {cardtypelist = cardmanageservice. loadcardtype (); // query the data ex Celutils excelutil = new excelutils () {// generate a tool class instance and implement the data population abstraction @ override ("deprecation") @ overridepublic void filldata (hssfsheet sheet, hssfcellstyle style) {ssfrow row; // row. A row contains multiple hssfcell cells. // cell if (null! = Cardtypelist | cardtypelist. Size ()! = 0) {for (int K = 0; k <cardtypelist. size (); k ++) {ROW = sheet. createrow (k + 1); row. setheightinpoints (float) 15); cardType = cardtypelist. get (k); cell = row. createcell (short) 0); cell. setcellstyle (style); cell. setencoding (hssfcell. encoding_utf_16); cell. setcellvalue (systemutil. isnull (string. valueof (cardType. gettypeid (); // product no. Cell = row. createcell (short) 1); cell. setcellstyle (style); Cell. setencoding (hssfcell. encoding_utf_16); cell. setcellvalue (systemutil. isnull (cardType. getname (); // product name }}}; excelutil. excel (response, cloumnname, sheetname, filename); return NULL;} catch (exception e) {logger. error ("exportcardtypelist error! ", E); Return Error ;}}}