簡體   English   中英

使用jquery從html表生成excel表

[英]Generate excel sheet from html tables using jquery

我想在用戶點擊按鈕后生成Excel工作表。 基本上我想做的就是這里討論的內容

如何將html表值傳遞給excel表格單元格 (KalleH.Väravas答案)

用於將HTML表導出到Excel的JavaScript

但是當我點擊按鈕時,不知何故沒有任何反應。 我正在使用Mozilla瀏覽器。 我的代碼需要啟用ActiveXObject。 我需要做一些額外的事情才能完成它。

我創造了這個小提琴進行測試。 如果這工作,我會嘗試我的真實代碼。 如果這對你有用,那么請告訴我。 謝謝

jFiddle

代碼:

JS:

<script type="text/javascript">
    function CreateExcelSheet() {
        var i, j, str,
                myTable = document.getElementById('mytable'),
                rowCount = myTable.rows.length,
                excel = new ActiveXObject('Excel.Application');// Activates Excel
        excel.Workbooks.Add(); // Opens a new Workbook
        excel.Application.Visible = true; // Shows Excel on the screen
        for (i = 0; i < rowCount; i++) {
            for (j = 0; j < myTable.rows[i].cells.length; j++) {
                str = myTable.rows[i].cells[j].innerText;
                excel.ActiveSheet.Cells(i + 1, j + 1).Value = str; // Writes to the sheet
            }
        }
        return;
    }
</script>

Html:

<table id ="myTable" >
            <tr>
                <td>1</td>
                <td>Jan</td>
                <td>01/04/2012</td>
                <td>Approved</td>
            </tr>
            <tr>
                <td>2</td>
                <td>Feb</td>
                <td>01/04/2012</td>
                <td>Approved</td>
            </tr>
        </table>
        <form>
            <input type="button" onclick="CreateExcelSheet();" value="Create Excel Sheet" >
        </form>

這是答案:通過insin 導出動態html表在firefox瀏覽器中的javascript中出類拔萃

var tableToExcel = (function() {
      var uri = 'data:application/vnd.ms-excel;base64,'
        , template = '<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"><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><body><table>{table}</table></body></html>'
        , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
        , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
      return function(table, name) {
        if (!table.nodeType) table = document.getElementById(table)
        var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
        window.location.href = uri + base64(format(template, ctx))
      }
    })()

要回答有關IE的問題(由於聲譽限制,我無法在評論框中回答),Sharun發布的上述代碼將無法在IE 10+上運行(“權限被拒絕”錯誤)。 這是一個可以在IE 10+以及舊版本的IE,Chrome和FF中使用的片段:

var TableToExcel = (function () {
    var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<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"><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><body><table cellspacing="0" rules="rows" border="1" style="color:Black;background-color:White;border-color:#CCCCCC;border-width:1px;border-style:None;width:100%;border-collapse:collapse;font-size:9pt;text-align:center;">{table}</table></body></html>'
    , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
    return function (table, name) {
        if (!table.nodeType) table = document.getElementById(table)
        var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
        if (navigator.msSaveBlob) {
            var blob = new Blob([format(template, ctx)], { type: 'application/vnd.ms-excel', endings: 'native' });
            navigator.msSaveBlob(blob, 'export.xls')
        } else {
            window.location.href = uri + base64(format(template, ctx))
        }
    }
})()
      $("#btnExport").click(function(e) {
              e.preventDefault();

            //getting data from table
            var data_type = 'data:application/vnd.ms-excel';
            var table_div = document.getElementById('table_wrapper');//table_wrapper is table id
            var table_html = table_div.outerHTML.replace(/ /g, '%20');

            var a = document.createElement('a');
            a.href = data_type + ', ' + table_html;
            a.download = 'table_name_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls';
            a.click();
          });   

您可以使用這個好的庫: https//github.com/jmaister/excellentexport

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM