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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to export templated / converter values from SfDataGrid cells

Thread ID:

Created:

Updated:

Platform:

Replies:

140612 Oct 28,2018 01:21 PM UTC Nov 1,2018 07:02 AM UTC WPF 3
loading
Tags: SfDataGrid
Milan Danrel
Asked On October 28, 2018 01:21 PM UTC

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,



Deivaselvan Y [Syncfusion]
Replied On 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 


Milan Danrel
Replied On November 1, 2018 06:57 AM UTC

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

Deivaselvan Y [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

or the page will be automatically redirected to sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;