Dzwebs.Net

撰写电脑技术杂文十余年

js将表格导出为word/excel文件的代码

Admin | 2009-6-8 11:27:25 | 被阅次数 | 11403

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

<table width="100%" border="1" id = "PrintA">
<tr>
    <td>dfsdfds</td>
    <td>&dsfds</td>
    <td>&sdf;</td>
    <td>dsf</td>
</tr>
<tr>
    <td>dsf</td>
    <td>dsfds</td>
    <td>fsdfdsfdsf</td>
    <td>dsfs</td>
</tr>
<tr>
    <td>dsfds</td>
    <td>dsf</td>
    <td>dsfd</td>
    <td>sfds</td>
</tr>
<tr>
    <td>&nbsp;</td>
    <td>sdf</td>
    <td>fdsf</td>
    <td>&nbsp;</td>
</tr>
</table>


<input type="button" onclick="javascript:AllAreaWord();" value="导出页面指定区域内容到Word">

<input type="button" onclick="javascript:AllAreaExcel();" value="导出页面指定区域内容到Excel">

<input type="button" onclick="javascript:CellAreaExcel();" value="导出表单单元格内容到Excel">

<SCRIPT LANGUAGE="javascript">

//指定页面区域内容导入Excel

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;

}

//指定页面区域“单元格”内容导入Excel

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;

}

//指定页面区域内容导入Word

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;

//window.close();

oWD.ActiveWindow.ActivePane.View.Type=3

oWD.Application.close;

}

</SCRIPT>

</body></html>


该杂文来自: 网页制作杂文

上一篇:站长投放网络广告的技巧

下一篇:必须按照顺序才能选择控件进行操作的JS代码

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计