For some reason I'm unable to retrieve the value of a cell using C#'s Interop.Excel library. I'm getting a null reference exception when trying to access the .Value property of a Range object. The sheet in question definitely has a value in the cell I'm targeting. Any tips? Please see the code below:
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);
}
}
}