如何获得Excel的工作表名称?但是正确的名字 [英] how can i get Excel's sheets name ? but the right names

查看:89
本文介绍了如何获得Excel的工作表名称?但是正确的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此方法来获取Excel的工作表名称,但有些方法却给了我错误的结果.
有时有些奇怪的名字,没有相同名字的工作表.
有时候给我一个表名两次.
它怎么了?
您还有其他想法吗?

感谢您的帮助.



im using this method to get Excel''s sheets name , but some how it gives me wrong result .
some times strange names which there are not sheet with the same name .
some times gives me a sheet name twice .
whats wrong with it ?
do u have any other idea ?

thanks for your help .



public static List<string> GetSheetsNames(string path, ExcelVersion excelversion)
       {
           string connectionString;
           List<string> sheets = new List<string>();
           if (excelversion == ExcelVersion.Excel2003)
           {
               connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";";
           }
           else
           {
               connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";

           }
           DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
           DbConnection connection = factory.CreateConnection();
           connection.ConnectionString = connectionString;
           connection.Open();
           DataTable tbl = connection.GetSchema("Tables");
           connection.Close();
           foreach (DataRow row in tbl.Rows)
           {
               string sheetName = (string)row["TABLE_NAME"];
               if (sheetName.EndsWith("$"))
               {
                   sheetName = sheetName.Substring(0, sheetName.Length - 1);
               }
               sheets.Add(sheetName);
           }
           return sheets;
       }

推荐答案

)) { sheetName = sheetName.Substring(0,sheetName.Length-1); } sheets.Add(sheetName); } 退货单; }
")) { sheetName = sheetName.Substring(0, sheetName.Length - 1); } sheets.Add(sheetName); } return sheets; }


我认为这些是隐藏的工作表(不知道为什么),其名称末尾带有"_".
您可以使用"_"过滤工作表.
希望这会有所帮助...
I think these are some hidden sheets (Dont know why) which have "_" at the end of their name.
You can filter out sheets with "_".
Hope this will help...


这篇关于如何获得Excel的工作表名称?但是正确的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆