multi-sheet import with oledb netting “_xlnm#_FilterDatabase” as sheet names

后端 未结 1 410
野性不改
野性不改 2021-01-06 09:21

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#_Filter

相关标签:
1条回答
  • 2021-01-06 09:50

    Excel creates a hidden sheet each time you filter on a sheet and all though this sheet should not be available when retrieving the sheet names. Here is a piece of code that will help you get the sheet names using 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; }
    }
    

    Please get back to me if you're having any kind of issues with this.

    Have fun!

    0 讨论(0)
提交回复
热议问题