ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   unable to read first cell from 2nd row (https://www.excelbanter.com/excel-programming/394436-unable-read-first-cell-2nd-row.html)

Jerry N[_2_]

unable to read first cell from 2nd row
 
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




All times are GMT +1. The time now is 06:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com