C#,将Npoi.ICell转换为Double类型

C#, Convert Npoi.ICell to Double type

我正在使用 NPOI 从 excel 文件导入数值数据。至于读取单元格,我遇到了这个问题"Cannot implicitly convert convert type NPOI.SS.UserModel.ICell to Double"。

    public class Clas {
       public double CellValue;
       public string CellName;
    }

    ...
     public CLAS ClasMaker(int a, int b)
            {
                C ClaA;
                ClaA.CellValue = sheet.GetRow(a).GetCell(b) // here
                ClaA.CellName = sheet.Getrow(a).GetCell(b-1).ToString() // As for string its okay i used ToString()
            }

如何将ICell转换为特定类型,如ToString()?

我无法将 CellValue 的类型更改为 var。因为它在许多其他事物中被声明为 double。

查找 here ICell 的文档。看起来您想要做的是 sheet.GetRow(a).GetCell(b).NumericCellValue。您可能还想将 toString() 更改为 StringCellValue,因为 toString 可能只是 return class 带有哈希码或类似内容的名称。

试试这个(未测试)

ClaA.CellValue = Convert.ToDouble(sheet.GetRow(a).GetCell(b).getStringCellValue());