首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将html5表导出到excel jquery

将html5表导出到excel jquery
EN

Stack Overflow用户
提问于 2013-02-15 19:57:20
回答 2查看 11.1K关注 0票数 1

我想导出我的html表格到excel表格使用js或jquery我已经搜索了谷歌,但没有得到任何有用的资源。下面是我的html表代码

代码语言:javascript
复制
<table>
        <thead id='headers'>
            <tr>
                <th>Select</th>
                <th>Name</th>
                <th>Mobile</th>
                <th>Mail ID</th>
                <th>Rating</th>
                <th>Date</th>
                <th>Notify</th>
                                <th>View</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                                <td><input type="checkbox"/></td>
                <td>Praveen</td>
                <td>97910123123</td>
                <td>praveen@360i</td>
                <td>5 star</td>
                <td>15.2.2013</td>
                <td>A</td>
                                <td>4</td>
            </tr>
                        <tr>
                                <td><input type="checkbox"/></td>
                <td>kumar</td>
                <td>97912342333</td>
                <td>kumar@360i</td>
                <td>4 star</td>
                <td>16.2.2013</td>
                <td>D</td>
                                <td>3</td>
            </tr>
        </tbody>
    </table>

请帮助我找出任何解决方案.........

EN

回答 2

Stack Overflow用户

发布于 2018-01-26 05:39:41

我找到了the following,它可以在Chrome(63)、火狐(57)和IE11上运行。简而言之,您可以通过分割和连接表行来创建一个csv。接下来创建一个"text/csv“类型的Blob,最后使用锚"a”标记的download属性下载它,这样页面就不会导航到该文件。

代码语言:javascript
复制
var xport = {
  _fallbacktoCSV: true,  
  toXLS: function(tableId, filename) {   
    this._filename = (typeof filename == 'undefined') ? tableId : filename;

    //var ieVersion = this._getMsieVersion();
    //Fallback to CSV for IE & Edge
    if ((this._getMsieVersion() || this._isFirefox()) && this._fallbacktoCSV) {
      return this.toCSV(tableId);
    } else if (this._getMsieVersion() || this._isFirefox()) {
      alert("Not supported browser");
    }

    //Other Browser can download xls
    var htmltable = document.getElementById(tableId);
    var html = htmltable.outerHTML;

    this._downloadAnchor("data:application/vnd.ms-excel" + encodeURIComponent(html), 'xls'); 
  },
  toCSV: function(tableId, filename) {
    this._filename = (typeof filename === 'undefined') ? tableId : filename;
    // Generate our CSV string from out HTML Table
    var csv = this._tableToCSV(document.getElementById(tableId));
    // Create a CSV Blob
    var blob = new Blob([csv], { type: "text/csv" });

    // Determine which approach to take for the download
    if (navigator.msSaveOrOpenBlob) {
      // Works for Internet Explorer and Microsoft Edge
      navigator.msSaveOrOpenBlob(blob, this._filename + ".csv");
    } else {      
      this._downloadAnchor(URL.createObjectURL(blob), 'csv');      
    }
  },
  _getMsieVersion: function() {
    var ua = window.navigator.userAgent;

    var msie = ua.indexOf("MSIE ");
    if (msie > 0) {
      // IE 10 or older => return version number
      return parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)), 10);
    }

    var trident = ua.indexOf("Trident/");
    if (trident > 0) {
      // IE 11 => return version number
      var rv = ua.indexOf("rv:");
      return parseInt(ua.substring(rv + 3, ua.indexOf(".", rv)), 10);
    }

    var edge = ua.indexOf("Edge/");
    if (edge > 0) {
      // Edge (IE 12+) => return version number
      return parseInt(ua.substring(edge + 5, ua.indexOf(".", edge)), 10);
    }

    // other browser
    return false;
  },
  _isFirefox: function(){
    if (navigator.userAgent.indexOf("Firefox") > 0) {
      return 1;
    }

    return 0;
  },
  _downloadAnchor: function(content, ext) {
      var anchor = document.createElement("a");
      anchor.style = "display:none !important";
      anchor.id = "downloadanchor";
      document.body.appendChild(anchor);

      // If the [download] attribute is supported, try to use it

      if ("download" in anchor) {
        anchor.download = this._filename + "." + ext;
      }
      anchor.href = content;
      anchor.click();
      anchor.remove();
  },
  _tableToCSV: function(table) {
    // We'll be co-opting `slice` to create arrays
    var slice = Array.prototype.slice;

    return slice
      .call(table.rows)
      .map(function(row) {
        return slice
          .call(row.cells)
          .map(function(cell) {
            return '"t"'.replace("t", cell.textContent);
          })
          .join(",");
      })
      .join("\r\n");
  }
};
票数 1
EN

Stack Overflow用户

发布于 2013-05-17 05:25:28

以下是解决方案(link)

代码语言:javascript
复制
$("#btnExport").click(function(e) {
    window.open('data:application/vnd.ms-excel,' + $('#dvData').html());
    e.preventDefault();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14894237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档