This is a migrated thread and some comments may be shown as answers.

Export to excel, both single grid to single file and multiple grid to single file

3 Answers 293 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matias
Top achievements
Rank 1
Matias asked on 18 Oct 2016, 01:08 PM

Is there any way to achieve this? I found that I have to choose between one of the two options, and I'd like to keep them both.

Thanks in advance.

3 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 20 Oct 2016, 06:58 AM
Hello Matias,

The following example demonstrates how to export multiple grid to a single Excel document. Generally you can modify the code to export only a single grid or multiple grids. Nevertheless, in case you need to export two files, one that will contains the single grid and another one that will contains the multiple grids you can execute the same logic to export twice. Please check out the following code snippet.
$("#export").click(function (e) {
    // trigger export of the products grid
    $("#products").data("kendoGrid").saveAsExcel();
    // trigger export of the orders grid
    $("#orders").data("kendoGrid").saveAsExcel();
    
    // wait for both exports to finish
    $.when.apply(null, promises)
     .then(function (productsWorkbook, ordersWorkbook) {
 
         // create a new workbook using the sheets of the products and orders workbooks
         var sheets = [
           productsWorkbook.sheets[0],
           ordersWorkbook.sheets[0]
         ];
 
         sheets[0].title = "Products";
         sheets[1].title = "Orders";
 
         var workbook = new kendo.ooxml.Workbook({
             sheets: sheets
         });
 
         // save the new workbook,b
         kendo.saveAs({
             dataURI: workbook.toDataURL(),
             fileName: "ProductsAndOrders.xlsx"
         })
     });
 
    // this will create another Excel document.
        $("#products").data("kendoGrid").saveAsExcel();
        $.when.apply(null, promises)
        .then(function (productsWorkbook) {
 
        // create a new workbook using the sheets of the products and orders workbooks
        var sheets = [
          productsWorkbook.sheets[0]
        ];
 
        sheets[0].title = "Products";
 
        var workbook = new kendo.ooxml.Workbook({
            sheets: sheets
        });
 
        // save the new workbook,b
        kendo.saveAs({
            dataURI: workbook.toDataURL(),
            fileName: "ProductsAndOrders.xlsx"
        })
    });
});


Regards,
Kostadin
Telerik by Progress
Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
0
Matias
Top achievements
Rank 1
answered on 20 Oct 2016, 03:17 PM

Hello Kostadin,

As I understand the code snippet you provided, with the same trigger (e.g. a button) I can fire two exports at the same time, one with every grid in a single file, and one with the desired grid in another file, generating the 2 files at the same time.

What I would like to achieve is, triggering the 2 exports separately, for example with 2 different buttons. In "ButtonA" I fire the script that export every grid in a single file, and in "ButtonB" I fire the export that generates a file for a single grid.

Is that possible?

Thank you very much.

0
Kostadin
Telerik team
answered on 24 Oct 2016, 10:40 AM
Hi Matias,

You can achieve that by using a single button to export the multiple grid and a single button to export only one grid. The multiple grid exporting is demonstrated in the provided sample and you can use the following code to export only a single grid.
$("#exportSingle").click(function () {
    singleExport = true;
    $("#products").data("kendoGrid").saveAsExcel();
});

Keep in mind you need to check is a single export button is clicked when products_excelExport function is called. For your convenience, I modified the sample and attached it to this thread.

I hope this information helps. 

Regards,
Kostadin
Telerik by Progress
Check out the new UI for ASP.NET Core, the most complete UI suite for ASP.NET Core development on the market, with 60+ tried-and-tested widgets, based on Kendo UI.
Tags
Grid
Asked by
Matias
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Matias
Top achievements
Rank 1
Share this question
or