一。导出Excel有两种:
1.
function ExportExcel(tableid){//读取表格中每个单元到EXCEL中
try
{
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application"); //创建AX对象excel
var oWB = oXL.Workbooks.Add(); //获取workbook对象
var oSheet = oWB.ActiveSheet; //激活当前sheet
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; //设置excel可见属性
}
catch(e)
{
alert(e.message);
}
}
2:
function dataToExcel(tableid) {//整个表格拷贝到EXCEL中
var curTbl = document.getElementById(tableid);
var oXL;
try {
oXL = GetObject("", "Excel.Application");
}
catch (E) {
try {
oXL = new ActiveXObject("Excel.Application");
}
catch (E2) {
//alert("Please confirm:\n1.Microsoft Excel has been installed.\n2.Internet Options=>Security=>Setting \"Enable unsafe ActiveX\"");
alert("请确认:\n1.机器上Excel已经安装.\n2.Internet 选项=>安全=>Internet \"对没有标记为安全的ActiveX控件进行初始化和脚本运行,设定为启用\"");
return;
}
}
//创建AX对象excel
var oWB = oXL.Workbooks.Add();
//获取workbook对象
var oSheet = oWB.ActiveSheet;
//激活当前sheet
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
//把表格中的内容移到TextRange中
sel.select();
//全选TextRange中内容
sel.execCommand("Copy");
//复制TextRange中内容
oSheet.Paste();
//粘贴到活动的EXCEL中
oXL.Visible = true;
//设置excel可见属性
}
二。导出Txt
function dataToTxt(tableid)
{
var file_name=window.prompt("请指定输出文件名称(.txt)","C://ExportTxt.txt");
if(file_name!=null)
{
var curTbl = document.getElementById(tableid);
file_name=file_name.split("//").join("////");
alert(file_name);
var FSO=new ActiveXObject("Scripting.FileSystemObject");
var f1 = FSO.CreateTextFile(file_name, true);
var Lenr = curTbl.rows.length; //取得表格行数
for (i = 0; i < Lenr; i++){
var Lenc = curTbl.rows(i).cells.length; //取得每行的列数
for (j = 0; j < Lenc; j++){
f1.write(curTbl.rows(i).cells(j).innerText+" "); //赋值
}
f1.write("\r\n");
}
f1.close();
}
}
网友评论