Excel files are frequently encountered in the office. In addition to using php to generate excel files, we can also use js to export page tables into excel files, we sorted out some examples for you to come and have a look.
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:
The Code is as follows: |
Copy code |
<Table id = "tableExcel" width = "100%" border = "1" cellspacing = "0" cellpadding = "0"> <Tr> <Td colspan = "5" align = "center"> html table export Excel </td> </Tr> <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> <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. js method
A. Copy the entire table to EXCEL.
The Code is as follows: |
Copy code |
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.exe cCommand ("Copy "); OSheet. Paste (); OXL. Visible = true; } |
B. read each unit in the table to EXCEL:
The Code is as follows: |
Copy code |
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.
The Code is as follows: |
Copy code |
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 + = "rn "; } } 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 + ". 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:
The Code is as follows: |
Copy code |
Function Cleanup (){ Window. clearInterval (idTmr ); CollectGarbage (); } |
Call method:
The Code is as follows: |
Copy code |
IdTmr = window. setInterval ("Cleanup ();", 1 ); |
2. 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 or php in asp ), output the table as an excel file stream. The instance code is as follows: public void OutPutExcel (string title)
The Code is as follows: |
Copy code |
{ 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. Use ExceL Application, MSOWC, or ado.net