I'm trying to write an app that will open an excel spreadsheet find the worksheet with the correct name and iterate through the rows until I find the cell at column 0 that contains the text "Cont Date" and then read through until I find the first blank cell (column 0 as well). I'm getting hung up on how to iterate through the rows.
Here's what I have so far:
public static void LoadFromFile(FileInfo fi)
{
Application ExcelObj = new Application();
if (ExcelObj != null)
{
Workbook wb = ExcelObj.Workbooks.Open(fi.FullName,
Type.Missing, true, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Sheets sheets = wb.Worksheets;
foreach (Worksheet ws in sheets)
{
if (ws.Name == "Raw Data")
LoadFromWorkSheet(ws);
}
wb.Close(false, Type.Missing, Type.Missing);
}
}
public static void LoadFromWorkSheet(Worksheet ws)
{
int start = 0;
int end = 0;
// Iterate through all rows at column 0 and find the cell with "Cont Date"
}
Apparently you can't
foreach(Row row in worksheet.Rows)
{
}
EDIT::
What I did was this:
for (int r = 0; r < 65536; r++)
{
string value = ws.Cells[r, 0].Value;
}
Which gives me the following exception when trying to read the value of the cell:
Exception from HRESULT: 0x800A03EC