The most common way is to use the JS function to manipulate excle files, which may have compatibility problems due to different browsers. Another easy way to do this is to export an XML file that Excel can recognize in a format and open it in Excel.
Copy Code code as follows:
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). Document;
}
}
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: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=" ">n";
var re =/^[0-9]+.? [0-9]*$/; is a digital
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;
//Less than 12 digits with number
if (re.test (Cellvalue) && (new String (cellvalue). Length < 11) {
outstr = outstr + "<cell><data ss:type=" number ">" + cellvalue + "</data></c Ell>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;
}
This function is originally a function of exporting TXT file. By saving an Excel file as an XML file, you get an XML file in which Excel can identify the content format.