unable to read first cell from 2nd row

J

Jerry N

I am trying to load a spreadsheet into a DataTable and have code to read one
cell at a time. When I try to read the first cell of the 2nd row (A2), the
value returned is always null. Do I need to call Select() somewhere? My
spreadsheet has 21 columns and 2 rows.

Here's my code:

workbook = app.Workbooks.Open(fileName,
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);

worksheet = (Excel.Worksheet) workbook.Worksheets[1];
range = worksheet.get_Range("A1", Type.Missing);

Excel.Range rangeTemp = range.get_End(Excel.XlDirection.xlToRight);
sheetColumns = rangeTemp.Column;

rangeTemp = range.get_End(Excel.XlDirection.xlDown);
sheetRows = rangeTemp.Row;

string cell;
for (int rowCount = 0; rowCount < sheetRows; rowCount++)
{
if (repRow == null)
repRow = repList.NewRow();
for (int columnCount = 0; columnCount < sheetColumns; columnCount++)
{
cell = Convert.ToString((char)('A' + columnCount)) +
Convert.ToString(rowCount + 1);
range = worksheet.get_Range(cell, Type.Missing);
value = range.Value2 == null ? "" : range.Value2.ToString();
...
}
...
}

Thanks,
Jerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top