In a recent project, you need to import the report table to excel. You have found some methods on the Internet and made a comparative study. Remember here and forget it.
An example is as follows:
<Table id = "tableexcel" width = "100%" border = "1" cellspacing = "0" cellpadding = "0"> <tr> <TD colspan = "5" align = ""center"> HTML table Export Channel Excel </TD> </tr> <TD> Column Title 1 </TD> <TD> Column Title 2 </TD> <TD> class title 3 </TD> <TD> Column Title 4 </TD> <TD> Column Title 5 </TD> </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> <TD> fff </TD> <TD> ggg </TD> <TD> hhh </TD> <TD> III </TD> <TD> jjj </TD> </tr> </table>
1. js method
A. Copy the entire table to 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 unit in the table to 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 save it in CVS format.
function getXlsFromTbl(inTblId, inWindow)
{Try {var allstr = ""; var curstr = ""; if (intblid! = NULL & intblid! = "" & Intblid! = "Null") {curstr = gettbldata (intblid, inwindow);} 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:" + 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).doc ument ;}} 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. rows [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]. rowspan> 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. l Ength = 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 + ". CSV "; 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-height/2) + ", scrollbars = No, width =" + width + ", Height =" + height; xlswin = Window. open ("", "_ blank", openpara);} xlswin.doc ument. write (instr); xlswin.doc ument. close (); xlswin.document.exe ccommand ('saveas', true, inname); xlswin. close ();}
Conclusion: Comparing the above three methods, I feel that the first method is more perfect, because this method is more complete in the output table format. However, ActiveX objects are used in methods 1 and 2, which have requirements on Client Security and the biggest problem is that Excel objects cannot be closed. The method in 3rd does not use ActiveX objects, but uses pop-up window output. If the pop-up window is disabled, it cannot be used.
For the problem that the execl object cannot be closed, the following method is an appropriate 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 method in JS 3rd above (it can also be implemented in other web scripts, such as VBScript in ASP or PHP), and the table is used as a file stream.
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(); }
In essence, this method is not a standard Excel format. However, you can save an HTML file as an Excel file and then open it in Excel.
3. ExploitationExcel application, msowc, or ado.net
This method is implemented by using server components and requires the server to install Excel. For specific code, see the following link:
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 method because the server resources need to be occupied.