JavaScript利用ActiveX匯出Excel,Word
來源:互聯網
上載者:User
<HTML> <HEAD> <title>WEB頁面匯出為EXCEL文檔的方法 </title> </HEAD><body><BR><table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13"><TR style="text-align : center;"> <TD>儲存格A</TD> <TD>儲存格A</TD> <TD>儲存格A</TD> <TD>儲存格A</TD></TR><TR> <TD colSpan=4 style="text-align : center;"><font color="BLUE" face="Verdana">儲存格合并行A</FONT></TD></TR></TABLE><BR><table id = "PrintB" width="100%" border="1" cellspacing="0" cellpadding="0"><TR style="text-align : center;"> <TD>儲存格B</TD> <TD>儲存格B</TD> <TD>儲存格B</TD> <TD>儲存格B</TD></TR><TR> <TD colSpan=4 style="text-align : center;">儲存格合并行B</TD></TR></TABLE><br><br><br><input type="button" onclick="javascript:AllAreaWord();" value="匯出頁面指定地區內容到Word"><input type="button" onclick="javascript:AllAreaExcel();" value="匯出頁面指定地區內容到Excel"><input type="button" onclick="javascript:CellAreaExcel();" value="匯出表單儲存格內容到Excel"><SCRIPT LANGUAGE="javascript"> //指定頁面地區內容匯入Excel function AllAreaExcel() { var oXL = new ActiveXObject("Excel.Application"); var oWB = oXL.Workbooks.Add(); var oSheet = oWB.ActiveSheet; var sel=document.body.createTextRange(); sel.moveToElementText(PrintA); sel.select(); sel.execCommand("Copy"); oSheet.Paste(); oXL.Visible = true; } //指定頁面地區“儲存格”內容匯入Excel function CellAreaExcel() { var oXL = new ActiveXObject("Excel.Application"); var oWB = oXL.Workbooks.Add(); var oSheet = oWB.ActiveSheet; var Lenr = PrintA.rows.length; for (i=0;i<Lenr;i++) { var Lenc = PrintA.rows(i).cells.length; for (j=0;j<Lenc;j++) { oSheet.Cells(i+1,j+1).value = PrintA.rows(i).cells(j).innerText; } } oXL.Visible = true; } //指定頁面地區內容匯入Word function AllAreaWord() { 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(PrintA); sel.select(); sel.execCommand("Copy"); oRange.Paste(); oWD.Application.Visible = true; //window.close(); }</SCRIPT></body></html> Code function preview() { window.clipboardData.setData("Text",document.all('AreaPrint').outerHTML); try { var ExApp = new ActiveXObject("Excel.Application") var ExWBk = ExApp.workbooks.add() var ExWSh = ExWBk.worksheets(1) ExApp.DisplayAlerts = false ExApp.visible = true } catch(e) { alert("您的電腦沒有安裝Microsoft Excel軟體!") return false } ExWBk.worksheets(1).Paste; }