i have an excel with multi-sheets that i want to import
the code is fairly simple and basic and should work
but my sheetnames keep coming back as "_xlnm#_FilterDatabase" in the debugger
and is the root of my prob
here is the relevant portion of the code:
string sheetName = "";
file = HostingEnvironment.MapPath("~/files/master1.xlsx");
xConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
using (OleDbConnection connection = new OleDbConnection(xConnStr))
{
// get sheet names
connection.Open();
DataTable sheets = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
// eeo10 (2nd tab: first sheet to be imported)
sheetName = sheets.Rows[1]["TABLE_NAME"].ToString();
OleDbCommand command = new OleDbCommand("Select * FROM ["+sheetName+"]", connection);
// Create DbDataReader to Data Worksheet
using (OleDbDataReader dr = command.ExecuteReader())
{
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnx))
{
bulkCopy.ColumnMappings.Add("code", "id");
bulkCopy.ColumnMappings.Add("category10", "category");
bulkCopy.DestinationTableName = "eeo10";
bulkCopy.WriteToServer(dr);
}
}
// eeo14 (3rd tab: second sheet to be imported)
sheetName = sheets.Rows[2]["TABLE_NAME"].ToString();
command = new OleDbCommand("Select * FROM [" + sheetName + "]", connection);
DataTable cols = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, sheetName, null });
foreach (DataRow r in cols.Rows)
{
System.Diagnostics.Debug.WriteLine("{0} = {1}", r["COLUMN_NAME"], r["ORDINAL_POSITION"]);
}
// Create DbDataReader to Data Worksheet
using (OleDbDataReader dr = command.ExecuteReader())
{
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnx))
{
bulkCopy.ColumnMappings.Add("code", "id");
bulkCopy.ColumnMappings.Add("category14", "category");
bulkCopy.DestinationTableName = "eeo14";
bulkCopy.WriteToServer(dr);
}
}
}
so as stated before
the debugger returns sheetName="_xlnm#_FilterDatabase"
which weirdly enough the first one eeo10 works
but eeo14 doesnt because its still trying working with the eeo10 sheet
here maybe some other relevant pieces of information:
- i have turned off autofilter in the workbook
- i put in the column header printout just to confirm which sheet it was reading
any insights would be apprec
thanks so much!