【问题标题】:Format error when exporting HTML table to Excel将 HTML 表格导出到 Excel 时出现格式错误
【发布时间】:2015-11-23 06:55:00
【问题描述】:

我正在将 HTML 表格导出到 Excel,但它给了我这样的错误:

“您尝试打开的文件与文件扩展名指定的格式不同...”

我尝试了不同的扩展名,如 .xlsx 也仍然给出相同的错误。下面是我的 jQuery 代码:

$(document).ready(function() {
      $("#btnExport").click(function(e) {
        e.preventDefault();

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

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

还有我要导出到 Excel 的 HTML 表格:

<div id="table_wrapper">
    <table border="1" id="list" class="table-style-two">
        <tbody>
            <tr>
                <th>NAME</th>
                <th>PROJECT_NAME</th>
                <th>DESCRIPTION</th>
                <th>HOURS</th>
                <th>START_DATE</th>
                <th>CURRENT_PROJECT_STATUS</th>
                <th>WORK_FOR_TOMORROW</th>
                <th>TOMORROW_WORK_STATUS</th>
                <th>COMMENTS</th>
                <th>VIEW_POINT_TICKET</th>
                <th>DSR_CURRENT_DATE</th>
            </tr>
            <tr id="417">
                <td>Anupam Bhattacharjee</td>
                <td>Cybage</td>
                <td>Daily standup meeting</td>
                <td>0.5</td>
                <td>2015-11-20</td>
                <td>Complete</td>
                <td>NA</td>
                <td>NA</td>
                <td>NA</td>
                <td>NA</td>
                <td>2015-11-20</td>
            </tr>
            <tr id="418">
                <td>Anupam Bhattacharjee</td>
                <td>Tomford</td>
                <td>TF3799-28303 - Assisted Pavan in resolving the issue via skype.Fixed issue in Tomford mule where the OrderShipment was erring out.</td>
                <td>1.5</td>
                <td>2015-11-19</td>
                <td>Complete</td>
                <td>NA</td>
                <td>NA</td>
                <td>NA</td>
                <td>WO3799</td>
                <td>2015-11-20</td>
            </tr>

        </tbody>
    </table>
</div>

编辑:这不是 this 问题的重复,因为在该问题中用户无法导出,但我能够导出并且我导出的 excel 文件有问题。

【问题讨论】:

标签: jquery html excel


【解决方案1】:

Check here might this will help you out

$(document).ready(function () {

function exportTableToCSV($table, filename) {

    var $rows = $table.find('tr:has(td)'),

        // Temporary delimiter characters unlikely to be typed by keyboard
        // This is to avoid accidentally splitting the actual contents
        tmpColDelim = String.fromCharCode(11), // vertical tab character
        tmpRowDelim = String.fromCharCode(0), // null character

        // actual delimiter characters for CSV format
        colDelim = '","',
        rowDelim = '"\r\n"',

        // Grab text from table into CSV formatted string
        csv = '"' + $rows.map(function (i, row) {
            var $row = $(row),
                $cols = $row.find('td');

            return $cols.map(function (j, col) {
                var $col = $(col),
                    text = $col.text();

                return text.replace(/"/g, '""'); // escape double quotes

            }).get().join(tmpColDelim);

        }).get().join(tmpRowDelim)
            .split(tmpRowDelim).join(rowDelim)
            .split(tmpColDelim).join(colDelim) + '"',

        // Data URI
        csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

    $(this)
        .attr({
        'download': filename,
            'href': csvData,
            'target': '_blank'
    });
}

// This must be a hyperlink
$(".export").on('click', function (event) {
    // CSV
    exportTableToCSV.apply(this, [$('#dvData>table'), 'export.csv']);

    // IF CSV, don't do event.preventDefault() or return false
    // We actually need this to be a typical hyperlink
});

});

【讨论】:

  • 它正在生成 CSV,但我想要它在 excel 中。
【解决方案2】:

你可以试试这个插件 - tableExport.js

HTML:

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="src/jquery.table2excel.js"></script>
<body>
<tr class="noExl">
  <th>#</th>
  <th>Column heading</th>
  <th>Column heading</th>
  <th>Column heading</th>
</tr>
</body>

jQuery:

$("button").click(function(){
  $("#table2excel").table2excel({
    // exclude CSS class
    exclude: ".noExl",
    name: "Excel Document Name"
  });
});

插件下载: http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel.html

【讨论】:

  • 这也不起作用它的显示错误。 "Excel 无法打开文件,因为文件格式或扩展名无效"
【解决方案3】:

Javascript

$("#btn").click(function (e) {
    window.open('data:application/vnd.ms-excel,' + $('#tableData').html());
    return false;
});

HTML

<input type="button" id="btn" value="EXPORT" />

<div id="tableData">
   <table>
        <tbody>
            <tr>
                <th>NAME</th>
                <th>PROJECT_NAME</th>
                <th>DESCRIPTION</th>
                <th>HOURS</th>
                <th>START_DATE</th>
                <th>CURRENT_PROJECT_STATUS</th>
                <th>WORK_FOR_TOMORROW</th>
                <th>TOMORROW_WORK_STATUS</th>
                <th>COMMENTS</th>
                <th>VIEW_POINT_TICKET</th>
                <th>DSR_CURRENT_DATE</th>
            </tr>
            <tr id="417">
                <td>Anupam Bhattacharjee</td>
                <td>Cybage</td>
                <td>Daily standup meeting</td>
                <td>0.5</td>
                <td>2015-11-20</td>
                <td>Complete</td>
                <td>NA</td>
                <td>NA</td>
                <td>NA</td>
                <td>NA</td>
                <td>2015-11-20</td>
            </tr>
            <tr id="418">
                <td>Anupam Bhattacharjee</td>
                <td>Tomford</td>
                <td>TF3799-28303 - Assisted Pavan in resolving the issue via skype.Fixed issue in Tomford mule where the OrderShipment was erring out.</td>
                <td>1.5</td>
                <td>2015-11-19</td>
                <td>Complete</td>
                <td>NA</td>
                <td>NA</td>
                <td>NA</td>
                <td>WO3799</td>
                <td>2015-11-20</td>
            </tr>
       </tbody>
    </table>
</div>

【讨论】:

  • 它在打开 excel 时给出相同的错误消息“您尝试打开的文件与文件扩展名指定的格式不同...”
  • 它也显示同样的错误,它在 excel 中显示 HTML。
【解决方案4】:

该文件基本上只是一个带有 XLS 扩展名的 CSV 文件,以便在 Excel 中打开它。如果您有问题,只需将文件名重命名为“.csv”。 这只是我找到了一个好的答案。Here

【讨论】:

    猜你喜欢
    • 2011-09-28
    • 1970-01-01
    • 2015-07-08
    • 2014-10-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 2016-05-15
    • 2018-06-24
    相关资源
    最近更新 更多