JavaScript直接导出Excel,Word(及错误处理)
|
admin
2010年8月18日 1:9
本文热度 3778
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>WEB页面导出为EXCEL文档的方法</title>
</head>
<body>
<BR>
<table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13">
<TR style="text-align : center;">
<TD>单元格A</TD>
<TD>单元格A</TD>
<TD>单元格A</TD>
<TD>单元格A</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">单元格合并行A</FONT></TD>
</TR>
</TABLE>
<BR>
<table id = "PrintB" width="100%" border="1" cellspacing="0" cellpadding="0">
<TR style="text-align : center;">
<TD>单元格B</TD>
<TD>单元格B</TD>
<TD>单元格B</TD>
<TD>单元格B</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;">单元格合并行B</TD>
</TR>
</TABLE>
<br>
<br>
<br>
<input type="button" onClick="javascript:AllAreaWord();" value="导出页面指定区域内容到Word">
<input type="button" onClick="javascript:CellAreaExcel();" value="导出页面指定区域内容到Excel">
<input type="button" onClick="javascript:AllAreaExcel2();" value="导出表单单元格内容到Excel">
<SCRIPT LANGUAGE="javascript">
//----------------------------------------------------------------
//------------ 随即函数
function genRandNumber(startNum,endNum){
var randomNumber;
randomNumber = Math.round(Math.random()*(endNum-startNum))+startNum;
return randomNumber;
}
//----------------------------------------
var now= new Date();
var year=now.getYear();
var month=now.getMonth()+1;
var day=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
var oHtmlNow = year+''+month+''+day+''+hour+''+minute+''+second+''+genRandNumber(999999,111111);
//----------------------------------------------------------------
function AllAreaExcel() {
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel = document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true
}
function AllAreaExcel2() {
window.clipboardData.setData("Text", document.all('PrintA').outerHTML);
try {
var ExApp = new ActiveXObject("Excel.Application");
var ExWBk = ExApp.workbooks.add();
var ExWSh = ExWBk.worksheets(1);
ExApp.DisplayAlerts = false;
ExApp.visible = true;
} catch(e) {
alert("您的电脑没有安装Microsoft Excel软件!");return false;
}
ExWBk.worksheets(1).Paste;
ExApp.Visible = true;
//设置excel可见属性
try{
var fname = ExApp.Application.GetSaveAsFilename(oHtmlNow+".xls", "Excel Spreadsheets (*.xls), *.xls");
if(fname){
ExWBk.SaveAs(fname);
}
}catch(e){
print("Nested catch caught " + e);
}finally{
ExWBk.Close(savechanges=false);
ExApp.Quit();
ExApp=null;
//结束excel进程,退出完成
alert('已经成功导入到Excel:'+oHtmlNow+'.xls');
idTmr = window.setInterval("Cleanup();",1);
}
}
function CellAreaExcel() {
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var Lenr = PrintA.rows.length;
for (i = 0; i < Lenr; i++) {
var Lenc = PrintA.rows(i).cells.length;
for (j = 0; j < Lenc; j++) {
oSheet.Cells(i + 1, j + 1).value = PrintA.rows(i).cells(j).innerText
}
}
oXL.Visible = true;
}
function AllAreaWord() {
var oWD = new ActiveXObject("Word.Application");
var oDC = oWD.Documents.Add("", 0, 1);
var oRange = oDC.Range(0, 1);
var sel = document.body.createTextRange();
sel.moveToElementText(PrintA);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true
}
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
//-----------------------------------------------------------------
</script>
</body>
</html>
在线导出Excel,错误信息:Automation服务器不能创建对象解决方案
问题描述:
在部分以IE为内核的浏览器下,按钮“导出为Excel”不起作用,错误信息为Automation服务器不能创建对象;
官方(微软)解释:
http://support.microsoft.com/kb/828550/zh-cn
解决方案:
- 单击 IE 菜单:工具;
- 选择 Internet选项;
- 切换为 安全选项卡;
- 单击自定义级别按钮;
- 更改 对未标记为可安全执行脚本的ActiveX控件初始化并执行 为 提示或者 启用 ;
- 单击确定,重启浏览器即可。
该文章在 2010/8/18 1:09:28 编辑过