html+js代码实现table导出Excel表格功能

1. table表格页面样式

在这里插入图片描述

2. 导出数据样式在这里插入图片描述

3.HTML页面代码

<table id="score" cellspacing="0" cellpadding="0" border="1" solid="#000000" width="200px"  height="40px"   text-align="center" >
   <tr>
       <th>姓名</th>
       <th>班级</th>
       <th>数学成绩</th>
       <th>历史成绩</th>
    </tr>
    <tr>
       <td>张三</td>
       <td>1</td>
       <td>65</td>
       <td>83</td>
     </tr>
     <tr>
        <td>李四</td>
        <td>1</td>
        <td>34</td>
        <td>89.5</td>
     </tr>
     <tr>
         <td>王五</td>
         <td>2</td>
         <td>90</td>
         <td>95</td>
      </tr>
      <tr>
          <td>赵六</td>
          <td>2</td>
          <td>76.3</td>
          <td>80.1</td>
      </tr>
 </table>
 <button id="export" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>

4. js代码

$('#export').click(function () {
        var excel="";
        excel+="<table>";
        var html=document.getElementById("score").innerHTML;
        excel+=html;
        excel+="</table>";
        var excelFile="<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+excel+"' xmlns='http://www.w3.org/TR/REC-html40'>";
        excelFile=excelFile+"<head><style type=\"text/css\">table td {border: 1px solid #000000;width: 200px;height: 30px text-align: center;}</style></head>"+excel+"</body></html>";
        var base64data="base64,"+window.btoa(unescape(encodeURIComponent(excelFile)));
        window.open('data:application/vnd.ms-excel;'+ base64data);
    });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值