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

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

问题描述

我有 jquery 数据表,其中包含以下列和数据.

I have jquery datatable having below columns and data.

我正在使用内置功能将数据导出到 excel,并且它按预期工作.

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);
            

要求是在点击excel按钮时将数组数据添加到excel中.即,在导出时将外部数组中的数据包含到网格值中.这可能吗?excel shou如下所示.

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.

我们可以在导出数据的同时利用下面的方法将自定义数据添加到jquery数据表中.

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

  customizeData: function (data) {
  
  }

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

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

因此,我们可以在此处操作整个 excel 标题/单元格数据.

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

我为单元格数据创建了自定义数组,并在data.body"中按如下所示进行了替换

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天全站免登陆