When I exported Exce, I found that the number was truncated It becomes 6.21225E18

Image_4382_1705118432755

6212252013005379619

become

6212252013005380000

 6.21225E+18

I tried to change the columns to strings, but it didn't work

Image_5984_1705118586051


5 Replies

YM yu ma January 13, 2024 09:34 AM UTC

 


 dt.Columns["aa"].DataType = typeof(string);


 sfDataGrid1.DataSource = dt;

Strings are treated as numbers  


6212252013005379619

become

6212252013005380000



CM Chidanand Murugaiah Syncfusion Team January 15, 2024 03:25 PM UTC

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.



CM Chidanand Murugaiah Syncfusion Team January 18, 2024 04:47 PM UTC

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 linkhttps://help.syncfusion.com/windowsforms/datagrid/exporttoexcel#exporting-options


We hope this helps. Please let us know, if need any further assistance.


Regards,

Chidanand M.



YM yu ma January 20, 2024 09:28 AM UTC

ExportMode.Value
ExportMode.text

Can 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

Image_6714_1705742821889  

is office mid 




CM Chidanand Murugaiah Syncfusion Team January 22, 2024 03:14 PM UTC

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.


Attachment: SfDataGrid_Demo_4_8_d334f166.zip

Loader.
Up arrow icon