我正在使用NPOI来阅读excel电子表格。我在代码dr [“Amount”] = row.GetCell(4)时遇到上述错误。有人可以告诉我问题是什么。
XSSFWorkbook xssfwb;
using (FileStream file = new FileStream(excelPath, FileMode.Open, FileAccess.Read))
{
xssfwb = new XSSFWorkbook(file);
}
var sheet = xssfwb.GetSheetAt(0); // Change this to the worksheet you want to import.
var rows = sheet.GetRowEnumerator();
var dtExcelData = new DataTable();
var linenumber = 0;
DataRow dr;
dtExcelData.Columns.AddRange(new DataColumn[3] {
new DataColumn("AccountNumber", typeof(string)),
new DataColumn("Amount", typeof(decimal)),
new DataColumn("Sedol",typeof(string)) });
while (rows.MoveNext())
{
var row = (XSSFRow)rows.Current;
linenumber++;
if (row.GetCell(0) != null)
{
dr = dtExcelData.NewRow();
dr["AccountNumber"] = row.GetCell(1).ToString();
dr["Amount"] = row.GetCell(4);
dr["Sedol"] = row.GetCell(11).ToString();
dtExcelData.Rows.Add(dr);
}
}
答案 0 :(得分:1)
看起来您正在尝试将对象放入十进制字段中。你可以试试
dr["Amount"] = decimal.parse(row.GetCell(4).toString());
查看是否可以解决您的错误。