How to get list of ONLY excel worksheet names in Excel using OLEDB; filter out non-worksheets that show up in metadata

前端 未结 4 2225
眼角桃花
眼角桃花 2020-12-28 22:05

I have an issue getting worksheet names from an Excel spreadsheet using OLEDB. The problem is that when I use GetOleDbSchemaTable, the resulting DataTable has more than jus

4条回答
  •  孤独总比滥情好
    2020-12-28 22:29

    You can test EndsWith("$") instead of Contains("$") like below:

    List lstsheetNames = new List();
    String sheetName;
    foreach (DataRow row in schemaTable.Rows)
    {
        sheetName = row.Field("TABLE_NAME");
        String strTemp = sheetName.Split(' ');
    
        if(strTemp.Length == 1 && sheetName.EndsWith("$"))
           lstsheetNames.Add(sheetName.Substring(0, sheetName.Length - 1));
    
        else if(strTemp.Length > 1 && strTemp.GetValue(strTemp.Length - 1).ToString().EndsWith("$'"))
           lstsheetNames.Add(sheetName.Substring(1, sheetName.Length - 3));
    }
    

    I have used this code in a same problem and it works fine.

    Edit : Sorry,I did not pay attention to this.I changed the code now.It might not the best or shortest way but it works.

提交回复
热议问题