網頁內容導出word/excel的js代碼

IE設置:html

 工具-> 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;
}

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)
    }
}

Area:要導出的html元素ID。spa

 

轉載:http://www.cnblogs.com/sobne/articles/2103999.htmlcode

相關文章
相關標籤/搜索