I have an Excel workbook where the Table is placed after the 9th row in the worksheet. How am I supposed to read the Table at that point?
Currently, I am able to read the Excel worksheet using Microsoft.ACE.OLEDB.12.0 provider like this:
OleDbConnection connection = new OleDbConnection();
var connectionString = $"Provider=Microsoft.ACE.OLEDB.12.0; data source={fileName}; Extended Properties=Excel 8.0;";
connection.ConnectionString = connectionString;
connection.Open();
DataTable dbSchema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dbSchema == null || dbSchema.Rows.Count < 1)
{
throw new Exception("Error: Could not determine the name of the first worksheet.");
}
string firstSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
var adapter = new OleDbDataAdapter($"SELECT * FROM [{firstSheetName}]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "anyNameHere");
DataTable table = ds.Tables[0];
MessageBox.Show($"No of Records found: {table.Rows.Count}");
What I observe in the above code is that the Table is read but null values are yielded for non-table fields. However, I will need to do a filter for Row Number after n ( if n is the place where the table is placed) if I am supposed to get the intended.
I would welcome if this can be achieved by other means instead of OleDbConnection
So you know the index of the heading
Actually, I use
ExcelDataReader.Mapping, you can specify the row of the heading, here is how it works:I have this data in an excel file
Model
Usage (note that
HeadingIndextakes value of 8 = 9 - 1)That's it.