3
我有,我想導入
的代碼是相當簡單和基本的,並應工作
多張的Excel,但我sheetnames滾滾而來早在在調試器「_xlnm#_FilterDatabase」
,是我的概率多頁導入與OLEDB網「_xlnm#_FilterDatabase」作爲工作表名稱
這裏的根代碼的相關部分:
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);
}
}
}
從而
如前所述調試器返回SHEETNAME =「_ xlnm#_FilterDatabase」
其古怪足夠的第一個eeo10作品
但eeo14犯規,因爲它仍然在試圖與eeo10片
這裏也許一些信息的其他相關部件的工作:
- 我已經在工作簿
關閉自動篩選 - 我把列標題打印只是爲了確認它正在讀
任何見解這片將apprec
非常感謝!