The example in this article tells you how JSP exports Excel and supports sheet export. Share to everyone for your reference, specific as follows:
Public DownloadFile exportToExcel () throws exception{String config_value = Systemconfigservice.getconfigvaluebykey ("
Export.xls ");
Logger.info ("Get export type is execl table" + ", sheet the number of rows exported per page is:" +config_value+ "Row-------------------");
File File = new file (new SimpleDateFormat ("Yyyy-mm-dd"). Format (new Date ()) + ". xls"); Writableworkbook wbook = null;
Writablesheet Wsheet =null; Wbook = Workbook.createworkbook (file);
Build Excel file Long StartTime = System.currenttimemillis (); try {exporttoexcelsearchcondition condittion = (exporttoexcelsearchcondition) servletcontext.currentsession ().
GetAttribute ("condittion"));
list<contenttoexcel> CTE = new arraylist<contenttoexcel> ();
if (condittion = = null) {throw new Exception ("Session Fetch query condition failed"); }else{//Based on criteria query needs to export data CTE = Category2contentdao.searchbycondition (Condittion.getcategoryid (), Condittion.gethsdfla g (), Condittion.getexternalcontentid (), Condittion.getcontentname (), Condittion.getcontentid (), Condittion.getsystemid (),Condittion.getstate (), Condittion.getcpid (), Condittion.getcontenttype (), Condittion.getbeforemodifytime (),
Condittion.getaftermodifytime ());
Logger.info ("Start exporting Excel Tables--");
if (Stringutils.isnotblank (config_value)) {int value = Numberutils.toint (config_value); if (CTE!=null && cte.size () >0) {int k = 0;//cent sheet number int i = 0;//line number for looping Excel Iterator<contentto
Excel> it = Cte.iterator ();
map<string,string> Contentidandcontenttypemap = new hashmap<string,string> ();
map<string,string> contentidanddurationmap= new hashmap<string,string> (); for (Contenttoexcel contenttoexcel:cte) {contentidandcontenttypemap.put (Contenttoexcel.getcontentid (), contentToEx
Cel.getcontenttype ());
///Calculate time long Contentidanddurationmap = Category2contentdao.getdurationbycontentidmap (Contentidandcontenttypemap);
/** * 1) is iterated using a iterator iterator, which optimizes iterative efficiency compared to a for loop.
* 2) A large number of data impact efficiency, the solution of large data volume.
* Add by Guohua.yuan 2013-06-08 * *while (It.hasnext ()) {Contenttoexcel contenttoexcel = It.next (); if (i% = = 0) {Wsheet = Wbook.createsheet ("Program Export (" + (int) (i/value+1) +) ", (int) (i/value+1);//sheet name//Set Place Excel font Writablefont wfont = new Writablefont (Writablefont.arial, Writablefont.bold, False, Jxl.format.Underli
Nestyle.no_underline, Jxl.format.Colour.BLACK);
Writablecellformat TitleFormat = new Writablecellformat (Wfont);
String[] title = {"Identity", "name", "type", "" Code Flow "," Time Long "," quick edit personnel "," First officer "," reviewer "," Final officer "," Last updated "," Content creation time "," Warehousing Time "," status "}; Sets the Excel header for (int j = 0 J < Title.length J + +) {Label exceltitle = new Label (j, 0, Title[j], Titlefo
Rmat);
Wsheet.addcell (Exceltitle);
} k = k+1;
Wsheet.addcell (New Label (0, i+1-value* (k-1), Contenttoexcel.getcontentid ()));
Wsheet.addcell (New Label (1, i+1-value* (k-1), Contenttoexcel.getcontentname ())); if (Contenttoexcel.getcontenttype (). Equals ("Serie")) {Wsheet. Addcell (New Label (2, i+1-value* (k-1), "TV set"));
}else if (Contenttoexcel.getcontenttype (). Equals ("Series")) {Wsheet.addcell (New Label (2, i+1-value* (k-1), "serial")); }else if (Contenttoexcel.getcontenttype (). Equals ("Movie")) {Wsheet.addcell (new Label 2, i+1-value* (k-1), "movie
"));
} if (Contenttoexcel.gethsdflag () = = 1) {Wsheet.addcell (New Label (3, i+1-value* (k-1), "SD"));
}else if (contenttoexcel.gethsdflag () = = 2) {Wsheet.addcell (New Label (3, i+1-value* (k-1), "HD"));
}else if (contenttoexcel.gethsdflag () = = 3) {Wsheet.addcell (New Label (3, i+1-value* (k-1), "Super HD")); }//Add Time Wsheet.addcell (new Label 4, i+1-value* (k-1), Contentidanddurationmap.get (Contenttoexcel.getcontentid
())));
Wsheet.addcell (New Label (5, i+1-value* (k-1), Contenttoexcel.getkbpersonner ()));
Wsheet.addcell (New Label (6, i+1-value* (k-1), Contenttoexcel.getcspersonner ())); Wsheet.addcell (New Label (7, I+1-value* (k-1), Contenttoexcel.getfspersonner ()));
Wsheet.addcell (New Label (8, i+1-value* (k-1), Contenttoexcel.getzspersonner ()));
if (Contenttoexcel.getmodifytime () ==null) {Wsheet.addcell (New Label (9, i+1-value* (k-1), "")); }else{Wsheet.addcell (New Label (9, i+1-value* (k-1), New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (Contenttoe
Xcel.getmodifytime ()));
} if (contenttoexcel.getcreatetime () = = null) {Wsheet.addcell (New Label (i+1-value* (k-1), "")); }else{Wsheet.addcell (New Label (i+1-value* (k-1), New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (Contentto
Excel.getcreatetime ()));
} if (Contenttoexcel.getinstoragetime () ==null) {Wsheet.addcell (New Label (11,i+1-value* (k-1), "")); }else{Wsheet.addcell (New Label (11,i+1-value* (k-1), New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (contenttoexc
El.getinstoragetime ())); } if (Stringutils.equals (contenttoexcel.geTstatus (), "1500")) {Wsheet.addcell (new Label (in i+1-value* (k-1), "finished product Ready")); }else if (Stringutils.equals (Contenttoexcel.getstatus (), "1700")) {Wsheet.addcell (new Label (i+1-value*), "issued
Cloth successful ")); }else if (Stringutils.equals (Contenttoexcel.getstatus (), "1800")) {Wsheet.addcell (new Label (i+1-value*), "issued
Cloth failure ")); }else if (Stringutils.equals (Contenttoexcel.getstatus (), "1901")) {Wsheet.addcell (new Label (i+1-value*), "issued
Cloth "));
} i++;
Wbook.write ()//write file}else{throw new Exception ("No Data to guide");
}else{throw new Exception ("Please check system Configuration Management to configure export type data");
The catch (Exception e) {throw new Exception (e);
}finally{if (wbook!=null) {wbook.close ();
} logger.info ("Export Excel Time Consuming:" + (System.currenttimemillis ()-StartTime) + "MS");
Servletcontext.currentsession (). RemoveAttribute ("Condittion");
return new DownloadFile (File.getname ()). Readfrom (file);
}
I hope this article will help you with JSP programming.