The table in the JSP is exported to Excel__js

Source: Internet
Author: User

Package COM.ITIPTOP.JXL; Import Java.io.BufferedOutputStream; Import Java.io.OutputStream; Import Java.util.Calendar; Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletResponse; Import JXL. workbook; Import Jxl.write.Label; Import Jxl.write.WritableSheet; Import Jxl.write.WritableWorkbook; Import Org.apache.struts.action.ActionForm; Import Org.apache.struts.action.ActionForward; Import org.apache.struts.action.ActionMapping; Import com.itiptop.portal.action.SecurityAction; public class Exporttabletoexcel extends SecurityAction {@Override the Public Actionforward service (actionmapping mapping, Ac Tionform form, httpservletrequest request, httpservletresponse response) throws Exception {String cellsstr = new String (r Equest.getparameter ("Arraystr"). GetBytes ("Iso-8859-1"), "UTF-8"); String[] cells = Cellsstr.split (":"); System.out.println (CELLSSTR); String filename = calendar.getinstance (). Gettimeinmillis () + ". xls"; filename = new String (Filename.getbytes ("UTF-8 ")," iso-8859-1 "); Response.AddHeader ("Content-disposition", (New StringBuilder ()). Append ("Attachment;filename="). Append (filename). ToString ()); OutputStream toclient = new Bufferedoutputstream (response. Getoutputstream ()); Exporttojxlexcel (filename, "test", cells, toclient); Toclient.write (Cellsstr.tostring (). GetBytes ()); Toclient.flush (); Toclient.close (); return null; /** *///writes Excel objects directly to the output stream, the user accesses the Web server through the browser, * If the HTTP headers are set correctly, the browser automatically invokes the client's Excel application, * to display the dynamically generated Excel spreadsheet * @param fileName * @param sheetname * @param cellstrarray * @param OS * @return/public string Exporttojxlexcel (string fileName, String s Heetname, string[] cellstrarray, OutputStream os) {try {//FileOutputStream OS = new FileOutputStream (fileName); if (cel Lstrarray!= null && cellstrarray.length > 0 {writableworkbook WWB = workbook.createworkbook (OS); Writablesheet ws = Wwb.createsheet (sheetname, 0); for (int i = 0; i < i++) Ws.setcolumnview (I, 20); Label LBL = null; string[] Objprops = null; int col; int row; int RowSpan; int ColSpan; for (String Objstr:cellstrarray) {//each object in row,col,rowspan,colspan,value form objprops = Objstr.split (","); if (objprops.le Ngth > 4) {col = Integer.parseint (objprops[1]); row = Integer.parseint (objprops[0)); RowSpan = Integer.parseint (Objpro PS[2]); ColSpan = Integer.parseint (objprops[3]); LBL = new Label (col, Row, objprops[4]); Ws.addcell (LBL); if (RowSpan > 1 | | colSpan > 1) ws.mergecells (col, Row, col + colSpan-1, row + rowSpan-1); } wwb.write (); Wwb.close (); return fileName; } catch (Exception e) {e.printstacktrace ();} return null; } }

JS:

/** Browser judgment */var Sys = {}; var ua = Navigator.userAgent.toLowerCase (); if (window. ActiveXObject) sys.ie = Ua.match (/msie ([D.] +)/) [1]; else if (document.getboxobjectfor) Sys.firefox = Ua.match (/firefox//([/D.] +)/) [1]; function Containsarray (array, obj) {for (var i = 0; i < Array.Length. i++) {if (array[i] = = obj) {return i; break;} } return-1; } Array.prototype.contains = function (obj) {return Containsarray (this, obj);} function Exporttabletojxlexcel (tableId) { var offsetleftarray = new Array (); var cell;//cell Dom Var col;//cell is actually listed Var cellstr;//each cell in row,col,rowspan,colspan,value form var cellstrarray = []; var objtab = document.getElementById (tableId); Traversal first takes out the offsetleft set for (var i = 0; i < objTab.rows.length; i++) {for (var j = 0; J < Objtab.rows[i].cells.lengt H J + +) {cell = Objtab.rows[i].cells[j]; if (offsetleftarray.contains (cell.offsetleft) = = 1) offsetleftarray.push ( Cell.offsetleft); } offsetleftarray.sort (function (x, y) {return parseint (x)-ParseInt (y); }); Alert ("Offsetleft set:" + offsetleftarray.join (', ')); Traversal second generation Cellstrarray for (var i = 0; i < objTab.rows.length; i++) {for (var j = 0; J < Objtab.rows[i].cells.lengt H J + +) {cell = Objtab.rows[i].cells[j]; col = offsetleftarray.contains (cell.offsetleft); cellstr = i + ', ' + col + ', ' + CE Ll.rowspan + ', ' + Cell.colspan + ', ' + (Sys.firefox?cell.textcontent:cell.innertext); Cellstrarray.push (CELLSTR); }//show//var str = "row, column, Rowspan,colspan, value/n"; str = cellstrarray.join (' n '); alert (str); Upload Cellstrarray to the background}

JSP Request:

function Exportexl () {var arraystr=exporttabletojxlexcel (' exporttable '); var url= "<%=request.getcontextpath ()% >/exporttoexl.do?arraystr= "+arraystr+" SheetName "+sheetname; window.open (URL, ' new4 ', ' Left=220,top=40,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars =yes,resizable=yes '); };

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.