js导出Excel

<html>
<body>
<script language="javascript">       
//导出excel
function exportExcel(DivID){

 //先声明Excel插件、Excel工作簿等对像
 var jXls, myWorkbook, myWorksheet;
 
 try {
  //插件初始化失败时作出提示
  jXls = new ActiveXObject('Excel.Application');
 }catch (e) {
  alert("无法启动Excel!\n\n如果您确信您的电脑中已经安装了Excel,"+"那么请调整IE的安全级别。\n\n具体操作:\n\n"+"工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");
  return false;
 }
 
 //不显示警告 
 jXls.DisplayAlerts = false;
 
 //创建AX对象excel
 myWorkbook = jXls.Workbooks.Add();
 //myWorkbook.Worksheets(3).Delete();//删除第3个标签页(可不做)
 //myWorkbook.Worksheets(2).Delete();//删除第2个标签页(可不做)
 
 //获取DOM对像
 var curTb = document.getElementById("tabEnterNpDiv");
 
 //获取当前活动的工作薄(即第一个)
 myWorksheet = myWorkbook.ActiveSheet; 
 
 //设置工作薄名称
 myWorksheet.name="NP统计";
 
 //获取BODY文本范围
 var sel = document.body.createTextRange();
 
 //将文本范围移动至DIV处
 sel.moveToElementText(curTb);
 
 //选中Range
 sel.select();
 
 //清空剪贴板
 window.clipboardData.setData('text','');
 
 //将文本范围的内容拷贝至剪贴板
 sel.execCommand("Copy");
 
 //将内容粘贴至工作簿
 myWorksheet.Paste();
 
 //打开工作簿
 jXls.Visible = true;
 
 //清空剪贴板
 window.clipboardData.setData('text','');
 jXls = null;//释放对像
 myWorkbook = null;//释放对像
 myWorksheet = null;//释放对像
}

</script>     
<input type="button" onClick="exportExcel('tabEnterNpDiv')" value="导出表格">
fgsghsh
<table width="200" border="1">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<div></div>
<div id="tabEnterNpDiv">
 <table border="1" style="display:block">
 <tr bgcolor="tomato">
  <td align="middle">省份</td>
  <td id = "province" colspan="2" align="middle">天津</td>
  <td colspan="4" align="middle">携入</td>
 </tr>
 <tr bgcolor="turquoise">
  <td rowspan="2" align="middle">时间</td>
  <td align="middle">开始时间</td>
  <td align="middle">结束时间</td>
  <td align="middle">电信</td>
  <td align="middle">移动</td>
  <td align="middle">联通</td>
  <td align="middle">携出合计</td>
 </tr>
 <tr bgcolor="mistyrose">
  <td id = "sDate" align="middle">2010-10-17</td>
  <td id = "eDate" align="middle">2010-11-17</td>
  <td align="middle">成功量</td>
  <td align="middle">成功量</td>
  <td align="middle">成功量</td>
  <td align="middle">成功量</td>
 </tr>
 <tr bgcolor="loralwhite">
  <td rowspan="4" align="middle">携出</td>
  <td colspan="2" align="middle">电信</td>
  <td align="middle">--</td>
  <td id = "s001002" align="middle">500</td>
  <td id = "s001003" align="middle">700</td>
  <td id = "out001" align="middle">1200</td>
 </tr>
 <tr bgcolor="indigo">
  <td colspan="2" align="middle">移动</td>
  <td id = "s002001" align="middle">3000</td>
  <td align="middle">--</td>
  <td id = "s002003" align="middle">2500</td>
  <td id = "out002" align="middle">5500</td>
 </tr>
 <tr bgcolor="Gray">
  <td colspan="2" align="middle">联通</td>
  <td id = "s003001" align="middle">3000</td>
  <td id = "s003002" align="middle">500</td>
  <td align="middle">--</td>
  <td id = "out003" align="middle">3500</td>
 </tr>
 <tr bgcolor="tan">
  <td colspan="2" align="middle">携入合计</td>
  <td id = "in001" align="middle">6000</td>
  <td id = "in002" align="middle">1000</td>
  <td id = "in003" align="middle">3200</td>
  <td id = "inout" align="middle">10200</td>
 </tr>
</table>
</div>
<div></div><table width="200" border="1">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

</body>
</html>


文章参考  http://blog.sina.com.cn/s/blog_531308390100vboj.html

你可能感兴趣的:(JavaScript,Excel)