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

DataGrid Export To Excel Not Working

I want to export data in a grid to excel but when you click the export button nothing happens. Below is the code:


@using BusinessMinid.HouseRenting.Data

@using BusinessMinid.HouseRenting.Models

@using Syncfusion.Blazor.Grids


<MudCard Elevation="10">

    <MudCardHeader Style="background-color:#FEDBD0">

        <CardHeaderAvatar>

            <MudAvatar Style="background-color:#442C2E">I</MudAvatar>

        </CardHeaderAvatar>

        <CardHeaderContent>

            <MudText Typo="Typo.h6">Export To Excel</MudText>

        </CardHeaderContent>

        <CardHeaderActions>

            <MudIconButton Icon="@Icons.Material.Filled.Close" Color="Color.Error" Title="Close" OnClick="Cancel" />

        </CardHeaderActions>

    </MudCardHeader>


    <MudCardContent>

        <SfGrid ID="Grid" @ref="DefaultGrid" DataSource="@Rentals" AllowPaging="true" AllowSorting="true" Toolbar="@(new List<string>() { "ExcelExport" })" AllowExcelExport="true">


            <GridEvents OnToolbarClick="ToolbarClickHandler" TValue="RentalsView"></GridEvents>

            <GridColumns>

                <GridColumn Field="@nameof(RentalsView.BuildingName)" HeaderText="Building"></GridColumn>

                <GridColumn Field="@nameof(RentalsView.UnitName)" HeaderText="Unit Name"></GridColumn>

                <GridColumn Field="@nameof(RentalsView.ClientName)" HeaderText="Client"></GridColumn>

                <GridColumn Field="@nameof(RentalsView.FromDate)" HeaderText="From" Format="d" Type=ColumnType.Date></GridColumn>

                <GridColumn Field="@nameof(RentalsView.ToDate)" HeaderText="To" Format="d" Type=ColumnType.Date></GridColumn>

                <GridColumn Field="@nameof(RentalsView.TotalMonths)" HeaderText="Months" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></GridColumn>

                <GridColumn Field="@nameof(RentalsView.Rent)" HeaderText="Rent" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></GridColumn>

                <GridColumn Field="@nameof(RentalsView.Amount)" HeaderText="Amount" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></GridColumn>

                <GridColumn Field="@nameof(RentalsView.Tax)" HeaderText="Tax" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></GridColumn>

            </GridColumns>

        </SfGrid>

    </MudCardContent>


</MudCard>


@code {

    [CascadingParameter] MudDialogInstance MudDialog { get; set; }

    private SfGrid<RentalsView> DefaultGrid;

    public List<RentalsView> Rentals { get; set; }


    public List<CompanyInfo> CompanyData { get; set; }


    public int TId;

    public string CompanyName;

    public string TINumber;


    protected override void OnInitialized()

    {

        ReadCompanyInfo.CompanyInformation?.Clear();

        ReadCompanyInfo.Read();

        CompanyData = ReadCompanyInfo.CompanyInformation;


        TId = CompanyData[0].TId;

        CompanyName = CompanyData[0].CompanyName;

        TINumber = CompanyData[0].TINumber;


        Rentals = RentalsView.TaxToGRA;

    }



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

    {

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

        {

            ExcelExportProperties ExportProperties = new ExcelExportProperties();

            ExcelHeader header = new ExcelHeader();

            header.HeaderRows = 2;

            List<ExcelCell> cell = new List<ExcelCell>

            {

                new ExcelCell() { RowSpan= 2,ColSpan=5 , Value = CompanyName + " " + " TIN: " + TINumber, Style = new ExcelStyle() { Bold = true, FontSize = 13, Italic= true } }

            };


            List<ExcelRow> HeaderContent = new List<ExcelRow>

            {

                new ExcelRow() { Cells = cell, Index = 1 }

            };

            header.Rows = HeaderContent;

            ExportProperties.Header = header;

            ExportProperties.FileName = @"C:\Home\Tax.xlsx";

            await this.DefaultGrid.ExportToExcelAsync(ExportProperties);

        }

    }


    void Cancel() => MudDialog.Cancel();


}



3 Replies

BL Balamurugan Lakshmanan Syncfusion Team March 28, 2023 05:43 PM UTC

 Hi Kingsley,


Greeting from Syncfusion support.


The excel export allows exporting DataGrid data to Excel document. You need to use the ExcelExport method for exporting. To enable Excel export in the datagrid, set theAllowExcelExport property as true.Kindly refer the UG documentation for your reference.


https://blazor.syncfusion.com/documentation/datagrid/excel-exporting


Kindly get back to us if you have further queries.


Regards,

Bala.



KB Kingsley Bainn March 29, 2023 09:11 AM UTC

Thanks. But that is not the issue. If you had taken your time to look at the code I posted, you will have noticed that it is already done. Please I am serious here and want this to work, so look carefully before responding. below is the repetition of the code:

        <SfGrid ID="Grid" @ref="DefaultGrid" DataSource="@Rentals" AllowPaging="true" AllowSorting="true" Toolbar="@(new List<string>() { "ExcelExport" })" AllowExcelExport="true">

Can you now see that AllowExcelExport is set to true?

So please give me a solution.



BL Balamurugan Lakshmanan Syncfusion Team March 30, 2023 06:13 PM UTC

Hi Kingsley,

 

We are not able to reproduce the reported issue at our end while preparing a sample as per your suggestion. Kindly refer the attached solution file and video demo for your reference. If the issue still persists, Kindly modify the attached solution file as per your requirement and please share us the issue reproducible video demo for further validation at our end.

 

Please let us know if you have any concern or question.

 

Regards,

Bala.


Attachment: BlazorApp1_e5866057.zip

Loader.
Live Chat Icon For mobile
Up arrow icon