JavaScript將Table匯出到Excel實現思路及代碼

來源:互聯網
上載者:User

複製代碼 代碼如下:function copyToExcel(tableid) {
//控制按鈕
var btn = document.getElementById("copy");
btn.setAttribute("disabled", "true");
btn.setAttribute("value", "處理中...");

var curTbl = document.getElementById(tableid);
try {
var oXL = new ActiveXObject("Excel.Application");
}
catch (e) {//IE安全層級未設定將出現錯誤 ( Automation 伺服器不能建立對象 )
/*
如果是Scripting.FileSystemObject (FSO 文字檔讀寫)被關閉了,開啟FSO功能即可,在“運行”中執行regsvr32 scrrun.dll即可
*/
alert("無法啟動Excel!\n\n如果您確信您的電腦中已經安裝了Excel," + "那麼請調整IE的安全層級。\n\n具體操作:\n\n" + "工具 → Internet選項 → 安全 → 自訂層級 → 對沒有標記為安全的ActiveX進行初始化和指令碼運行 → 啟用");
return false;
}
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 fname = oXL.Application.GetSaveAsFilename("將table匯出到excel.xls", "Excel Spreadsheets (*.xls), *.xls");
oWB.SaveAs(fname);
oWB.Close();
oXL.Quit();
//控制按鈕
btn.removeAttribute("disabled");
btn.setAttribute("value", "匯出結果到Excel");
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.