1. The most common method is to use js functions to operate excle files. This method may vary with browsers and may cause compatibility issues.
2. Another simple method is to export an xml file that can be recognized by excel and open it in excel.
Function getTableDataByXML (inTable, inWindow ){
Var rows = 0;
// Alert ("getTblData is" + inWindow );
Var tblDocument = document;
If (!! InWindow & inWindow! = ""){
If (! Document. all (inWindow )){
Return null;
}
Else {
TblDocument = eval(inWindow).doc ument;
}
}
Var inTbl = tblDocument. getElementById (inTable );
Var outStr = "<? Xml version = \ "1.0 \"?> \ N ";
OutStr = outStr + "<? Mso-application progid = \ "Excel. Sheet \"?> \ N ";
OutStr = outStr + "<Workbook xmlns = \" urn: schemas-microsoft-com: office: spreadsheet \"";
OutStr = outStr + "xmlns: o = \" urn: schemas-microsoft-com: office \"";
OutStr = outStr + "xmlns: x = \" urn: schemas-microsoft-com: office: excel \"";
OutStr = outStr + "xmlns: ss = \" urn: schemas-microsoft-com: office: spreadsheet \ "> \ n ";
OutStr = outStr + "<Worksheet ss: Name = \" Sheet1 \ "> \ n ";
OutStr = outStr + "<Table ss: ExpandedColumnCount = \" 30 \ "> \ n ";
Var re =/^ [0-9] + .? [0-9] * $/; // whether it is a number
If (inTbl! = Null ){
For (var j = 0; j <inTbl. rows. length; j ++ ){
OutStr + = "<Row ss: AutoFitHeight = \" 0 \ "> \ n ";
For (var I = 0; I <inTbl. rows [j]. cells. length; I ++ ){
If (I = 0 & rows> 0 ){
OutStr + = "<Cell> <Data ss: Type = \" String \ "> </Data> </Cell> \ n ";
Rows-= 1;
}
Var cellValue = inTbl. rows [j]. cells [I]. innerText;
// Number for a Number smaller than 12 digits
If (re. test (cellValue) & (new String (cellValue). length <11 ){
OutStr = outStr + "<Cell> <Data ss: Type = \" Number \ ">" + cellValue + "</Data> </Cell> \ n ";
} Else {
OutStr = outStr + "<Cell> <Data ss: Type = \" String \ ">" + cellValue + "</Data> </Cell> \ n ";
}
If (inTbl. rows [j]. cells [I]. colSpan> 1 ){
For (var k = 0; k <inTbl. rows [j]. cells [I]. colSpan-1; k ++ ){
OutStr + = "<Cell> <Data ss: Type = \" String \ "> </Data> </Cell> \ n ";
}
}
If (I = 0 ){
If (rows = 0 & inTbl. rows [j]. cells [I]. rowSpan> 1 ){
Rows = inTbl. rows [j]. cells [I]. rowSpan-1;
}
}
}
OutStr + = "</Row> \ n ";
}
}
Else {
OutStr = null;
Alert ("the table you want to export does not exist !! ");
Return;
}
OutStr = outStr + "</Table> \ n </Worksheet> \ n </Workbook> ";
Return outStr;
}
The above functions are originally used to export txt files. Save an excel file as an xml file to get an xml file in which excel can recognize the content format.
Author: "ios2007"