javascript

位置:IT落伍者 >> javascript >> 浏览文章

js导出格式化的excel 实例方法


发布日期:2018年07月10日
 
js导出格式化的excel 实例方法
最常见的方式是使用js函数操作excle文件这种方式可能因浏览器不同存在兼容性问题另一种简单方法就是导出一个excel能识别格式的xml文件用excel打开复制代码 代码如下:
function getTableDataByXML(inTable inWindow) {
var rows = ;
//alert("getTblData is " + inWindow);
var tblDocument = document;
if (!!inWindow && inWindow != "") {
if (!documentall(inWindow)) {
return null;
}
else {
tblDocument = eval(inWindow)document;
}
}
var inTbl = tblDocumentgetElementById(inTable);
var outStr = "<?xml version=""?>n";
outStr = outStr + "<?msoapplication progid="ExcelSheet"?>n";
outStr = outStr + "<Workbook xmlns="urn:schemasmicrosoftcom:office:spreadsheet"";
outStr = outStr + " xmlns:o="urn:schemasmicrosoftcom:office:office"";
outStr = outStr + " xmlns:x="urn:schemasmicrosoftcom:office:excel"";
outStr = outStr + " xmlns:ss="urn:schemasmicrosoftcom:office:spreadsheet">n";
outStr = outStr + "<Worksheet ss:Name="Sheet">n";
outStr = outStr + "<Table ss:ExpandedColumnCount="">n";
var re = /^[]+?[]*$/; //是否为数字
if (inTbl != null) {
for (var j = ; j < inTblrowslength; j++) {
outStr += "<Row ss:AutoFitHeight="">n";
for (var i = ; i < inTblrows[j]cellslength; i++) {
if (i == && rows > ) {
outStr += "<Cell><Data ss:Type="String"></Data></Cell>n";
rows = ;
}
var cellValue = inTblrows[j]cells[i]innerText;
//小于位数字用Number
if(retest(cellValue) && (new String(cellValue))length < ){
  outStr = outStr + "<Cell><Data ss:Type="Number">" +  cellValue + "</Data></Cell>n";
}else{
  outStr = outStr + "<Cell><Data ss:Type="String">" +  cellValue + "</Data></Cell>n";
}
if (inTblrows[j]cells[i]colSpan > ) {
for (var k = ; k < inTblrows[j]cells[i]colSpan ; k++) {
outStr += " <Cell><Data ss:Type="String"></Data></Cell>n";
}
}
if (i == ) {
if (rows == && inTblrows[j]cells[i]rowSpan > ) {
rows = inTblrows[j]cells[i]rowSpan ;
}
}
}
outStr += "</Row>n";
}
}
else {
outStr = null;
alert("你要导出的表不存在!!");
return;
}
outStr = outStr + "</Table>n</Worksheet>n</Workbook>";
return outStr;
}


上述函数原本是导出txt文件的函数把excel文件另存为一个xml文件就可得到excel能识别什么内容格式的xml文件

               

上一篇:JavaScript:new 一个函数和直接调用函数的区别分析

下一篇:JavaScript定时器详解及实例