2014-04-12 469 views
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
非常感謝!

回答

19

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; } 
} 

請打電話給我,如果你有任何與此問題。

玩得開心!

相關問題