将HTML表导出到Excel时格式化错误 [英] Format error when exporting HTML table to Excel

查看:215
本文介绍了将HTML表导出到Excel时格式化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


您要打开的文件是在不同的格式比文件扩展名指定...


我尝试不同的扩展名.xlsx也仍然提供相同的错误。以下是我的jQuery代码:

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

//从我们的表获取数据
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)+'.xls';
a.click();
});
});

我正在导出到Excel的HTML表:

 < div id =table_wrapper> 
< table border =1id =listclass =table-style-two>
< tbody>
< tr>
< th> NAME< / th>
  PROJECT_NAME< / th>
<描述< / th>
< th> HOURS< / th>
< th> START_DATE< / th>
< th> CURRENT_PROJECT_STATUS< / th>
< th> WORK_FOR_TOMORROW< / th>
< th> TOMORROW_WORK_STATUS< / th>
>评论< / th>
< th> VIEW_POINT_TICKET< / th>
< th> DSR_CURRENT_DATE< / th>
< / tr>
< tr id =417>
< td> Anupam Bhattacharjee< / td>
< td> Cybage< / td>
< td>每日站立会议< / td>
< td> 0.5< / td>
< td> 2015-11-20< / td>
< td>完成< / 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 - 辅助Pavan通过skype解决问题。在Tomford骡子中,OrderShipment发生错误的固定问题。< / td>
< td> 1.5< / td>
< td> 2015-11-19< / td>
< td>完成< / td>
< td> NA< / td>
< td> NA< / td>
< td> NA< / td>
< td> WO3799< / td>
< td> 2015-11-20< / td>
< / tr>

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

编辑:它不重复这个问题,因为那个问题用户无法导出,但是我可以导出,导出的excel文件是有问题。

解决方案

你可以试试这个插件 - 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>列标题< / th>
< th>列标题< / th>
< th>列标题< / th>
< / tr>
< / body>

jQuery:



($ {
$(#table2excel)。table2excel({
// exclude CSS class
exclude:.noExl,
名称:Excel文档名称
});
});

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


I am exporting a HTML table to Excel but it is giving me error like:

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

I tried different extension like .xlsx also still giving same error. Below is my jQuery code:

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

And the HTML table I am exporting to Excel:

<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>

EDIT : It is not duplicate of this question as in that question user is not able to export but i am able to export and my exported excel file is having issue.

解决方案

You can try this plugin - 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"
  });
});

Plugin download: http://www.jqueryscript.net/table/Export-Html-Table-To-Excel-Spreadsheet-using-jQuery-table2excel.html

这篇关于将HTML表导出到Excel时格式化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆