Template columns do not render when exporting to PDF or Excel

I have the following, if I use the Template it displays fine on the grid but that column will not be included in the export to Excel or PDF.  The phone numbers (all US) are stored as a string with no formatting, so I need to add that back in when I display/export them.

Thanks, Mike

<SfGrid ID="Grid" @ref="grdPhoneList" DataSource="@pList" AllowSorting="true" AllowExcelExport="true" AllowPdfExport="true" Toolbar=@ToolBar>
    <GridEvents TValue="PhoneListClass" OnToolbarClick="PhoneListToolbarClickHandler"></GridEvents>

    <GridColumns>
        <GridColumn Field=@nameof(PhoneListClass.Badge) MaxWidth="200" HeaderText="Badge" Width="90"></GridColumn>
        <GridColumn Field=@nameof(PhoneListClass.FullName) MaxWidth="200" HeaderText="Name" Width="120"></GridColumn>
        <GridColumn Field=@nameof(PhoneListClass.CellPhone) MaxWidth="200" HeaderText="Cell" Width="120">
            <Template>  //This column will not export at all, though will display fine in the grid.
                @{
                    var t = (context as PhoneListClass);
                    <span>@String.Format("{0:###-###-####}", Convert.ToInt64(t.CellPhone))</span>
                }
            </Template>
        </GridColumn>
        <GridColumn Field=@nameof(PhoneListClass.HomePhone) MaxWidth="200" HeaderText="Home" Width="120">
            @*<Template>  //If commit the Template out, the number exports, though with no formatting..
                @{
                    var t = (context as PhoneListClass);
                    <span>@String.Format("{0:###-###-####}", Convert.ToInt64(t.HomePhone))</span>
                }
            </Template>*@
        </GridColumn>
        <GridColumn Field=@nameof(PhoneListClass.WorkPhone) MaxWidth="200" HeaderText="Work" Width="120">
            @*<Template>
                @{
                    var t = (context as PhoneListClass);
                    <span>@String.Format("{0:###-###-####}", Convert.ToInt64(t.WorkPhone))</span>
                }
            </Template>*@

        </GridColumn>

    </GridColumns>





3 Replies 1 reply marked as answer

JP Jeevakanth Palaniappan Syncfusion Team November 18, 2020 01:56 PM UTC

Hi Michael, 

Greetings from Syncfusion support. 

We have validated your query and you can excel export the template columns by enabling the IncludeTemplateColumn of ExcelExportProperties/PdfExportProperties. 

The template values cannot be directly exported into the cells and so we need to use ExcelQueryCellInfoEvent/PdfQueryCellInfoEvent to 
customize the values of template columns in excel export/Pdf export. Please refer the below code snippet and the sample for your reference. 

<SfGrid ID="Grid" @ref="_sfGrid" DataSource="@Transits"  
TValue="TransitVehiclesResult"> 
    <GridEvents  ExcelQueryCellInfoEvent="ExcelQueryCellInfoHandler" OnToolbarClick="ToolbarClick" TValue="TransitVehiclesResult" /> 
    <GridColumns> 
         
    </GridColumns> 
</SfGrid> 
 
 
@code{  
   public void ExcelQueryCellInfoHandler(ExcelQueryCellInfoEventArgs<TransitVehiclesResult> args) 
    { 
        if (args.Column.Field == "KemlerCode") 
        { 
            args.Cell.Value = 'X' + args.Data.KemlerCode; 
        } 
    } 
 
 
    private async Task ToolbarClick(Syncfusion.Blazor.Navigations.ClickEventArgs args) { 
        if (args.Item.Id == "Grid_excelexport") { 
            ExcelExportProperties exportProperties = new ExcelExportProperties(); 
            exportProperties.IncludeTemplateColumn = true; 
            await _sfGrid.ExcelExport(exportProperties); 
        } 
    } 
} 


Documentation: 

Please get back to us If you need further assistance. 

Regards, 
Jeevakanth SP. 


Marked as answer

ML Michael Lambert November 18, 2020 04:40 PM UTC

Worked, thanks!


JP Jeevakanth Palaniappan Syncfusion Team November 19, 2020 04:52 AM UTC

Hi Michael, 

Thanks for the update. Please get back to us if you have any other queries. 

Regards, 
Jeevakanth SP. 


Loader.
Up arrow icon