js 导出到word,excel itprobie

1>js导出,原理:复制页面中的table,让后粘贴到excle中

 1   function exportToWord(id) {
 2             //Scripting.FileSystemObject (FSO 文本文件读写)被关闭了,
 3             //开启FSO功能即可,在“运行”中执行regsvr32 scrrun.dll即可
 4             try {
 5                 var oElement = document.getElementById(id);
 6                 var word = new ActiveXObject("Word.Application");
 7                 var doc = word.Documents.Add("", 0, 1); //不打开模版直接加入内容
 8                 var Range = doc.Range();
 9 
10                 var sel = document.body.createTextRange();
11                 sel.moveToElementText(oElement);
12                 sel.select();
13                 sel.execCommand("Copy");
14                 Range.Paste();
15                 word.Application.Visible = true;
16             }
17             catch (e) {
18 
19                 alert("无法启动Excel!\n\n" + e.message +
20                 "\n\n如果您确信您的电脑中已经安装了Excel," +
21                 "那么请调整IE的安全级别。\n\n具体操作:\n\n" +
22                 "工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");
23             }
24         }
25         function exportToExcel(id) {
26             //Scripting.FileSystemObject (FSO 文本文件读写)被关闭了,
27             //开启FSO功能即可,在“运行”中执行regsvr32 scrrun.dll即可
28             try {
29                 var oElement = document.getElementById(id);
30                 var oRangeRef = document.body.createTextRange();
31                 oRangeRef.moveToElementText(oElement);
32                 oRangeRef.execCommand("Copy");
33 
34                 var oXL = new ActiveXObject("Excel.Application")
35                 var oWB = oXL.Workbooks.Add;
36                 var oSheet = oWB.ActiveSheet;
37                 oSheet.Paste();
38                 oSheet.Cells.NumberFormatLocal = "@";
39                 oXL.Selection.ColumnWidth = 8;
40 
41                 oXL.Visible = true;
42                 oSheet = null;
43                 oWB = null;
44                 appExcel = null;
45             } catch (e) {
46                 alert("无法启动Excel!\n\n" + e.message +
47                 "\n\n如果您确信您的电脑中已经安装了Excel," +
48                 "那么请调整IE的安全级别。\n\n具体操作:\n\n" +
49                 "工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");
50             }

 程序员的基础教程:菜鸟程序员

原文地址:https://www.cnblogs.com/guohu/p/3090243.html