C# Convert Number stored as Text to Number when exporting to Excel

2k views Asked by At

I export data from datagrid to Excel and one column with numbers is with error 'numbers stored as text'. I am trying to convert them using following code, but its not working. Could you suggest?

The problem is that then column headers are moved up and data is broken.

  worksheet.Range["E5", "E5"].EntireColumn.TextToColumns();
  worksheet.Columns[5].NumberFormat = "# ##0,00";

Problem shown

1

There are 1 answers

0
Dash_Xyz_Abc On

This code converts numbers stored in text format to number format. If you want to use it to opposite direction set the fieldinfo to { 1, 2 }.

Range rng = Worksheet.Range["A1:A10""];

rng.TextToColumns(rng, XlTextParsingType.xlDelimited, XlTextQualifier.xlTextQualifierDoubleQuote, Tab: true, FieldInfo: new int[] { 1, 1 }, TrailingMinusNumbers: true);