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

Exporting strings containing numbers to Excel

I have the following problem, I've a SfDataGrid containing an arbitrary built DataTable created with a Query.
Some of the columns are strings containing numbers (codes) and the standard export made with the ExcelEngine ExportToExcel method, automatically converts the strings beginning with number in numbers loosing formatting and changing the meaning of codes.

I've looked in the ExcelExportingOptions but there is no option to tell the engine to mantain the type of the columns in the Excel worksheet
which is the best practice in this case?

I have three events in the exporting options of the engine:


None of the events allows me a direct access to the column type of the source so there is not a simple way to tell excel which columns are and must remain strings or change the content adding the apostrophe in front of the number when needed.
I was thinking to set a class level variable with the list of the string fields and check the cell content adding the leading apostrophe to the fields starting with a number when the column name is contained in the list  but which is the best event where to do it, ChildExporting or CellsExporting, or maybe there is another way to tell the exporter to preserve the type of the cells?

thank you in advance

1 Reply

SP Sowndaiyan Paulpandi Syncfusion Team February 25, 2016 01:13 PM UTC

Hi Sabrina,

Thanks for contacting Syncfusion Support.

You can set ExportMode as Text to export all cells as string to Excel instead of number.

In another way, you can handle exporting of each cell using CellsExportingEventHandler. Please find the documentation link,

You can get the column name using GridCellExcelExportingEventArgs.ColumnName property and also you can get the cell type from column name through datagrid.Columns[e.ColumnName].CellType.

You can change the value in CellsExportingEventHandler by setting e.Range.Cells[0].Text or e.Range.Cells[0].Number. For more information about formatting refer the below link,



Live Chat Icon For mobile
Up arrow icon