0
votes

I have 2 buttons Excel and CSV to export into excel and csv sheets respectively for jquery datatable, Till now our jquery datatable data is getting exported and downloaded into an excel and csv files fine. But my problem here is after export into excel sheet the file name is also inserted into the first row of excel sheet along with datatable data. I want to remove that file name because as I need only the datatable data.

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

My html Datatable is:

<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 Problem is File name as "My_Report_startDate_endDate.xls" is getting inserted into the first row in the exported excel sheet. I want to remove that file name from the excel sheet. CSV file data is getting exported fine. Any help would be appreciated. Thanks.

4

4 Answers

3
votes

I had same problem. This is how i solved it:

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:'',
            }
          ],
0
votes

Use 'filename' instead of 'title'

$(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 means title of the table

0
votes

Add filename instead of title and set title null

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

By setting title to an empty, first row of title in excel sheet will remove

refer following code

 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]
                }
            }],