Using js to export tables to excel, Baidu can search for many methods, but its compatibility is quite poor. This article develops a method that can be compatible with FF and IE at the same time, if you are interested, refer
The Code is as follows:
Foreground call (the first parameter is the table id ):
Function toExcel (inTblId, inWindow ){
If ($. browser. msie) {// if it is an IE browser
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 + "! ");
}
}
Else {
Window. open ('data: application/vnd. ms-excel, '+ encodeuricomponent(('p?id=*pgvdata='{.html ()));
E. preventDefault ();
}
}
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 );
If (curTbl. rows. length & gt; 65000 ){
Alert ('the number of source lines cannot exceed 65000 rows ');
Return false;
}
If (curTbl. rows. length <= 1 ){
Alert ('no data from the data source ');
Return false;
}
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;
}
Var tc = curTbl. rows [j]. cells [I];
If (j> 0 & tc. hasChildNodes () & tc. firstChild. nodeName. toLowerCase () = "input "){
If (tc. firstChild. type. toLowerCase () = "checkbox "){
If (tc. firstChild. checked = true ){
OutStr + = "yes" + "\ t ";
}
Else {
OutStr + = "no" + "\ t ";
}
}
}
Else {
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. length = 1 ){
CurHour = "0" + curHour;
}
If (curMinute. length = 1 ){
CurMinute = "0" + curMinute;
}
If (curSecond. length = 1 ){
CurSecond = "0" + curSecond;
}
Var fileName = "device status" + curYear + curMonth + curDate + curHour + curMinute + curSecond + ". xls ";
Return fileName;
}
Function doFileExport (inName, inStr ){
Var xlsWin = null;
If (!! Document. all ("glbHideFrm ")){
XlsWin = glbHideFrm;
}
Else {
Var width = 1;
Var height = 1;
Var openPara = "left =" + (window. screen. width/2 + width/2)
+ ", Top =" + (window. screen. height + 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 ();
}