【问题标题】:how to remove file_name from Excel sheet after export excel file using datatables?使用数据表导出 excel 文件后如何从 Excel 工作表中删除 file_name?
【发布时间】:2017-09-14 10:40:46
【问题描述】:

我有 2 个 Excel 和 CSV 按钮分别导出到 excel 和 csv 表中用于 jquery 数据表,到目前为止,我们的 jquery 数据表数据正在导出并下载到 excel 和 csv 文件中。但是我的问题是在导出到 Excel 工作表后,文件名也与数据表数据一起插入到 Excel 工作表的第一行。我想删除该文件名,因为我只需要数据表数据。

$(document).ready(function () {
  $('#example thead tr:eq(1) th').each( function () {
       var title = $('#example thead tr:eq(0) th').eq( $(this).index() ).text();

       if(title!="Action"){
          $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
           }
         } );

         var table = $('#example').DataTable( {
              dom: 'Bfrtip',
              buttons: [
                {
                    extend: 'excel',
                    title: 'My_Report_'+$('#from_date').val()+'_'+$('#to_date').val()
                },
                {
                    extend: 'csv',
                    title: 'My_Report_'+$('#from_date').val()+'_'+$('#to_date').val() 
                }
              ],
              "bInfo": false,
              fixedHeader: true,
              "iDisplayLength": 100,
          } ); 
});

我的 html 数据表是:

<table class="table table-hover table-condensed dataTable no-footer table-bordered" id="example" role="grid" >
              <thead>
                <tr>
                   <th>Column1</th>
                   <th>Column2</th>
                   <th>Column3</th>
                   <th>Column4</th>
                   <th>Column5</th>
                   <th>Column6</th>
                   <th>Column7</th>
                   <th>Column8</th>
                   <th>Column8</th>
                   <th>Column10</th>
                </tr>
                </thead>
              <tbody>
                <td>---</td>
                -----------
              </tbody>
            </table>

我的问题 是文件名,因为“My_Report_startDate_endDate.xls”被插入到导出的 Excel 工作表的第一行。我想从 Excel 表中删除该文件名。 CSV 文件数据导出正常。任何帮助,将不胜感激。谢谢。

【问题讨论】:

    标签: jquery datatables


    【解决方案1】:

    我有同样的问题。我就是这样解决的:

    buttons: [
                {
                    extend: 'excel',
    
     filename:'My_Report_'+$('#from_date').val()+'_'+$('#to_date').val() 
     title:'',
                },
                {
                    extend: 'csv',
    
    filename:'My_Report_'+$('#from_date').val()+'_'+$('#to_date').val() 
    title:'',
                }
              ],
    

    【讨论】:

    • 使用文件名:'ANY NAME' 标题:'' 这会起作用。
    【解决方案2】:

    使用“文件名”而不是“标题”

    $(document).ready(function () {
      $('#example thead tr:eq(1) th').each( function () {
           var title = $('#example thead tr:eq(0) th').eq( $(this).index() ).text();
    
           if(title!="Action"){
              $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
               }
             } );
    
             var table = $('#example').DataTable( {
                  dom: 'Bfrtip',
                  buttons: [
                    {
                        extend: 'excel',
                        filename: 'My_Report_'+$('#from_date').val()+'_'+$('#to_date').val()
                    },
                    {
                        extend: 'csv',
                        filename: 'My_Report_'+$('#from_date').val()+'_'+$('#to_date').val() 
                    }
                  ],
                  "bInfo": false,
                  fixedHeader: true,
                  "iDisplayLength": 100,
              } ); 
    });
    

    Title 表示表格的标题

    【讨论】:

    • 感谢您的快速回复。但它不起作用。这次我们的应用程序 被插入到 excel 表的第一行。
    • 那行就像所有列中的标题还是什么?如果发生这种情况,那么它是由 Datatable 本身创建的
    • 是的,你是对的,就像第一行中所有列的标题一样插入,但从第二行开始,列标题也像 Column1 Column2 Column3 以及每个相应列下的值一样正确插入。如何解决这个问题?
    • 只有一个方法是更改​​dataTables.buttons.min.js中的设置否则我认为这是不可能的
    • 我被困在这个问题上,这看起来很简单,但没有得到解决。如何解决这个问题?
    【解决方案3】:

    添加文件名而不是标题并将标题设置为空

    $(document).ready(function () {
      $('#example thead tr:eq(1) th').each( function () {
           var title = $('#example thead tr:eq(0) th').eq( $(this).index() ).text();
    
           if(title!="Action"){
              $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
               }
             } );
    
             var table = $('#example').DataTable( {
                  dom: 'Bfrtip',
                  buttons: [
                    {
                        extend: 'excel',
                        filename: 'My_Report_'+$('#from_date').val()+'_'+$('#to_date').val(),
                        title:''
                    },
                    {
                        extend: 'csv',
                        filename: 'My_Report_'+$('#from_date').val()+'_'+$('#to_date').val(),
                        title:'' 
                    }
                  ],
                  "bInfo": false,
                  fixedHeader: true,
                  "iDisplayLength": 100,
              } ); 
    });
    

    【讨论】:

      【解决方案4】:

      通过将 title 设置为 empty,excel 表格中的第一行标题将被删除

      参考以下代码

       buttons: [{
                      extend: 'excel',
                      text: '<img src="Content/Images/Excel-Logo.jpg" style="width:50px;height:50px;"/>',
                      tag: 'span',
                      title: '',
                      init: function (api, node, config) {
                          $(node).removeClass('btn btn-secondary buttons-excel buttons-html5')
                      },
                      exportOptions: {
                          columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
                      }
                  }],
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-09
        • 2014-02-13
        相关资源
        最近更新 更多