js table 导出 Excel

function ExportExcel(tableid) {
    var curTbl = document.getElementById(tableid);
    var oXL = new ActiveXObject("Excel.Application");
    var oWB = oXL.Workbooks.Add();
    var oSheet = oWB.ActiveSheet;
    var sel = document.body.createTextRange();
    sel.moveToElementText(curTbl);
    sel.select();
    sel.execCommand("Copy");
    oSheet.Paste();
    //oXL.Visible = true;

    var now = new Date();
    var nowStr = now.format("yyyyMMddhhmmss"); 

    try {
        var fname = oXL.Application.GetSaveAsFilename(nowStr + ".xls", "Excel Spreadsheets (*.xls), *.xls");
        if (fname) {
            oWB.SaveAs(fname);
            oWB.Close(savechanges = false);
            oXL.Quit();
            oXL = null;
            //结束excel进程,退出完成  
            idTmr = window.setInterval("Cleanup();", 1);
        }
        else {
            oXL.Visible = true;
        }
    } catch (e) {
        print("Nested catch caught " + e);
    } finally {

    }
}
function Cleanup() {
    window.clearInterval(idTmr);

    CollectGarbage();
}  

 

posted @ 2013-04-09 10:01  小さいです哥  阅读(313)  评论(0编辑  收藏  举报