我有一张我要导入的多张表格 代码相当简单和基本,应该工作 但我的片名不断回归" _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表
这里可能还有一些其他相关的信息:
- 我在工作簿中关闭了自动过滤器
- 我在列标题打印输出中只是为了确认它正在读哪张表
任何见解都会令人赞赏 非常感谢!
答案 0 :(得分:21)
每次在工作表上过滤时,Excel都会创建一个隐藏工作表,并且在检索工作表名称时,此工作表不应该可用。 这是一段代码,可以帮助您使用System.Data.OleDb获取工作表名称:
class Retriever
{
public List<SheetName> GetSheetNames(OleDbConnection conn)
{
List<SheetName> sheetNames = new List<SheetName>();
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
DataTable excelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach (DataRow row in excelSchema.Rows)
{
if (!row["TABLE_NAME"].ToString().Contains("FilterDatabase"))
{
sheetNames.Add(new SheetName() { sheetName = row["TABLE_NAME"].ToString(), sheetType = row["TABLE_TYPE"].ToString(), sheetCatalog = row["TABLE_CATALOG"].ToString(), sheetSchema = row["TABLE_SCHEMA"].ToString() });
}
}
conn.Close();
return sheetNames;
}
}
class SheetName
{
public string sheetName { get; set; }
public string sheetType { get; set; }
public string sheetCatalog { get; set; }
public string sheetSchema { get; set; }
}
如果你遇到任何问题,请回复我。
玩得开心!