我正在尝试制作一个C#Excel插件来自动计算某些单元格的值。我得到的错误是“无法将null转换为int,因为它不是不可为null的值类型”。这行代码从Excel中获取空值:
int cellValue = sheet.Cells[1, 12].Value2;
Excel中有一些值(因此Excel不为空),这些值在“选项”中设置为数字。这是整个代码:
if (((RibbonCheckBox)sender).Checked)
{
try
{
Excel.Application app = new Excel.Application();
app.Workbooks.Add();
Excel.Worksheet sheet = app.ActiveSheet;
int cellValue = sheet.Cells[1, 12].Value2;
for (int i = 0; i <= sheet.Cells.Count; i++)
{
for (int j = 0; j <= sheet.Cells.Count; j++)
{
while (sheet.Cells[i + 1, 7] != null)
{
sheet.Cells[i + 1, 8].Value2 = sheet.Cells[i + 1, 7] * cellValue;
}
}
}
}
catch(Exception ex)
{
MessageBox.Show("Exceptie:" + ex);
}