javascript操作并打开Excel实例代码

发布时间:2019-11-13编辑:脚本学堂
分享几个javascript操作并打开excel文件的代码,有关javascript操作excel文件的方法,需要的朋友参考下。

例子,javascript脚本操作打开excel文件。
 

复制代码 代码示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<TITLE>top30销售业绩表--www.jb200.com</TITLE>
</HEAD>
<BODY>
<input name="shanghai" type="button" class="but" id="shanghai" onClick="StartExcel('上海')" value="上海店">
<SCRIPT LANGUAGE="Javascript">
var idTmr = "";
function StartExcel(strFileName)
{
var oExcel;
var oWorkbook;
var strAllName;
oExcel = new ActiveXObject("Excel.Application");
//if(strFileName=null || strFileName="")
//{return;
//}
strAllName="\ep-hghTop30销控表TOP30招商续约销控表-"+strFileName+".xls";
oExcel.Workbooks.Open(strAllName);
//oExcel.Workbooks.Open("file://ep-hgh/Top30销控表/TOP30招商续约销控表-上海.xls");
oExcel.DisplayAlerts = false;
oExcel.Visible = true;
//oExcel.Quit();
//oExcel = null;
//防EExcel死进程的关键!!必须!!!downmoon严重声明
idTmr = window.setInterval("Cleanup();",1000);
}

function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
</SCRIPT>
</BODY>
</HTML>

例2,用javascript将数据导入excel
 

复制代码 代码示例:

<input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint">

导出到Excel代码:
<SCRIPT LANGUAGE="javascript">
<!--
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = document.all.data;
var hang = table.rows.length;

var lie = table.rows(0).cells.length;

// Add table headers going cell by cell.
for (i=0;i<hang;i++)
{
for (j=0;j<lie;j++)
{
//oSheet.Cells(i+1,j+1).Font.Bold = True
//oSheet.Cells(i+1,j+1).Font.Size = 50
//oSheet.Cells(i+1,j+1).Alignment = 2
oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;
}

}
oXL.Visible = true;
oXL.UserControl = true;
}
//-->
</SCRIPT>
<table border="1" width="100%" id="data">
 <tr>
  <td>&nbsp;姓名</td>
  <td colspan="2">年龄</td>
  <td>出生日期</td>
 </tr>
 <tr>
  <td>6</td>
  <td>25</td>
  <td colspan="2">8</td>
 </tr>
 <tr>
  <td height="22">1</td>
  <td height="22">2</td>
  <td colspan="2" height="22">4</td>
 </tr>
</table>