如何在数据表中的excel导出文件中添加空行或自定义标题?

时间:2022-09-07 09:16:39

Can anyone tell me how to add some blank rows in datatables exported excel file?

谁能告诉我如何在数据表中导出excel文件中添加一些空白行?

I am trying to add headers into an excel file and I tried many approaches but none of them are working for me.

我试图将标题添加到excel文件中,我尝试了很多方法,但没有一个对我有效。

If no one knows how to do that a hint how to add blank rows in excel would also be useful for me.

如果没有人知道怎么做,提示如何在excel中添加空行也对我有用。

My simple code at the moment is below and I wanted to know what I can add to it to make it work for excel export as I've already found a solution for pdf and csv but not for excel file:

我现在的简单代码如下,我想知道我可以添加什么来使它适用于excel导出,因为我已经找到了pdf和csv的解决方案,但不是excel文件的解决方案:

$('#invoice_data').dataTable( {
    ordering: true,"autoWidth": false,
    paging: true,
    searching: true,
    dom: 'Bftripl',
    buttons: [
        'excelHtml5',
        'csvHtml5',
        'pdfHtml5'
    ]
});

Following is another try, where I am trying to add blank row but it's not working:

以下是另一个尝试,我试图添加空行但它不起作用:

var buttonCommon = {
    exportOptions: {
        format: {
            header: function ( data, row, column, node ) {
                // Strip $ from salary column to make it numeric
                return column === 1 ?
                    "llll "+row+" nd "+node :
                    data+"KKK "+row+" nd "+node;
            }
        }
    }
};
 $('#invoice_data').DataTable({
    ordering: true,"autoWidth": false,
    paging: true,
    searching: true,
    dom: 'Bftripl',
    buttons: [
        $.extend( true, {}, buttonCommon, {
            extend: 'copyHtml5'
        } ),
        $.extend( true, {}, buttonCommon, {
            extend: 'excelHtml5'
        } ),
        $.extend( true, {}, buttonCommon, {
            extend: 'pdfHtml5'
        } )
    ]
} );

Thanks Friends in advanced.

谢谢高级朋友。

1 个解决方案

#1


4  

So finally I have found a solution to this so my code is capable to place header in Excel, PDF, CSV file in datatable. As I have found it's really hard to find an answer for this, so I am placing my code here. So that anyone can use it.

所以最后我找到了解决方案,所以我的代码能够将标题放在数据表中的Excel,PDF,CSV文件中。因为我发现很难找到答案,所以我将代码放在这里。这样任何人都可以使用它。

Before placing below code, download and store the file for version 1.2.2. you can find link here: version 1.2.2 link and download following file //cdn.datatables.net/buttons/1.2.2/js/buttons.html5.js

在放置代码之前,请下载并存储1.2.2版的文件。你可以在这里找到链接:版本1.2.2链接和下载文件//cdn.datatables.net/buttons/1.2.2/js/buttons.html5.js

So place following code line before my DataTable code (I have placed above downloaded file to js folder and then rename to "buttons_export_config_header.js"):

因此,在我的DataTable代码之前放置以下代码行(我已将上面的下载文件放到js文件夹中,然后重命名为“buttons_export_config_header.js”):

<script type="text/javascript" src="../js/buttons_export_config_header.js">

I have altered line number 1129 to 1131 where I have placed following code in above file:

我更改了行号1129到1131,我在上面的文件中放了以下代码:

if ( config.header) {
         var tablecaption = [config.message];
  addRow( tablecaption, rowPos );
         //addRow( "testing", "0" );
          addRow( "", rowPos );
        addRow( data.header, rowPos );
        //$('row c', rels).attr( 's', '2' ); // bold
    }

I am really thankful to following link: datatables post

我非常感谢以下链接:datatables post

$('#invoice_data').DataTable({
    ordering: true,"autoWidth": false,
    paging: true,
    searching: true,
    dom: 'Bftripl',
    buttons: [
    {
            extend: 'excelHtml5',

            title: 'Any title for file',
            message: "Any message for header inside the file. I am not able to put message in next row in excel file but you can use \n"


        },
        {
            extend: 'csvHtml5',
            title: 'Any title for the file',
             customize: function (csv) {
                 return "Any heading for the csv file can be separated with , and for new line use \n"+csv;
              }
        },
        {
            extend: 'pdfHtml5',
            title: 'Any title for file',
            customize: function ( doc ) {
                            doc.content.splice( 0, 0, {
                                text: "custom header\n my header"
                            } );
            }
        }

        //'excelHtml5',
        //'csvHtml5',
        //'pdfHtml5'
    ] 

}); 

I am glad that I have found solution by myself.:)

我很高兴我自己找到了解决办法。:)

#1


4  

So finally I have found a solution to this so my code is capable to place header in Excel, PDF, CSV file in datatable. As I have found it's really hard to find an answer for this, so I am placing my code here. So that anyone can use it.

所以最后我找到了解决方案,所以我的代码能够将标题放在数据表中的Excel,PDF,CSV文件中。因为我发现很难找到答案,所以我将代码放在这里。这样任何人都可以使用它。

Before placing below code, download and store the file for version 1.2.2. you can find link here: version 1.2.2 link and download following file //cdn.datatables.net/buttons/1.2.2/js/buttons.html5.js

在放置代码之前,请下载并存储1.2.2版的文件。你可以在这里找到链接:版本1.2.2链接和下载文件//cdn.datatables.net/buttons/1.2.2/js/buttons.html5.js

So place following code line before my DataTable code (I have placed above downloaded file to js folder and then rename to "buttons_export_config_header.js"):

因此,在我的DataTable代码之前放置以下代码行(我已将上面的下载文件放到js文件夹中,然后重命名为“buttons_export_config_header.js”):

<script type="text/javascript" src="../js/buttons_export_config_header.js">

I have altered line number 1129 to 1131 where I have placed following code in above file:

我更改了行号1129到1131,我在上面的文件中放了以下代码:

if ( config.header) {
         var tablecaption = [config.message];
  addRow( tablecaption, rowPos );
         //addRow( "testing", "0" );
          addRow( "", rowPos );
        addRow( data.header, rowPos );
        //$('row c', rels).attr( 's', '2' ); // bold
    }

I am really thankful to following link: datatables post

我非常感谢以下链接:datatables post

$('#invoice_data').DataTable({
    ordering: true,"autoWidth": false,
    paging: true,
    searching: true,
    dom: 'Bftripl',
    buttons: [
    {
            extend: 'excelHtml5',

            title: 'Any title for file',
            message: "Any message for header inside the file. I am not able to put message in next row in excel file but you can use \n"


        },
        {
            extend: 'csvHtml5',
            title: 'Any title for the file',
             customize: function (csv) {
                 return "Any heading for the csv file can be separated with , and for new line use \n"+csv;
              }
        },
        {
            extend: 'pdfHtml5',
            title: 'Any title for file',
            customize: function ( doc ) {
                            doc.content.splice( 0, 0, {
                                text: "custom header\n my header"
                            } );
            }
        }

        //'excelHtml5',
        //'csvHtml5',
        //'pdfHtml5'
    ] 

}); 

I am glad that I have found solution by myself.:)

我很高兴我自己找到了解决办法。:)