技术干货编程语言爱好者程序员

js导出word和excel

2017-07-25  本文已影响0人  每木传情

有些时候,我们想把网页上的一些数据下载下来,我们想要把它们用word和excel的方式保存,但是总是找不到合适的方法,最近我也遇到了这样的问题,在网上搜罗一圈下来,也倒是有很多这方面的文章,但是主要是适用于IE浏览器的,对于其他浏览器几乎都是用的插件,我就想用js自己写一个可以导出文件的页面。在搜寻了一些资料后,自己写了一个比较简洁的。
一般情况下,我们导出的内容为表格数据居多,所以我们就以表格为例,导出页面内容

html页面内容

页面就一个基本的表格,和两个按钮,分别用于导出word和excel

   <body>
   <table id="score">
      <tr>
        <th>Name</th>
        <th>Grade</th>
        <th>Math</th>
        <th>History</th>
      </tr>
      <tr>
        <td>Like</td>
        <td>1</td>
        <td>65</td>
        <td>83</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>1</td>
        <td>34</td>
        <td>89.5</td>
      </tr>
      <tr>
        <td>Lily</td>
        <td>2</td>
        <td>90</td>
        <td>95</td>
      </tr>
      <tr>
        <td>BOb</td>
        <td>2</td>
        <td>76.3</td>
        <td>80.1</td>
      </tr>
      <tr>
        <td>Sendy</td>
        <td>3</td>
        <td>60</td>
        <td>79</td>
      </tr>
  </table>
     <input id="Button1" type="button" value="导出word" onclick="tableExport('doc')" />
     <input id="Button1" type="button" value="导出excel" onclick="tableExport('excel')" />
    </body>
js内容

导出excel

      if(type=='excel'){
            var doc="";
            doc+="<table>";
            var html=document.getElementById("score").innerHTML;
            doc+=html;
            doc+="</table>";
            var docFile="<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+doc+"' xmlns='http://www.w3.org/TR/REC-html40'>";
            docFile=docFile+"<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>"+doc+"</body></html>";
            var base64data="base64,"+window.btoa(unescape(encodeURIComponent(docFile)));
            window.open('data:application/vnd.ms-excel;'+ base64data);
        }

下面为导出excel的示例图片

image.png

你可以发现导出word和excel的代码几乎相同,所以我们可以合并他们,写的更简洁。
  function tableExport(type){
        var doc="";
        doc+="<table>";
        var html=document.getElementById("score").innerHTML;
        doc+=html;
        doc+="</table>";
        var a=document.body.innerHTML;
        var docFile="<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+a+"' xmlns='http://www.w3.org/TR/REC-html40'>";
        docFile=docFile+"<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>"+doc+"</body></html>";
        var base64data="base64,"+window.btoa(unescape(encodeURIComponent(docFile)));
        if(type=='doc'){
            window.open('data:application/msword;'+ base64data);
        }else if(type=='excel'){
            window.open('data:application/vnd.ms-excel;'+ base64data);
        }
    }

但是唯一不同的是MIME类型,也就是代码中window.open里的内容不同,如果需要查看更多网页中的重要MIME类型列表,点击这里

扩展名 文档类型 MIME Type
.doc Microsoft Word application/msword
.xls Microsoft Excel application/vnd.ms-excel
上一篇 下一篇

猜你喜欢

热点阅读