网页内容导出word/excel的js代码
IE设置:
工具-> Internet选项-> 安全->自定义级别-> 对没有标记安全级别的ActiveX控件进行初始化
设为启用!
1.导出word
//指定区域导出到Word
function html2word(Area) {
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(document.getElementById(Area));
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
}
function html2word(Area) {
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(document.getElementById(Area));
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
}
Area:要导出的html元素ID。
2.导出Excel
//指定区域导出到Excel
function html2Excel(Area) {
try {
var oRangeRef = document.body.createTextRange();
oRangeRef.moveToElementText(document.getElementById(Area));
//oRangeRef.select();
oRangeRef.execCommand("Copy");
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add;
var oSheet = oWB.ActiveSheet;
oSheet.Paste();
oXL.Visible = true;
oSheet = null;
oWB = null;
oXL = null;
} catch (e) {
alert(e.description)
}
}
function html2Excel(Area) {
try {
var oRangeRef = document.body.createTextRange();
oRangeRef.moveToElementText(document.getElementById(Area));
//oRangeRef.select();
oRangeRef.execCommand("Copy");
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add;
var oSheet = oWB.ActiveSheet;
oSheet.Paste();
oXL.Visible = true;
oSheet = null;
oWB = null;
oXL = null;
} catch (e) {
alert(e.description)
}
}
Area:要导出的html元素ID。