为什么我在Excel中以string的forms读取数字值?

我尝试使用NPOI库来读取excel文件。

这里是代码:

public void ReadDataFromXL() { try { for (int i = 1; i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); for (int j = 0; j < row.Cells.Count(); j++) { var columnIndex = row.GetCell(j).ColumnIndex; var cell = row.GetCell(j); if (cell != null) { switch (cell.CellType) { case CellType.Numeric: var val = cell.NumericCellValue; ; break; case CellType.String: var str = cell.StringCellValue; break; } } } } } catch (Exception) { throw; } } 

这里是我尝试阅读的.xlsx文件的内容:

在这里输入图像说明

正如你所看到的列X和列Y是数字列。 但是,当我开始阅读使用上面的代码上面的这些列时,X和Y列中的一些数字值已被代码识别为string值。

例如,在上面的图片中,单元格B4是数字types,但cell.CellType显示的是StringString的值是31.724732480727\n 。 '\ n'被附加到值。

任何想法为什么一些数值显示为string ,为什么'\ n'附加到值?

它看起来像列的数据types是string,所以如果你想检查双数据types(假设它将以num+'\n'格式,你可以尝试下面的代码片段。

  String number = "1512421.512\n"; double res; if (double.TryParse(number.Substring(0, number.Length - 1), out res)) { Console.WriteLine("It's a number! " + res); }