由于某种原因,我无法使用C#的Interop.Excel库检索单元格的值。尝试访问Range对象的.Value属性时,出现了空引用异常。有问题的工作表在我定位的单元格中肯定有一个值。有小费吗?请参见下面的代码:
using System;
using System.Data;
using System.Collections.Generic;
using Microsoft.Office.Interop.Excel;
namespace C__Test
{
class Program
{
static void Main(string[] args)
{
Application xlApp = new Application();
xlApp.DisplayAlerts = false;
xlApp.ScreenUpdating = false;
xlApp.Visible = false;
Workbook xlBook = xlApp.Workbooks.Open(@"C:\Users\tsgardn\Desktop\test.xlsb",Type.Missing,true);
_Worksheet xlSheet = (_Worksheet)xlBook.Worksheets["Scorecard"];
xlBook.RefreshAll();
Range ranges = xlSheet.UsedRange;
foreach (Range c in ranges.Cells) {
Console.WriteLine("Address: " + c.Address + "Value: " + c.Value);
}
xlBook.SaveAs(@"C:\Users\tsgardn\Desktop\Corelogic Flood Scorecard - New.xlsb");
xlBook.Close(false);
}
}
}