DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JavaScript Web頁面內容導出到Word、Excel
JavaScript Web頁面內容導出到Word、Excel
編輯:關於JavaScript     

<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" value="導出頁面指定區域內容到Word">
<input type="button" value="導出頁面指定區域內容到Excel">
<input type="button" 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>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved