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

How export to excel or pdf a grid with detail templete?

Hi.
I have a grid works with detail template and i want know how export to excel or pdf.
There is one detail: in the details template, I am using a treegrid, Is it possible to export?
I hope you can help me
Thank you in advance for your help

Attachment: Grid_4fb5bc9a.rar

3 Replies

PK Prasanna Kumar Viswanathan Syncfusion Team September 10, 2018 06:30 AM UTC

Hi Antonio, 

Thanks for contacting Syncfusion support. 

Based on your update you have mentioned that you have a Grid with detailTemplate. In the detailTemplate you have used TreeGrid and you need to export to excel or PDF with the TreeGrid.  

By default in grid if we have used HTML elements in template, then while exporting we can export only the HTML elements as a string.  So, while using detailTemplate, the elements which has been rendered within the template is exported as a string. So, it is not feasible to export treegrid with the detailsTemplate.  
 
If you want to export only the HTML elements that you have used in detailTemplate, then you need to set IncludeDetailRow as true in the parameter of the export method. You can handle template elements using server side event while exporting grid to various files such as Excel, PDF and Word.  
  
For more information refer the below help document.   
 

Regards, 
Prasanna Kumar N.S.V 



JC Juan Cruz replied to Prasanna Kumar Viswanathan September 10, 2018 05:03 PM UTC

Hi Antonio, 

Thanks for contacting Syncfusion support. 

Based on your update you have mentioned that you have a Grid with detailTemplate. In the detailTemplate you have used TreeGrid and you need to export to excel or PDF with the TreeGrid.  

By default in grid if we have used HTML elements in template, then while exporting we can export only the HTML elements as a string.  So, while using detailTemplate, the elements which has been rendered within the template is exported as a string. So, it is not feasible to export treegrid with the detailsTemplate.  
 
If you want to export only the HTML elements that you have used in detailTemplate, then you need to set IncludeDetailRow as true in the parameter of the export method. You can handle template elements using server side event while exporting grid to various files such as Excel, PDF and Word.  
  
For more information refer the below help document.   
 

Regards, 
Prasanna Kumar N.S.V 


I have read the documentation to export but in my code I only have two <div> and in the second is where is the information I retrieve with the ajax request, with the server-side function to export only places the <div> (without information ). Can you put the information?

Attachment: Grid_b7e60a48.rar


PK Prasanna Kumar Viswanathan Syncfusion Team September 11, 2018 06:46 AM UTC

Hi Antonio, 
 
Thanks for the update. 
 
In previous update we have mentioned that we have used HTML elements in template, then while exporting we can export only the HTML elements as a string.  So, in detailTemplate we can export only the template element(HTML elements). 

So, it is not feasible to export treegrid with the detailsTemplate.   
 
Query : second is where is the information I retrieve with the ajax request, with the server-side function to export only places the <div> (without information ). Can you put the information? 
 
You can get the details of the detailTemplate in the server-side events(ExcelDetailTemplateInfo, WordDetailTemplateInfo  and PDFDetailTemplateInfo). In this event we have handle the template elements while exporting the grid. 
 
Refer the below code example:  
 


@(Html.EJ().Grid<EmployeeView>("DetailTemplate") 
       .Datasource((IEnumerable<object>)ViewBag.datasource) 
       .ClientSideEvents(eve => eve.DetailsDataBound("databound")) 
       .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items => 
       { 
           items.AddTool(ToolBarItems.ExcelExport); 
           items.AddTool(ToolBarItems.WordExport); 
           items.AddTool(ToolBarItems.PdfExport); 
       })) 
      .Columns(col => 
       { 
           ------------------------- 
       }) 
      .DetailsTemplate("#tabGridContents") 
) 
 
<script id="tabGridContents" type="text/x-jsrender"> 
    <div id="treegrid"></div> 
 
</script> 
 
[controller.cs] 
 
public void ExportToExcel(string GridModel) 
        { 
            ExcelExport exp = new ExcelExport(); 
            var DataSource = new NorthwindDataContext().EmployeeViews.ToList(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            GridExcelExport exp2 = new GridExcelExport() { IncludeDetailRow = true, Theme = "flat-saffron", FileName = "Export.xlsx" }; 
            obj.ExcelDetailTemplateInfo = templateInfo; 
            exp.Export(obj, DataSource, exp2); 
        } 
 
 
public void templateInfo(object currentCell, object row) 
        { 
            IRange range = (IRange)currentCell; 
            object templates; 
            foreach (var data in row.GetType().GetProperties()) 
            { 
                if (range.Value.Contains(data.Name)) 
                { 
                    templates = row.GetType().GetProperty(data.Name).GetValue(row, null); 
                    range.Value = range.Value.Replace(data.Name, templates.ToString()); 
                    var charsToRemove = new string[] { "{", "}", "<b>", ":", "</b>", "<br />", "style", "=", "class", "</div>", "<p>", "</p>", "detail", "<b", ">", }; 
                    foreach (var c in charsToRemove) 
                    { 
                        range.Value = range.Value.ToString().Replace(c, string.Empty); 
                    } 
                    range.HorizontalAlignment = ExcelHAlign.HAlignCenter; 
                } 
            } 
        } 
 


Screenshot: 

 
 
Regards, 
Prasanna Kumar N.S.V 


Loader.
Up arrow icon