Blazor Grid PDF and Excel export include aggregates

Is it possible for the blazor grid component to export the aggregate columns to PDF and Excel,when I manually add the columns to display and the data source? 

I'm using Syncfusion.Blazor.Grid (19.3.0.57)

List<GridColumn> columns = new List<GridColumn>();

                columns.Add(new GridColumn() { Field = "Cliente", Width = "270px", HeaderText = "Cliente", HeaderTextAlign = TextAlign.Left, TextAlign = TextAlign.Left });      //adding additional column
                columns.Add(new GridColumn() { Field = "Factura", AutoFit = true, HeaderText = "Factura", HeaderTextAlign = TextAlign.Left, TextAlign = TextAlign.Left });      //adding additional column
                columns.Add(new GridColumn() { Field = "Fecha", Width = "200px", HeaderText = "Fecha", HeaderTextAlign = TextAlign.Center, TextAlign = TextAlign.Center });      //adding additional column
                columns.Add(new GridColumn() { Field = "TotalFactura", HeaderText = "Total factura", HeaderTextAlign = TextAlign.Right, TextAlign = TextAlign.Right });      //adding additional column                
                columns.Add(new GridColumn() { Field = "PendientePago", HeaderText = "Pendiente pago", HeaderTextAlign = TextAlign.Right, TextAlign = TextAlign.Right });
                columns.Add(new GridColumn() { Field = "Concepto", AutoFit = true, HeaderText = "Concepto", HeaderTextAlign = TextAlign.Left, TextAlign = TextAlign.Left }); //adding additional column
 
 
PdfExportProperties PdfProperties = new PdfExportProperties();
PdfProperties.DataSource = ListaImpagosrTmp;
PdfProperties.PageOrientation = PageOrientation.Landscape;
PdfProperties.Header = Header;
PdfProperties.Footer = Footer;
PdfProperties.IsRepeatHeader = true;
PdfProperties.Theme = Theme;
PdfProperties.FileName = titulo + ".pdf";
PdfProperties.Columns = columns;
await gridImpagos.PdfExport(PdfProperties);

Grid:


Pdf document:


Excel document:



Thank you very much


6 Replies

MS Monisha Saravanan Syncfusion Team February 25, 2022 07:35 AM UTC

Hi Gerardo, 

Greetings from Syncfusion support. 

Query: “Is it possible for the blazor grid component to export the aggregate columns to PDF and Excel,when I manually add the columns to display and the data source?” 
 
Yes, it is possible to export Grid aggregates by using the events ExcelAggregateTemplateInfo and PdfAggregateTemplateInfo. Here in ExcelAggregateEventArgs/PdfAggregateEventArgs we will get corresponding template column details by using this We can customize the values or we can export the default value. Kindly check the attached code snippet and sample for your reference. 
  
<SfGrid DataSource="@Orders" ID="OrdersGrid" @ref="ordersGrid" AllowPaging="true" AllowSorting="true" 
        Toolbar="@(new List<string>() { "ExcelExport", "PdfExport" })" AllowExcelExport="true" AllowPdfExport="true"> 
 
    <GridEvents ExcelAggregateTemplateInfo="ExcelAggregateTemplateInfoHandler" 
                PdfAggregateTemplateInfo="PdfAggregateTemplateInfoHandler" OnToolbarClick="ToolbarClickHandler" TValue="Order"></GridEvents> 
 
    <GridAggregates> 
        <GridAggregate> 
            <GridAggregateColumns> 
               <GridAggregateColumn Field="@nameof(Order.Freight)" Type="AggregateType.Sum" Format="C2"> 
                    <FooterTemplate> 
                        @{ 
                            var aggregate = (context as AggregateTemplateContext); 
                            <div> 
                                <p>Sum: @aggregate.Sum</p> 
                            </div> 
                        } 
                    </FooterTemplate> 
                </GridAggregateColumn> 
            </GridAggregateColumns> 
        </GridAggregate> 
    </GridAggregates> 
 
    <GridColumns> 
... 
    </GridColumns> 
 
</SfGrid> 
 
@code { 
 
    public List<Order> Orders { get; set; } 
 
    public SfGrid<Order> ordersGrid; 
 
    public async Task ToolbarClickHandler(ClickEventArgs args) 
    { 
        if (args.Item.Id == "OrdersGrid_excelexport") 
        { 
            var columns = await ordersGrid.GetColumns();     //get the columns available in Grid 
            columns.Add(new GridColumn() { Field = "OrderDate" });      //adding additional column 
 
            ExcelExportProperties excelProperties = new() { FileName = "SomeFile.xlsx" }; 
             
 
            await ordersGrid.ExcelExport(excelProperties); 
        } 
        else if (args.Item.Id == "OrdersGrid_pdfexport") 
        { 
            var columns = await ordersGrid.GetColumns();  
            PdfExportProperties pdfProperties = new() { FileName = "SomeFile.pdf" }; 
            columns.Add(new GridColumn() { Field = "OrderDate" });      //adding additional column 
            pdfProperties.Columns = columns; 
            await ordersGrid.PdfExport(pdfProperties); 
        } 
    } 
    public void ExcelAggregateTemplateInfoHandler(ExcelAggregateEventArgs args) 
    { 
        if (args.Column.Field == "Freight") 
        { 
            args.Cell.Value = "";       //customize the aggregate cell value here 
        } 
        // Here you can customize your code 
    } 
    public void PdfAggregateTemplateInfoHandler(PdfAggregateEventArgs args) 
    { 
        // Here you can customize your code 
        if (args.Column.Field == "Freight") 
        { 
            args.Cell.Value = "";       //customize the aggregate cell value here 
        } 
 
    } 



Kindly get back to us if you have further queries. 

Regards, 
Monisha 



GL Gerardo Lopez Ruiz February 26, 2022 12:33 AM UTC

In the example you do not use pdfProperties.DataSource. When I pass a list to DataSource:


 var columns = await ordersGrid.GetColumns();

            PdfExportProperties pdfProperties = new() { FileName = "SomeFile.pdf" };

            columns.Add(new GridColumn() { Field = "OrderDate" }); //adding additional column

            pdfProperties.Columns = columns;

            pdfProperties.DataSource = Orders.OrderBy(p => p.Freight).ToList();

            await ordersGrid.PdfExport(pdfProperties);


I get the following error:

Object reference not set to an instance of an object



MS Monisha Saravanan Syncfusion Team February 28, 2022 03:01 PM UTC

Hi Gerardo 

Thanks for contacting Syncfusion support. 

Currently we are validating the reported query at our end and we are able to reproduce the reported query at our end. We will update further details within two business days (02.03.2022). Until then we appreciate your patience. 

Regards, 
Monisha 



MS Monisha Saravanan Syncfusion Team March 2, 2022 02:18 PM UTC

Hi Gerardo, 

Thanks for your patience. 

We have confirmed this as an issue and logged the defect report “Exception throws on exporting pdf and excel with datasource property” for the same. Thank you for taking time to report this issue and helping us to improve our product. At Syncfusion, we are committed to fix all validated defects (subject to technological feasibility and Product Development Life Cycle) and this fix will be included in our upcoming patch release which is expected to be rolled out on April 6th 2022.  

You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link.         


Until then we appreciate your patience. 

Regards, 
Monisha S 



MS Monisha Saravanan Syncfusion Team April 11, 2022 03:10 PM UTC

Hi Gerardo,


Sorry for the inconvenience caused.


Due to some unforeseen circumstances, we could not able to include the fix for the issue “Exception throws on exporting pdf and excel with datasource property in our mentioned weekly release But we will fix this issue and this will be included in our upcoming bi-weekly release which is expected to be rolled out on or before 20th April,
2022.


You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link.     
      

https://www.syncfusion.com/feedback/33107/exception-throws-on-exporting-pdf-and-excel-with-datasource-property


We will update you once the release is rolled out. Until then we appreciate your patience.   


Regards,

Monisha   



MS Monisha Saravanan Syncfusion Team April 20, 2022 01:52 PM UTC

Hi Gerardo,


We are glad to announce that, we have included fix for the issue “Exception throws on exporting pdf and excel with datasource property” in our release(20.1.0.50).  So please upgrade to our latest version of Syncfusion NuGet package to resolve the reported issue. Please find the Nuget package for latest fixes and features from below.


Nuget : https://www.nuget.org/packages/Syncfusion.Blazor.Grid


We thank you for your support and appreciate your patience in waiting for this release. Please get in touch with us if you would require any further assistance. 

   

Regards,            

Monisha



Loader.
Up arrow icon