如何从数组添加其他数据以导出到jQuery数据表中的Excel [英] How to add additional data from an array to export to excel in jquery data table

查看:153
本文介绍了如何从数组添加其他数据以导出到jQuery数据表中的Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的列和数据的jquery数据表。


我正在使用内置功能将数据导出到excel,并且可以按预期运行。


  var table = $('#example')。DataTable({
dom:' Bfrtip,
按钮:[
'copyHtml5',
'excelHtml5',
'csvHtml5',
'pdfHtml5'
],
stateSave:true,



rowsGroup:[//始终按指定的顺序将列选择器的数组(!)应用于行分组
//(列选择器可以是https://datatables.net/reference/type/column-selector中指定的任何一个)

1,0
]

});



要求是在单击excel按钮时将数组数据添加到excel中。
,即在导出时将外部阵列中的数据包括到网格值中。这可能吗? excel寿 ld如下。


我们将不胜感激。

解决方案

对此我有一个解决方法,我在这里分享。



我们可以在导出数据时利用以下内容向jquery数据表添加自定义数据。



  CustomizeData:函数(数据){}  



在这里您将在 data.header中获得标头,而单元格数据将在 data.body中获得。



因此,我们可以操纵整个excel标头/ cell数据在这里。



我已经为单元格数据创建了自定义数组,并在 data.body中将其替换如下:



< pre class = snippet-code-js lang-js prettyprint-override> customize:function(xlsx){ },customizeData:函数(数据){var excelbodyData = getBodyArray(); var excelHeader = getHeaderArray(); data.header = []; data.header = excelHeader; data.body = []; data.body = excelbodyData; }


I have jquery datatable having below columns and data.

I am using built-in functionality to export the data to excel and it is working as expected.

 var table = $('#example').DataTable({
                dom: 'Bfrtip',
                buttons: [
                    'copyHtml5',
                    'excelHtml5',
                    'csvHtml5',
                    'pdfHtml5'
                ],
                stateSave: true,
                
                
               
                rowsGroup: [// Always the array (!) of the column-selectors in specified order to which rows grouping is applied
                    // (column-selector could be any of specified in https://datatables.net/reference/type/column-selector)

                    1, 0
                ]
               
            });

Now, I have an array having data similar to grid definition.

 var aData = new Array();
            var array1 = {

                Name: "David",
                Position: "Software Engineer",
                Office: "Tokyo",
                OfficeId: "1000",
                Age: 29,
                "Start date": "2011/04/25",
                Salary: "$320,800"
            };
            aData.push(array1);
            

Requirement is to add the array data to the excel when clicking the excel button. i.e, include data from an exteranal array to grid value while exporting. Is this possible? The excel should be as below.

Any help will be appreciated. Thanks in advance.

解决方案

I got a resolution for this and I am sharing here.

We can make use of below to add custom data to jquery datatable while exporting data.

  customizeData: function (data) {
  
  }

Here you will get header in " data.header" and cell data will be available in "data.body".

So, we can manipulate entire excel header/cell data here.

I have created custom array for cell data and replced as below in "data.body"

customize: function (xlsx) {


                    },
customizeData: function (data) {
                        
     var excelbodyData = getBodyArray();
    var excelHeader =getHeaderArray();
     data.header = [];
     data.header = excelHeader;
     data.body = [];
      data.body = excelbodyData;
  }

这篇关于如何从数组添加其他数据以导出到jQuery数据表中的Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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