Export multiple grids to one excel file

Answered (Not Verified) This post has 0 verified answers | 11 Replies | 7 Followers Thread's RSS feed.

amos_silberman
Points 1,165
Replied On: Mon, Nov 30 2015 3:55 AM Reply

Hi,

Is it possible to export multiple grids to the same excel file?

Each grid will be on a different sheet.

This feature is available in the ASP.NET excel export, as you can use the Export method, and pass it the grid to be exported and an Excel sheet.

Thanks

  • Post Points: 20

All Replies

Nadia Robakova
Points 2,275
Infragistics Employee
Replied On: Mon, Nov 30 2015 7:32 AM Reply

Hi,

Thank you for using our forum.

Yes, it is possible to export 2 grids in same excel file.

You can look at our sample:

http://www.infragistics.com/samples/aspnet/data-grid/excel-exporter

Sincerely,
Nadia Robakova
Infragistics, Inc.

  • Post Points: 20
amos_silberman
Points 1,165
Replied On: Mon, Nov 30 2015 7:36 AM Reply

Thanks for the quick response.

I was asking about exporting the igGrid - the javascript control, not the ASP.NET server control.

thanks

  • Post Points: 20
[Infragistics]Hristo
Points 15,890
Infragistics Employee
Replied On: Wed, Dec 2 2015 4:09 AM Reply

Hello,

I have prepared a sample demonstrating how you can export two igGrids into the same excel file into different worksheets. Since this is a custom approach it may need a slight modification if you want to export more than two grids. The approach that is taken follows the scenario that:

1) Export first grid and handle the exportEnding event

2) In the exportEnding event call a function that exports the second grid and pass the workbook that contains the first grid as argument to the function

       exportEnding: function(sender, args) {
        exportSecondGrid(args.workbook);
        return false;
       }

3) Handle the headerCellExporting event for the second grid, where you change the default workbook with the workbook we already have. Also we add a new worksheet to this workbook, where the second grid will be exported:

      headerCellExporting: function(sender, args) {
       if (args.columnIndex === 0) {
        sender._workbook = workbook;
        sender._workbook.worksheets().add(sender._worksheet.name());
        sender._worksheet = sender._workbook.worksheets(1)

       }
      },

Please note there is a slight drawback of this scenario: since we add the new worksheet when exporting headers had started, the second worksheet will have default strings applied to column names. This can be corrected for example in headerCellExported event. Please let me now if you would need assistance on how to achieve this.

Best Regards,
Hristo Anastasov
Associate Software Developer
Infragistics, Inc.

  • Post Points: 20
Rob Smith
Points 45
Replied On: Thu, Dec 3 2015 4:27 PM Reply

Hello,

I am interested in the same thing. Please give an example of how to set the column names correctly on the second worksheet.

Also, how would this be done with 3 or 4 or 5 grids?

Thank you

  • Post Points: 20
[Infragistics]Hristo
Points 15,890
Infragistics Employee
Replied On: Wed, Dec 9 2015 8:52 AM Reply

Hi,

Achieving the same with more grids would follow the same approach - returning false in the exportEnding event and executing the same logic with the workbooks in next grid exporting event. Setting the columns name correctly would involve just writing value to the cell in the exportEnding event, like:

worksheet.rows(0).setCellValue(0, "text") - this will write "text" into the the first cell in the first row of the worksheet, which would be the headers row.

Best Regards,
Hristo Anastasov
Associate Software Developer
Infragistics, Inc.

  • Post Points: 20
Replied On: Thu, Aug 3 2017 7:29 AM Reply

How would I know the header values to be inserted here. In sample we are using the hardcoded text but in real we need it to come via grid's own data.

Please post the complete sample

  • Post Points: 5
Answered (Not Verified) Replied On: Thu, Aug 3 2017 2:34 PM Reply
Suggested by Tethys Technology

Managed to fix this myself after a bit of R&D. Posting complete code below (please excuse formatting)

function exportSecondGrid(workbook) {

var headerArr = [];
$.ig.GridExcelExporter.exportGrid($("#grid1"), {
fileName: "igGrid",
worksheetName: "Sheet2"
},
{
headerCellExporting: function(sender, args) {
// We will save all the headers coming to our array for retrieval later on
headerArr.push(args.headerText);
if (args.columnIndex === 0) {
sender._workbook = workbook;
sender._workbook.worksheets().add( sender._worksheet.name());
sender._worksheet = sender._workbook.worksheets(1);
}
},
exportEnding: function(sender, args) {
// Now use the array of headers to be updated
var row = sender._worksheet.rows(0);
for(var ind=0; ind < headerArr.length; ind++) {
row.setCellValue(ind, headerArr[ind]);
}

}
}
);

}

Use this with the original first function and everything will be perfect

  • Post Points: 20
Thufail
Points 40
Replied On: Sat, Aug 12 2017 8:36 PM Reply

Hi,

Is there any way to export two igGrids into single sheet within a workbook.

Thanks

  • Post Points: 20
[Infragistics]Hristo
Points 15,890
Infragistics Employee
Replied On: Mon, Aug 14 2017 2:48 AM Reply

Please refer to https://www.infragistics.com/community/forums/p/104362/493792.aspx#493792 for an overview of how to export to grids into the same worksheet.

Best Regards,
Hristo Anastasov
Associate Software Developer
Infragistics, Inc.

  • Post Points: 20
Thufail
Points 40
Replied On: Mon, Aug 14 2017 11:18 AM Reply

Hi,

The URL talks about exporting 2 iGGrids into  two separate sheets. Is there a way to combine two igGrids into one single sheet.

Thanks

  • Post Points: 20
[Infragistics]Hristo
Points 15,890
Infragistics Employee
Replied On: Thu, Aug 24 2017 2:29 AM Reply

Hi Tufail,

Each grid is exported into a table region in a worksheet, but Excel allows only one table region to be created within a single worksheet, so trying to export second grid into the same sheet will result in an error:

"There in another table in the specified region."

Best Regards,
Hristo Anastasov
Associate Software Developer
Infragistics, Inc.

  • Post Points: 5
Page 1 of 1 (12 items) | RSS