We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Export data to Excel in different worksheets

I'm using Blazor Server and I saw in your documentation that it's possible to export the Grid data to Excel.

I found also the way to specify a different datasource programmatically, how to add new worksheets in the Workbook and how to specify in which worksheet the data must be exported.

What I don't find is the option to specify a different datasource for every worksheet added.

I'm trying to use this piece of code:

public void ToolbarClick(Syncfusion.Blazor.Navigations.ClickEventArgs args)

    {

        if (args.Item.Id == "GridBolle_excelexport")

        {

            ExcelExportProperties ExportProperties = new ExcelExportProperties();

            ExportProperties.FileName = "Riepilogo_Bolle.xlsx";


            // Add a new workbook to the excel file that contains only 1 worksheet.

            ExportProperties.Workbook = new Workbook();

            //The line below should delete all the worksheets instead in the exported file "Sheet1" and "Sheet2" are always there

            ExportProperties.Workbook.Worksheets.Clear();

            // Add additional worksheets.

            ExportProperties.Workbook.Worksheets.Add();

            ExportProperties.Workbook.Worksheets[0].Name = "Jannuary";

//The line below should define the datasource for only the Worksheet[0] but I don't know how to do that

            ExportProperties.DataSource = bolleService.GetBollexContabilita(clientesceltoid, 1);

            ExportProperties.GridSheetIndex = 0;

            ExportProperties.Workbook.Worksheets.Add();

            ExportProperties.Workbook.Worksheets[1].Name = "February";

//The line below should define the datasource for only the Worksheet[1] but I don't know how to do that

            ExportProperties.DataSource = bolleService.GetBollexContabilita(clientesceltoid, 2);

           ExportProperties.GridSheetIndex = 1;

            // Define the Gridsheet index where Grid data must be exported.

            

            // Export the document.

            GridBolle.ExcelExport(ExportProperties);

        }

In summary I need to create one only Excel file with inside the data from my database where worksheet[0] has the data of Jannuary, worksheet[1] has data from February and so on

Is there any option to do that ?


1 Reply 1 reply marked as answer

NP Naveen Palanivel Syncfusion Team June 8, 2022 12:22 PM UTC

Hi Walter,


Greetings from Syncfusion support.


We have checked your query and as per your requirement we prepared sample in latest version. Here we have created a workbook and added two worksheets to it also we given the name to the worksheet. Inside the worksheet, we have imported data of Grid and exported it to xlsx format. Kindly refer the below code example and sample for your reference.


public List<Order> Orders { get; set; }

    public List<Order1> Orders1 { get; set; }

    public SfGrid<Order> DefaultGrid;

    public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args)

    {

        if (args.Item.Id == "Grid_excelexport") //Id is combination of Grid's ID and itemname

        {

            using (ExcelEngine excelEngine = new ExcelEngine())

            {

                IApplication application = excelEngine.Excel;

                application.DefaultVersion = ExcelVersion.Excel2013;

                IWorkbook workbook = application.Workbooks.Create(2); //worksheet is created

                IWorksheet worksheet = workbook.Worksheets[0];

                workbook.Worksheets[0].Name = "Jannuary";

                 

                //Import the data to worksheet

                IList<Order> reports = Orders; // pass the datasoruce

                worksheet.ImportData(reports, 2, 1, true);

 

                IWorksheet worksheet1 = workbook.Worksheets[1];

                workbook.Worksheets[1].Name = "February";

                //Import the data to worksheet

                IList<Order1> reports1 = Orders1; // pass the datasoruce

                worksheet1.ImportData(reports1, 2, 1, true);

 

                MemoryStream stream = new MemoryStream();

 

                //save the data in memory stream

                workbook.SaveAs(stream);




Kindly get back to us if you have further queries.


Regards,

Naveen Palanivel.


Attachment: ExcelDataGrid_39f7894.zip

Marked as answer
Loader.
Up arrow icon