Table multiple table header in JSP export Excel file specific implementation

Source: Internet
Author: User

  This article mainly introduces the JSP in the Table multiple table header export Excel file implementation, the need for friends can refer to the

First, we introduce two copies of JS:copyhtmltoexcel.js and tabletoexcel.js     code as follows:/*   * The default transformation implementation function, if you need other functions, you must expand the  * parameters: & nbsp;*      tableid:html Table Object id attribute value  * detailed usage see the following Tabletoexcel object definition    */function savease Xcel (TableID) { var TB = new Tabletoexcel (TableID);   Tb.setfontstyle ("Courier new");   Tb.setfontsize (10 );   Tb.settableborder (2);   Tb.setcolumnwidth (7);   Tb.islinewrap (FALSE);   Tb.isautofit (TRUE);   Tb.getexcelfile (); }  /*  *   Features: The Table object in HTML is converted to an Excel generic object.  *   Author: Jeva  *   Time: 2006-08-09  *   Parameters: TableID  html The id attribute value of the Table object  *   Description: & nbsp;*       can adapt to the complex HTML table object's automatic conversion, can automatically according to the row and column expands the information  *       Merges the Excel the cell, The client needs to have Excel  *       Detailed properties, method reference descriptions see: Excel Microsoft Excel Visual Basic Reference  *   Demo:  * & nbsp     var TB = new Tabletoexcel (' demotable ');  *    tb.setfontsTyle ("Courier New");  *    tb.setfontsize (10);  //recommended value of  *    tb.setfontcolor (6);  //General conditions do not have to set  *    tb.setbackground (4);  //General conditions do not have to set  *    tb.settableborder (2);  //Recommended value 2  *    tb.setcolumnwidth (10);  //recommended value of  *    tb.islinewrap (false);  *    tb.isautofit (true);  *      *    tb.getexcelfile ();  *   If cell adaptation is set, the cell width is invalid  *   version: 1.0  */function Tabletoexcel (TableID) {    THIS.T Ableborder =-1; Border type,-1 no border desirable 1/2/3/4     This.background = 0; Background color: white   Desirable palette color number 1/2/3/4 ....     This.fontcolor = 1;  //font Color: black     this.fontsize = 10;  //font size     This.fontstyle = "Song Body"; Font type     This.rowheight =-1; Row height     This.columnwidth =-1; Column width     this.linewrap = true; Whether to wrap the line     this.textalign =-4108;Content Alignment   default to center     This.autofit = false;  //is adaptive width     This.tableid = tableid; }   TableToExcel.prototype.setTableBorder = function (ex Celborder) {    this.tableborder = Excelborder;};   TableToExcel.prototype.setBackGround = function (excelcolor) {    this.background = Excelcolor;};   TableToExcel.prototype.setFontColor = function (excelcolor) {    this.fontcolor = Excelcolor;};   TableToExcel.prototype.setFontSize = function (excelfontsize) {    this.fontsize = excelfontsize;};   TableToExcel.prototype.setFontStyle = function (Excelfont) {    This.fontstyle = Excelfont;};   TableToExcel.prototype.setRowHeight = function (excelrowheight) {    this.rowheight = excelrowheight;} ;   TableToExcel.prototype.setColumnWidth = function (excelcolumnwidth) {    This.columnwidth = Excelcolumnwidth; };   TableToExcel.prototype.isLineWrap = function (linewrap) {    if (Linewrap = False | | linewrap = = true) {        This.linew Rap = Linewrap;    }};   TableToExcel.prototype.setTextAlign = function (textAlign) {    this.textalign = textAlign;};   TableToExcel.prototype.isAutoFit = function (autoFit) { if (AutoFit = = True | | autoFit = FALSE)   This.aut Ofit = AutoFit; }  //File conversion main function TableToExcel.prototype.getExcelFile = function () {    var jxls, MyWorkbook, Myworksheet, my HtmlTableCell, Myexcelcell, myExcelCell2;     var mycellcolspan, Mycellrowspan;       try {      Jxls = new ActiveXObject (' Excel.Application ');    } &nbs P   catch (E) {        alert ("Unable to start Excel!nn" + e.message +              NN if you are sure that Excel is already installed on your computer, "+      "      , please adjust IE security level. NN concrete operations: nn "+         &NBSp   Tools →internet options → security → custom level →activex controls and Plug-ins nn "+        " → Enable: Initialize and script ActiveX controls not marked as safe " ;         return false;    }       jxls.visible = true;     MyWorkbook = JXls.Workbooks.Add ();     Jxls.displayalerts = false;     Myworkbook.worksheets (3). Delete ();     Myworkbook.worksheets (2). Delete ();     Jxls.displayalerts = true;     Myworksheet = Myworkbook.activesheet;       var  readrow = 0,  readcol = 0;     var totalrow = 0, totalcol = 0;     var   tabnum = 0;  //Set row height, column width     if (this.columnwidth!=-1)      myworksheet.columns.columnwidth = This.colu Mnwidth;     Else      myworksheet.columns.columnwidth = 7;     if (this.rowheight!=-1)      myworksheet.rows.rowheight = this.rowheight;  //Search for the Table object that needs to be converted, get the corresponding row, column number    var obj = document.all.tags ("table");     for (x = 0 x < obj.length x + +) {        if (obj[x].id = = This.tableid) {  &N Bsp         tabnum = x;             TotalRow = obj[x].rows.length;             for (i = 0; i < obj[x].rows[0].cells.length i++) {      &NBS P         Myhtmltablecell = obj[x].rows (0). Cells (i);                 Mycellcolspan = Myhtmltablecell.colspan;                 Totalcol = Totalcol + Mycellcolspan;             {       }    }  //Start widget simulation form     var exceltable = new Array ();     for (i = 0; I <= totalrow; i++) {        Exceltable[i] = new Array ();         for (t = 0; t <= totalcol; t++) {            exceltable[i][t] = false;        }    } &nbsp ; Start conversion table         for (z = 0; z < obj[tabnum].rows.length z++) {        Readro W = z + 1;         Readcol = 1;         for (c = 0; c < obj[tabnum].rows (z). Cells.length C + +) {            Myhtmltablecell = Obj[tabnum].rows (z). Cells (c);             Mycellcolspan = Myhtmltablecell.colspan;             Mycellrowspan = Myhtmltablecell.rowspan;             for (y = 1; y <= totalcol y++) {            & nbsp   if (exceltable[readrow][y] = = False) {                    READCO L = y;                     break;       &NBSp        }                         if (my Cellcolspan * Mycellrowspan > 1) {                Myexcelcell = Myworksheet.ce LLS (Readrow, Readcol);                 MYEXCELCELL2 = myworksheet.cells (Readrow + myCellRowSpan-1, read Col + myCellColSpan-1);                 Myworksheet.range (Myexcelcell, myExcelCell2). Merge ();                 myexcelcell.horizontalalignment = this.textalign;                 myExcelCell.Font.Size = this.fontsize;                 myExcelCell.Font.Name = This.fontstyle;                 Myexcelcell.wraptext = This.linewrap;                 MyExcelCell.Interior.ColOrindex = This.background;                 MyExcelCell.Font.ColorIndex = This.fontcolor;                 if (This.tableborder!=-1) {          &NB Sp      myworksheet.range (Myexcelcell, myExcelCell2). Borders (1). Weight = This.tableborder;                  myworksheet.range (Myexcelcell, myExcelCell2). Borders (2). Weight = This.tableborder;                  myworksheet.range (Myexcelcell, myExcelCell2). Borders (3). Weight = This.tableborder;                  myworksheet.range (Myexcelcell, myExcelCell2). Borders (4). Weight = This.tableborder;                                   m Yexcelcell.value = Myhtmltablecell.innertext;                for (row = Readrow row <= Mycellrowspan + readRow-1; row++) {        &NBSP ;           for (col = readcol Col <= mycellcolspan + readCol-1; col++) {    &NBSP ;                   Exceltable[row][col] = true;                                   & nbsp }                   Readcol = Readcol + Mycellcolspan;            } else {                Myexcelcell = m Yworksheet.cells (Readrow, Readcol);                 myexcelcell.value = Myhtmltablecell.innertext;                 myexcelcell.horizontalalignment = this.textalign;                 Myexcelcell. Font.Size = this.fontsize;                 myExcelCell.Font.Name = This.fontstyle;                 Myexcelcell.wraptext = This.linewrap;                 MyExcelCell.Interior.ColorIndex = This.background;                 MyExcelCell.Font.ColorIndex = This.fontcolor;                 if (This.tableborder!=-1) {          &NB Sp      myexcelcell.borders (1). Weight = This.tableborder;                  myexcelcell.borders (2). Weight = This.tableborder;                  myexcelcell.borders (3). Weight = This.tableborder;                  myexcelcell.borders (4). Weight = This.tableborder;               }                            &N Bsp   Exceltable[readrow][readcol] = true;                 Readcol = Readcol + 1;                     {   }     if (This.autofit = = true)      myWorksheet.Columns.AutoFit;       Jxls.usercontrol = true;     Jxls = null;     MyWorkbook = null;     Myworksheet = null; };       copyhtmltoexcel.js     code is as follows://eltalbeout This is the outer table for the exported content, mainly to set the style of border, Eldiv is the entire exported HTML part function Onhtmltoexcel (eltableout,eldiv) { try{ //Set the data before export, set   VAR for return format after export Eldivstrbak = eldiv.innerhtml;  //Set table border=1 so that there is a form line PS in Excel: Thank you for double-sided reminders   eltableout.border=1;  //filter eldiv content   var eldivstr = eldiv.innerhtml;   ELDIVSTR = replacehtml (Eldivstr, "<a", ">");   ELDIVSTR = replacehtml (Eldivstr, "</a", ">");   eldiv.innerhtml=eldivstr;      var orangeref = Document.body.createTextRange ();   Orangeref.movetoelementtext (ELDIV);   Orangeref.execcommand ("Copy");    //Returns the previous contents of the format transformation   eldiv.innerhtml = Eldivstrbak;  //Content data may be very large, so empty   Eldivstrbak = "";   ELDIVSTR = "";     var oXL = new ActiveXObject ("Excel.Application")   var owb = oXL.Workbooks.Add;   var osheet = Owb.activesheet;   Osheet.paste ();   oSheet.Cells.NumberFormatLocal = "@";   Osheet.columns ("D:d"). Select   OXL.Selection.ColumnWidth =   oXL.Visible = true;     osheet = null;   OWB = null;   Appexcel = null;  }catch (e) {  alert (e.description) &nbsp}}   function replacehtml (replacedstr,repstr,endstr) { & nbsp   var replacedstrf = "";      var replacedstrb = "";      var Repstrindex= Replacedstr.indexof (REPSTR);      while (Repstrindex!=-1) {         REPLACEDSTRF = replacedstr.substring (0,re Pstrindex);          REPLACEDSTRB = replacedstr.substring (repstrindex,replacedstr.length);          REPLACEDSTRB = replacedstrb.substring (Replacedstrb.indexof (ENDSTR) +1, Replacedstrb.length);          REPLACEDSTR = replacedstrf + replacedstrb;          Repstrindex = Replacedstr.indexof (REPSTR);     }      return replacedstr;     Write JS method in JSP page     code as follows:  //Generate Excel   function Ontabletoexcel () {   var eltabl Eout = document.getElementById ("Eltableout");    var eldiv = document.getElementById ("Eldiv");    onhtmltoexcel (ELTABLEOUT,ELDIV);  }     NOTE: Using JS to export Excel needs to set IE active. Note that the best is the IE8 browser. As for the implementation of Excel in the Java background has not been studied, I hope that the great God advice.    
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.