如何在数据表的excel导出文件中添加空白行或自定义标题? [英] How to add blank rows or custom header in excel export file in datatables?

查看:50
本文介绍了如何在数据表的excel导出文件中添加空白行或自定义标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在导出的excel文件的数据表中添加一些空白行?

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 导出,因为我已经找到了适用于 pdf 和 csv 但不适用于 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:

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

推荐答案

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

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.

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

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

因此将以下代码行放在我的 DataTable 代码之前(我已将上面下载的文件放在 js 文件夹中,然后重命名为buttons_export_config_header.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"):

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

我已将行号 1129 更改为 1131,其中在上面的文件中放置了以下代码:

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

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
    }

我非常感谢以下链接:数据表帖子

$('#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.:)

这篇关于如何在数据表的excel导出文件中添加空白行或自定义标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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