I. Page image preview:
1)
2) Click the export button to preview:
3) The final Excel table image preview:
Ii. Code Demonstration:
Note: before performing the operation, import and export the JAR file package of the Excel table. You can download the package from your own resources and search and download it online,
Find the js method executed by the Export button, and check the implementation of this method in Java the day after tomorrow.
1) JSP code:
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <HTML>
2) Java code Demonstration:
/*** Export Excel chart for driving school pass rate */Public void downjxhglpm () {codelist = codesservice. getcode ("1042"); // code Table query value jxhelpmlist = hgltjservice. getjxhglpm (tjjxkshgl); // rank the driving pass rate for (INT I = 0; I <jxhelpmlist. size (); I ++) {tjjxkshgl TT = (tjjxkshgl) jxhelpmlist. get (I); TT. setjxmc (codeutil. getjxjcbyjxxh (codelist, TT. getjxxh ();} outputstream OS = NULL; writableworkbook wbook = NULL; try {OS = new bufferedoutputstream (response. getoutputstrea M (); // obtain the output stream statement // response. getoutputstream (); // gets the output stream response. reset (); // clear the output stream string tmptitle = "Driving School pass rate ranking"; // The title response. setheader ("content-disposition", "attachment; filename =" + new string (tmptitle. getbytes (), "iso8859-1") + ". xls "); // set the output file header response. setcontenttype ("application/vnd. MS-excel "); // defines the output type wbook = workbook. createworkbook (OS); // create an Excel file writablesheet wsheet = wbook. createsheet (TMP Title, 0); // Sheet Name // set the Excel title writablefont wfont = new writablefont (writablefont. arial, 16, writablefont. bold, false, underlinestyle. no_underline, colour. black); // set the table style writablecellformat wcffc = new writablecellformat (wfont); // wcffc. setbackground (colour. aqua); label Label = new label (0, 0, tmptitle, wcffc); writablecellformat cellformat = new writablecellformat (); cellformat. setalignment (jxl. format. Alignment. centre); cellformat. setverticalalignment (jxl. format. verticalalignment. centre); cellformat. setwrap (true); // cellformat. setbackground (colour. aqua); cellformat. setfont (wfont); label. setcellformat (cellformat); wsheet. addcell (Label); // wsheet. addcell (new label (0, 0, tmptitle, wcffc); wsheet. setrowview (0,500); // The height of the first row wsheet. mergecells (0, 0, 6, 1); // merge cells (the first row in the first column and the second row in the seventh column) // wsheet. mergecells (0, 1, 9, 1); // wsheet. mergecells (0, 2, 0, 4); // wsheet. mergecells (1, 2, 3, 2); // wsheet. mergecells (4, 2, 6, 2); // wsheet. mergecells (7, 2, 9, 2); wsheet. setcolumnview (); // width wsheet. setcolumnview (); // width wsheet. setcolumnview (); // width wsheet. setcolumnview (3, 10); // The width of wsheet. setcolumnview (); // width wsheet. setcolumnview (5, 10); // width // start to generate the body content wfont = new jxl. write. writablefont (writablefont. aria L, 14, writablefont. bold, false, underlinestyle. no_underline, colour. black); wcffc = new writablecellformat (wfont); wsheet. addcell (new label (0, 2, "ranking", wcffc); wsheet. addcell (new label (1, 2, "Driving School name", wcffc); wsheet. addcell (new label (2, 2, "Subject 1", wcffc); wsheet. addcell (new label (3, 2, "Subject 2", wcffc); wsheet. addcell (new label (4, 2, "subject 3", wcffc); wsheet. addcell (new label (5, 2, "pass rate", wcffc); int COUNT = jxh Elpmlist. size (); If (count> 0) {// determines whether the set is not 0 tjjxkshgl tjhgl = NULL; For (INT I = 0; I <jxhelpmlist. size (); I ++) {tjhgl = (tjjxkshgl) jxhelpmlist. get (I); wsheet. addcell (new label (0, I + 3, (I + 1) + ""); wsheet. addcell (new label (1, I + 3, tjhgl. getjxmc (); wsheet. addcell (new label (2, I + 3, tjhgl. getkm1hgl (); wsheet. addcell (new label (3, I + 3, tjhgl. getkm2hgl (); wsheet. addcell (new label (4, I + 3, tjhgl. getkm3hgl (); wsheet. A Ddcell (new label (5, I + 3, tjhgl. getavghgl ();} wbook. write (); // Write File} catch (exception ex) {ex. printstacktrace ();} finally {If (OS! = NULL) {try {OS. Close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} if (wbook! = NULL) {try {wbook. close ();} catch (writeexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}