6212252013005379619
become
6212252013005380000
6.21225E+18
I tried to change the columns to strings, but it didn't work
dt.Columns["aa"].DataType = typeof(string);
sfDataGrid1.DataSource = dt;
Strings are treated as numbers
6212252013005379619
become
6212252013005380000
Hi yu ma,
We can replicate the reported scenario on our end. Currently we are analyzing the scenario and will provide an update on or before January 18, 2024.
Regards,
Chidanand M.
Hi yu ma,
We have the reported scenario on our end. Your requirement can be achieved by using the ExportMode property of exportingOptions as Text. If the ExportMode is in Text mode, then the numbers are properly saved in the excel.
Kindly refer the below code snippet
|
var options = new ExcelExportingOptions(); options.ExcelVersion = ExcelVersion.Excel2013; options.ExportMode = ExportMode.Text; var excelEngine = sfDataGrid1.ExportToExcel(sfDataGrid1.View, options); |
You can also refer our online user guide documentation regarding the same by the following link and also we have attached the sample for your reference.
UG link: https://help.syncfusion.com/windowsforms/datagrid/exporttoexcel#exporting-options
We hope this helps. Please let us know, if need any further assistance.
Regards,
Chidanand M.
ExportMode.Value
ExportMode.textCan I automatically differentiate when I create a column whether the export should be a number or text based on the characteristics of the column
For now, all the numbers are text
is office mid
Hi yu ma,
We don't have direct support to provide the different ExportMode to different columns. Alternatively, you can achieve your requirement by overriding ExportToExcel method and DataGridToExcelConverter class. We attached the modified sample to achieve your requirement.
Kindly refer the below code snippet
Overriding the ExportToExcel method
|
public ExcelEngine ExportToExcel(ICollectionViewAdv view, ExcelExportingOptions excelExportingOptions) { var gridExcelConverter = new DataGridToExcelConverterExt();
var excelEngine = gridExcelConverter.ExportToExcel(sfDataGrid1, view, excelExportingOptions); gridExcelConverter = null; return excelEngine; } |
Overriding the DataGridConverter class
|
public class DataGridToExcelConverterExt : DataGridToExcelConverter { public DataGridToExcelConverterExt() { } protected override object GetCellValue(object record, IPropertyAccessProvider propertyAccessProvider, GridColumn gridColumn, ExportMode exportMode) { return base.GetCellValue(record, propertyAccessProvider, gridColumn, exportMode); } protected override void ExportRecordCellToExcel(SfDataGrid grid, IRange excelrange, ExcelExportingOptions excelExportingOptions, object record, IPropertyAccessProvider propertyAccessProvider, GridColumn gridColumn) { base.ExportRecordCellToExcel(grid, excelrange, excelExportingOptions, record, propertyAccessProvider, gridColumn); object cellValue = GetCellValue(record, propertyAccessProvider, gridColumn, excelExportingOptions.ExportMode); if(excelExportingOptions.ExportMode != ExportMode.Text && !(gridColumn is GridNumericColumn) && !(gridColumn is GridDateTimeColumn) && !(gridColumn is GridHyperlinkColumn) && !(gridColumn is GridImageColumn)) { excelrange.Worksheet.MigrantRange.ResetRowColumn(excelrange.Row, excelrange.Column); excelrange.Worksheet.MigrantRange.SetValue(cellValue != null ? cellValue.ToString() : string.Empty); } } } |
Calling the ExportToExcel method.
|
var options = new ExcelExportingOptions(); options.ExcelVersion = ExcelVersion.Excel2013; var excelEngine = this.ExportToExcel(sfDataGrid1.View, options); var workBook = excelEngine.Excel.Workbooks[0]; |
We hope this helps. If you need any assistance, feel free to let us know. We are happy to assist you.
Regards,
Chidanand M.