jquery - 将 HTML 表格导出到 Excel 时出现格式错误

标签 jquery html excel

我正在将 HTML 表格导出到 Excel,但它给出了如下错误:

"The file you are trying to open is in the different format than the specified by the file extension..."

我尝试了不同的扩展名,例如 .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 文件有问题。

最佳答案

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
});

});

关于jquery - 将 HTML 表格导出到 Excel 时出现格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33865241/

相关文章:

javascript - 在 ie11 上使用 jquery 滚动会出现抖动元素问题

javascript - 将文本区域内容移动到输入字段并返回会删除新行。如何克服?

javascript - 如何在一个div内添加多个iframe?

excel - 循环查找功能

arrays - 赋值左侧的函数调用必须返回 Variant 或 Object,在两个数组中查找公共(public)值

javascript - 按 Div Id 显示/隐藏

javascript - 如何清除之前检查的值

javascript - 检查类(class)内部或外部的选择

javascript - 仅使 div 的折叠部分可点击 - 展开部分应保持不可点击

excel - 通过 WinHttpRequest 将多个测量值发布到 influxDB 的正确行分隔符是什么?