I cannot display numbers when exporting to Excel in numeric format. First, all the numbers were exported in text format. Accordingly, when opening an eksel, all numbers were textual and eksel asked them to translate into numbers if desired.

I wrote a function that would check in what format the numbers in the database and if in the numeric then it left them so.

The problem is that the following integers become numeric, but with a comma, they remain textual, although they pass the test when debugging.

The function itself:

if (objDataType.Contains(TypeCode.Int32.ToString()) || objDataType.Contains(TypeCode.Int64.ToString()) || objDataType.Contains(TypeCode.Decimal.ToString()) || objDataType.Contains(TypeCode.Double.ToString()) || objDataType.Contains(TypeCode.Single.ToString())) { cell.DataType = new EnumValue<CellValues>(CellValues.Number); cell.CellValue = new CellValue(_table.Rows[i][j].ToString()); } else { cell.DataType = new EnumValue<CellValues>(CellValues.String); cell.CellValue = new CellValue(_table.Rows[i][j].ToString()); } 
  • Check regional settings. Which character stands as a decimal separator. - MaximK
  • CellValue takes only string as an argument. - Michael
  • And exactly the comma comes. - Michael
  • one
    And what library is used for export? - i-one
  • my experience with excell suggests that programmatically, you can use only the American format, i.e. with the point "1.0" butted here recently with a similar problem, maybe it will suit you ru.stackoverflow.com/a/501340/198316 - rdorn

1 answer 1

If the problem with the separator. Try this:

 NumberFormatInfo nfi = new CultureInfo( "ru-RU", false ).NumberFormat; cell.CellValue = new CellValue(_table.Rows[i][j].ToString("N", nfi)); 

Info: MSDN NumberDecimalSeparator

  • Does not roll. - Michael
  • What separator is in the database? "." or ","? - MaximK
  • Yes, the problem was in the separator. Recursive just replaced it and that's it. Thank you all! - Michael