2010.03.01——html导出为word excel文档

2010.03.01——html导出为word excel文档
html

<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><input type="text" value="fasdfsdafasd"/></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('PrintA');" value="导出页面指定区域内容到Word">
<input type="button" onclick="javascript:AllAreaExcel('PrintA');" value="导出页面指定区域内容到Excel">
<input type="button" onclick="javascript:CellAreaExcel('PrintA');" value="导出表单单元格内容到Excel">
</body>

js

<HEAD>
<title>WEB页面导出为EXCEL文档的方法
</title>
<SCRIPT LANGUAGE="javascript">
//指定页面区域内容导入Excel
function AllAreaExcel(tid)
{
var excel = document.getElementById(tid);
var oXL = null;
try{
oXL = new ActiveXObject("Excel.Application");
}catch(e){
alert('ERROR!! 原因分析: 浏览器安全级别较高导致不能创建Word对象或者客户端没有安装Word软件');
idTmr = window.setInterval("Cleanup();",1);
return;
}
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel=document.body.createTextRange();
sel.moveToElementText(excel);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
idTmr = window.setInterval("Cleanup();",1);
}
//指定页面区域“单元格”内容导入Excel,只有文本 不带背景
function CellAreaExcel(tid)
{
var excel = document.getElementById(tid);
var oXL = null;
try{
oXL = new ActiveXObject("Excel.Application");
}catch(e){
alert('ERROR!! 原因分析: 浏览器安全级别较高导致不能创建Word对象或者客户端没有安装Word软件');
idTmr = window.setInterval("Cleanup();",1);
return;
}
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var Lenr = excel.rows.length;
for (i=0;i<Lenr;i++)
{
var Lenc = excel.rows(i).cells.length;
for (j=0;j<Lenc;j++)
{
oSheet.Cells(i+1,j+1).value = excel.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
idTmr = window.setInterval("Cleanup();",1);
}
//指定页面区域内容导入Word
function AllAreaWord(tid)
{
var word = document.getElementById(tid);
var oWD = null;
try{
oWD = new ActiveXObject('Word.Application');
}catch(e){
alert('ERROR!!原因分析: 浏览器安全级别较高导致不能创建Word对象或者客户端没有安装Word软件');
idTmr = window.setInterval("Cleanup();",1);
return;
}
var oDC = oWD.Documents.Add("",0,1);
var orange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(word);
sel.select();
sel.execCommand("Copy");
orange.Paste();
oWD.Application.Visible = true;
//oDC.saveAs(false,"ba.doc");
idTmr = window.setInterval("Cleanup();",1);
/*
var wordApp = null;
try{
wordApp = new ActiveXObject('Word.Application'); }catch(e)
{
alert(e+', 原因分析: 浏览器安全级别较高导致不能创建Word对象或者客户端没有安装Word软件');
return;
}
var oDC = wordApp.Documents.Add("",0,1);
wordApp.Application.Visible = false;
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange();
sel.moveToElementText(word);
sel.select();
sel.execCommand("Copy");
wordApp.Application.Visible = false;
oRange.Paste();
wordApp.visible = false;
wordApp.ActiveDocument.printout();
wordApp.Application.ActiveDocument.SaveAs("c:/tempSample.doc",true);
wordApp.ActiveDocument.close();
ActiveWindow.Close();
wordApp=null;
wordApp.Quit();

idTmr = window.setInterval("Cleanup();",1);

window.close();
*/
}
//解决上面几个方法execl word对象无法关闭的问题
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
</SCRIPT>

</HEAD>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值