Reprint: http://www.cnblogs.com/liuguanghai/archive/2012/12/31/2840262.html
<table id= "Tableexcel" width= "100%" border= "1" cellspacing= "0" cellpadding= "0" > <tr> <td colspan= "5" align= "Center" >html Table Export channel excel</td> </tr> <tr> <td> column headings 1</ td> <td> column headings 2</td> <td> class headings 3</td> <td> column headings 4</td> <td > column headings 5</td> </tr> <tr> <td>aaa</td> <td>bbb</td > <td>ccc</td> <td>ddd</td> <td>eee</td> </tr > <tr> <td>AAA</td> <td>BBB</td> <td>CCC</td> <td>DDD</td> <td>EEE</td> </tr> <tr> <td> fff</td> <td>GGG</td> <td>HHH</td> <td>III</td> <td>JJJ</td> </tr></table>
1, the method of JS
A. Copy the entire table into Excel
function Method1 (tableid) { var curtbl = document.getElementById (tableid); var OXL = new ActiveXObject ("Excel.Application"); var owb = OXL.Workbooks.Add (); var osheet = Owb.activesheet; var sel = Document.body.createTextRange (); Sel.movetoelementtext (CURTBL); Sel.select (); Sel.execcommand ("Copy"); Osheet.paste (); oXL.Visible = true;}
B. Read each cell in the table into Excel:
function Method2 (tableid) { var curtbl = document.getElementById (tableid); var OXL = new ActiveXObject ("Excel.Application"); var owb = OXL.Workbooks.Add (); var osheet = Owb.activesheet; var lenr = curTbl.rows.length; for (i = 0; i < lenr; i++) {
var Lenc = curtbl.rows (i). Cells.length; for (j = 0; J < Lenc; J + +) { Osheet.cells (i + 1, j + 1). Value = Curtbl.rows (i). Cells (j). InnerText; } } oxl.visible = true;}
C, output the table to another page, and then save it in CVS format
function Getxlsfromtbl (intblid, Inwindow)
{try {var allstr = ""; var curstr = ""; if (intblid! = NULL && Intblid! = "" && intblid! = "null") {CURSTR = Gettbldata (Intblid, Inwin Dow); } if (curstr! = null) {allstr + = Curstr; } else {alert ("The table you want to export does not exist"); Return } var fileName = Getexcelfilename (); Dofileexport (FileName, ALLSTR); } catch (e) {alert ("Export exception occurred:" + e.name + "+" + e.description + "!");}} function Gettbldata (INTBL, Inwindow) {var rows = 0; var tbldocument = document; if (!! Inwindow && Inwindow! = "") {if (!document.all (Inwindow)) {return null; } else {tbldocument = eval (inwindow). Document; }} var curtbl = Tbldocument.getelementbyid (INTBL); var outstr = ""; if (curtbl! = null) {for (var j = 0; J < CurTbl.rows.length; J + +) {for (var i = 0; i < curtbl.ro Ws[j].cElls.length; i++) {if (i = = 0 && rows > 0) {outstr + = "\ T"; Rows-= 1; } outstr + = Curtbl.rows[j].cells[i].innertext + "\ T"; if (Curtbl.rows[j].cells[i].colspan > 1) {for (var k = 0; k < Curtbl.rows[j].cells[i].colspan- 1; k++) {outstr + = "\ T"; }} if (i = = 0) {if (rows = = 0 && Curtbl.rows[j].cells[i].rowspa n > 1) {rows = curtbl.rows[j].cells[i].rowspan-1; }}} outstr + = "\ r \ n"; }} else {outstr = null; Alert (intbl + "does not exist!"); } return outstr;} function Getexcelfilename () {var d = new Date (); var curyear = D.getyear (); var curmonth = "" + (D.getmonth () + 1); var curdate = "" + d.getdate (); var curhour = "" + D.gethouRS (); var Curminute = "" + d.getminutes (); var Cursecond = "" + d.getseconds (); if (curmonth.length = = 1) {Curmonth = "0" + curmonth; } if (curdate.length = = 1) {curdate = "0" + curdate; } if (curhour.length = = 1) {Curhour = "0" + curhour; } if (curminute.length = = 1) {Curminute = "0" + curminute; } if (cursecond.length = = 1) {Cursecond = "0" + cursecond; } var fileName = "Table" + "_" + curyear + curmonth + curdate + "_" + Curhour + curminute + cursecond + ". CS V "; return fileName;} function Dofileexport (inname, inStr) {var xlswin = null; if (!! document.all ("Glbhidefrm")) {Xlswin = glbhidefrm; } else {var width = 6; var height = 4; var Openpara = "left=" + (WINDOW.SCREEN.WIDTH/2-WIDTH/2) + ", top=" + (WINDOW.SCREEN.HEIGHT/2-Heig HT/2) + ", scrollbars=no,width=" + width + ", height=" + height; Xlswin = WINDOW.OPen ("", "_blank", Openpara); } xlsWin.document.write (INSTR); XlsWin.document.close (); XlsWin.document.execCommand (' Saveas ', true, inname); Xlswin.close ();}
Summary: Compare the above 3 methods, feeling the first method is more perfect, because this method compares the format of the complete output table. However, the first and second methods use ActiveX objects, which are required for client security, and one of the biggest problems is that Excel objects cannot be closed. The 3rd method, although not using an ActiveX object, uses a pop-up window output and cannot be used if a pop-up window is disabled.
For an issue where the Execl object cannot be closed, the following method is an expedient method:
function Cleanup () { window.clearinterval (IDTMR); CollectGarbage (); }
Call Method:
IDTMR = Window.setinterval ("Cleanup ();", 1);
2. Methods in ASP. NET (C #)
This method is similar to the above JS of the 3rd Chinese law (can also be implemented in other Web scripts, such as the ASP in VBScript, or PHP), the form of the file stream way
The output is Excel. The instance code is as follows:
public void Outputexcel (string title) { response.clear (); Response.Buffer = true; Response.Charset = "Utf-8"; Response.AddHeader ("Content-disposition", "attachment;filename=" + httputility.urlencode (title + ". xls")); response.contentencoding = System.Text.Encoding.GetEncoding ("Utf-8"); Response.ContentType = "Application/ms-excel"; Page.enableviewstate = false; System.IO.StringWriter ostringwriter = new System.IO.StringWriter (); System.Web.UI.HtmlTextWriter ohtmltextwriter = new System.Web.UI.HtmlTextWriter (ostringwriter); This. Page.rendercontrol (ohtmltextwriter); String temp = Ostringwriter.tostring (); Response.Write (temp); Response.End (); }
This method is essentially not the standard Excel format, but the HTML format file is saved as Excel format, and then opened with Excel.
3. Use ExceL application or MSOWC or ADO
This method is implemented using server components, requires the service side to install Excel, the specific code can see the following links:
Http://www.cnblogs.com/pucumt/archive/2006/09/13/503120.html
Http://support.microsoft.com/default.aspx?scid=kb;zh-cn;306023#top
I do not advocate this approach because of the need to occupy the resources of the server.
HTML page Table Export to Excel summary