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

本秂侑毒 提交于 2019-11-30 22:49:26
Catalin

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!