/// <summary>
        /// 获取指定Excel内Sheet集合
        /// </summary>
        /// <param name="pFilePath"></param>
        /// <param name="pOutInfo"></param>
        /// <returns></returns>
        public static string[] GetExcelSheetNames(string pFilePath, out string pOutInfo)
        {
            string vOutInfo = string.Empty;
            List<string> vList = new List<string>();
            try
            {
                string strConn = string.Format("Provider=Microsoft.Ace.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes;IMEX=2'", pFilePath);
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                DataTable sheetNames = conn.GetOleDbSchemaTable
                (System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                conn.Close();

string[] vSheets = new string[sheetNames.Rows.Count];
                string vName = string.Empty;

//填充 vSheets 数组
                for (int i = 0; i < sheetNames.Rows.Count; i++)
                {
                    vSheets[i] = sheetNames.Rows[i][2].ToString().Trim();
                }
                //对特殊字符进行规范处理
                string pSheetName = string.Empty;
                for (int i = 0; i < vSheets.Length; i++)
                {
                    string pStart = vSheets[i].Substring(0, 1);
                    string pEnd = vSheets[i].Substring(vSheets[i].Length - 1, 1);
                    if (pStart == "'" && pEnd == "'")
                    {
                        vSheets[i] = vSheets[i].Substring(1, vSheets[i].Length - 2);
                    }
                    Char[] pChar = vSheets[i].ToCharArray();
                    pSheetName = string.Empty;
                    for (int j = 0; j < pChar.Length; j++)
                    {
                        if (pChar[j].ToString() == "'" && pChar[j + 1].ToString() == "'")
                        {
                            pSheetName += pChar[j].ToString();
                            j++;
                        }
                        else
                        {
                            pSheetName += pChar[j].ToString();
                        }
                    }
                    vSheets[i] = pSheetName;
                }
                //当最后字符为$时移除
                for (int i = 0; i < vSheets.Length; i++)
                {
                    pSheetName = vSheets[i];
                    if (pSheetName.Substring(pSheetName.Length - 1, 1) == "$")
                    {
                        vSheets[i] = pSheetName.Substring(0, pSheetName.Length - 1);
                    }
                }
                //移除重复的Sheet名(因为特殊原因,通过这个方法获取的Sheet会有重名)
                for (int i = 0; i < vSheets.Length; i++)
                {
                    if (vList.IndexOf(vSheets[i].ToLower()) == -1)
                    {
                        vList.Add(vSheets[i]);
                    }
                }
            }
            catch (Exception vErr)
            {
                vOutInfo = vErr.Message;
            }
            pOutInfo = vOutInfo;
            return vList.ToArray();
        }

C#获取Excel Sheet名称,对特殊字符、重名进行了处理的更多相关文章

  1. C#中获取Excel文件的第一个表名

    //    2.以数据库方式打开并输入数据//      此方式将xls文件所在目录看作数据库,其中的xls文件看作数据库表,表名即文件名(不加扩展名).//      函数importExcelTo ...

  2. 获取Excel表中各个Sheet的方法

    获取Excel表中各个Sheet的方法 private void simpleButton2_Click(object sender, EventArgs e) { OfdBOM.Filter = & ...

  3. 利用POI获取Excel中图片和图片位置

    利用POI获取Excel中图片和图片位置(支持excel2003or2007多sheet) 转自:http://blog.csdn.net/delongcpp/article/details/8833 ...

  4. VB6.0 获取Excel文件工作表Sheet的名称

    获取Excel文件工作表Sheet的名称 '产生Excel文档 Dim xlapp, xlbook As Object Dim sSheetName As String Set xlapp = Cre ...

  5. 怎么利用Aspose.Cells 获取excel 数据表中sheet的名称

    说明:开发环境 vs2012 asp.net mvc4 c# 利用Aspose.Cells 获取Excel数据表的sheet的名称,并把获取的名称赋值给easyUI 的combobox 1.运行效果 ...

  6. C#获取Excel中所有的Sheet名称

    原文地址:http://blog.csdn.net/qq1010726055/article/details/6858849 Excel.Application myExcel = new Excel ...

  7. python excel练习:新建sheet、修改名称、设定颜色、打印sheet名称,复制,保存

    练习: 新建一个sheet 设定一个sheet的插入位置 修改sheet的名称为‘xiaxiaoxu’ 设定该sheet的背景标签的颜色 获取全部sheet的名称,打印每个sheet的名称 copy一 ...

  8. 获取Excel工作薄中Sheet页(工作表)名集合

    #region 获取Excel工作薄中Sheet页(工作表)名集合 02./// <summary> 03./// 获取Excel工作薄中Sheet页(工作表)名集合 04./// < ...

  9. C#可以获取Excel文件中Sheet的名字

    C#可以获取Excel文件中Sheet的名字吗 C#可以获取Excel文件中Sheet的名字吗 我试过WPS的表格可以 可以 要代码么 百度都有 [深圳]Milen(99696619)  14:13: ...

随机推荐

  1. Oracle sql语言模糊查询--like后面的通配符

    关于like后面的条件,Oracle提供了四种匹配模式: 1,% :表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FR ...

  2. QTestLib Tutorial

    本学习指南介绍了如何使用QTestLib框架的一些特性,分为4章: 编写一个单元测试程序 数据驱动测试 模拟GUI事件 重复GUI事件 第一章 编写一个单元测试程序 文件列表: qtestlib/tu ...

  3. activity的生命周期详解

    刚在看mars老师的视频,看到activity的生命周期,就看了一下,总结了一下.下面是各函数的调用时机 为了更清楚的看清楚工作的具体过程,举例如下: ,建立两个activity,一个main,一个a ...

  4. xss框架(二)基础框架实现

    简述 自上一篇博客介绍浏览器通信以来已经过去将近两个月了,兜兜转转挖了不少坑,也走了很多弯路.期间研究saml2.0和单点登录等技术都最后无疾而终. 只有xss框架这部分坚持了下来,这个框架还有很多事 ...

  5. Python成长之路_装饰器

    一.初入装饰器 1.首先呢我们有这么一段代码,这段代码假设是N个业务部门的函数 def f1(aaa): print('我是F1业务') if aaa == 'f1': return 'ok' def ...

  6. Android小记之--ClickableSpan

    在给TextView设置超链接时,要想ClickableSpan的onClick事件响应,还必须同时设置tv.setMovementMethod(LinkMovementMethod.getInsta ...

  7. C#、VB.NET 使用System.Media.SoundPlayer播放音乐

    原文:C#.VB.NET 使用System.Media.SoundPlayer播放音乐 同步播放: System.Media.SoundPlayer player = new System.Media ...

  8. IPicture总结

    1.利用IPicture接口加载.显示图片 IPicture接口管理一个图片对象和它的属性.图片对象提供对Bitmap Icon Metafile的语言不相关的抽象支持.图像对象的主要接口是IPict ...

  9. boost::asio 使用 libcurl

    curl 使用 asio 的官方样例 http://curl.haxx.se/libcurl/c/asiohiper.html, 但这个例子用起来有很明细的 bug,asio 异步IO 只注册一次,也 ...

  10. oracle error info

    1,oracle jdbc HTTP Status 500 - Incorrect result size: expected 1, actual 0 2015-03-31 00:03:58,250 ...