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

How to export templated / converter values from SfDataGrid cells

Hello,

I'm using the example of exporting content from an SFDataGrid to Excel.  It seems to work OK except that some cells in my datagrid
are using image converters to convert "Y" and "N" to certain images to display in the cell.   For example a "Y" would be converted to a green circle image,
and a "N" to a red circle image.

I've tried both settings for ExportMode (text and value) but the cells are always exported as blank.

            var options = new ExcelExportingOptions();
            options.ExcelVersion = Syncfusion.XlsIO.ExcelVersion.Excel2013;
            options.ExportMode = ExportMode.Value;

I would like to export the cells as images (if possible), but if not, then at the very least export them as "Y" or "N".  I see that there is a method called
GridCellExcelExportingEventHandler, but not sure if that is what I need to use.

Any suggestions or sample code to help out would be appreciated.

thank you,



3 Replies

DY Deivaselvan Y Syncfusion Team October 31, 2018 07:08 AM UTC

Hi Milan,

Thank you for contacting Syncfusion support.

As you specified, you can customize the cell value of the Worksheet during exporting using GridCellExcelExportingEventHandler which you handled in the converter for the DataGrid. We have prepared a sample to showcase on inserting the image to the Cell value of the Worksheet. You could find the code example for the same from below.

Code Example:

 
private void GridCellExcelExportingEvent(object sender, GridCellExcelExportingEventArgs e) 
{ 
    //Check whehter the CellType is RecordCell and the column name is IsCheck which is GridTemplateColumn contains the image. 
    if (e.CellType == ExportCellType.RecordCell && e.ColumnName == "IsCheck") 
    { 
        //Access the fail image as default value 
        System.Drawing.Image image = System.Drawing.Image.FromFile(@"..\..\Images\fail.png"); 
        //Images are exported based on the CellValue  
        if (e.CellValue.Equals("Pass")) 
        { 
            //Access the image from the specified path  
            image = System.Drawing.Image.FromFile(@"..\..\Images\pass.png");    
        } 
        //Add Picture to the specific range of cell in Worksheet 
        var pic = e.Range.Worksheet.Pictures.AddPicture(e.Range.Row, e.Range.Column, image); 
        //Adjust Position of the picture based on the expected appearance in the exported cell 
        pic.Left += 20; 
        pic.Top += 5; 
        //Adjust image size to the cell width and height when it's exceeded 
        if (image.Width > e.Range.ColumnWidth) 
            pic.Width = (int)e.Range.ColumnWidth; 
        if (image.Height > e.Range.RowHeight) 
            pic.Height = (int)e.Range.RowHeight; 
        e.Range.Cells[0].Value = null; 
        e.Handled = true; 
    } 
} 

Please find the sample for the same from the below link and let us know if this helps you.
http://www.syncfusion.com/downloads/support/forum/140601/ze/ExportIcontoExcel1507347646.zip

You can also refer the below articles to format the cell values while exporting to Excel and PDF
https://www.syncfusion.com/kb/3447/how-to-export-images-which-is-loaded-inside-the-gridtemplatecolumn-to-pdf-document
https://www.syncfusion.com/kb/5541/how-to-customize-cell-value-while-exporting-the-sfdatagrid-into-excel-and-pdf

Refer the articles from below for some other use cases can be achieved in exporting
https://www.syncfusion.com/kb/wpf/sfdatagrid?sortId=0&tags=exporting

Note:
Excel treat the picture as a movable and overlapping shape and not like a inline text in the cell. So, you should customize it position by your end during the insertion based on the appearance how you expect that in Excel file. Refer the below documentation for more details on inserting and positioning the picture using XlsIO.
https://help.syncfusion.com/file-formats/xlsio/working-with-pictures#positioning-and-re-sizing-pictures

Regards,
Deivaselvan 



MD Milan Danrel November 1, 2018 06:57 AM UTC

Thanks for the prompt answer - the solution worked. Very much appreciated.


DY Deivaselvan Y Syncfusion Team November 1, 2018 07:02 AM UTC

Hi Milan,

We are happy to hear that the given solution resolved your requirement. Please let us know if you require any further assistance.

Regards,
Deivaselvan 


Loader.
Live Chat Icon For mobile
Up arrow icon