I have excel with several sheets that I want to import the
code is pretty simple and simple and should work
but my sheets keep coming back as "_xlnm # _FilterDatabase" in the debugger
and is the root of my problem
here is the relevant piece of 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))
{
connection.Open();
DataTable sheets = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
sheetName = sheets.Rows[1]["TABLE_NAME"].ToString();
OleDbCommand command = new OleDbCommand("Select * FROM ["+sheetName+"]", connection);
using (OleDbDataReader dr = command.ExecuteReader())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnx))
{
bulkCopy.ColumnMappings.Add("code", "id");
bulkCopy.ColumnMappings.Add("category10", "category");
bulkCopy.DestinationTableName = "eeo10";
bulkCopy.WriteToServer(dr);
}
}
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"]);
}
using (OleDbDataReader dr = command.ExecuteReader())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnx))
{
bulkCopy.ColumnMappings.Add("code", "id");
bulkCopy.ColumnMappings.Add("category14", "category");
bulkCopy.DestinationTableName = "eeo14";
bulkCopy.WriteToServer(dr);
}
}
}
as indicated above, the
debugger returns sheetName = "_ xlnm # _FilterDatabase"
which is rather strange, the first eeo10 works
but eeo14 does not, because it is still trying to work with the eeo10 sheet
:
-
- , , .
!