C#获取Excel Sheet名称,对特殊字符、重名进行了处理

/// <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();
        }

时间: 2024-10-15 20:31:51

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

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

//    2.以数据库方式打开并输入数据//      此方式将xls文件所在目录看作数据库,其中的xls文件看作数据库表,表名即文件名(不加扩展名).//      函数importExcelToDataSet(string FilePath,string sheet)功能:从xls中读出数据到DataSet中,并返回DataSet对象. private DataSet importExcelToDataSet(string FilePath/*即文件目录的路径*/, string shee

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

利用POI获取Excel中图片和图片位置(支持excel2003or2007多sheet) 转自:http://blog.csdn.net/delongcpp/article/details/8833995 第三方JAR包(apache下载POI即可): poi-3.9-20121203.jar dom4j-1.6.1.jar poi-ooxml-3.9-20121203.jar poi-ooxml-schemas-3.9-20121203.jar poi-scratchpad-3.9-2012

C#获取Excel中所有的Sheet名称《转》以备忘

Excel.Application myExcel = new Excel.Application();object missing = System.Reflection.Missing.Value;myExcel.Application.Workbooks.Open(this.txtFile.Text, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missi

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

#region 获取Excel工作薄中Sheet页(工作表)名集合 /// <summary> /// 获取Excel工作薄中Sheet页(工作表)名集合 /// </summary> /// <param name="excelFile">Excel文件名及路径,EG:C:\Users\JK\Desktop\导入测试.xls</param> /// <returns>Sheet页名称集合</returns> pr

js兼容ie获取上传excel文件名称以及大小,绝对路径

/**  *   * @param obj file对象 document.getElementById(elementId);  * @returns  */ function getExcelFileFullPath(obj){ if (obj){ // ie if (window.navigator.userAgent.indexOf("MSIE") >= 1){ obj.select(); return document.selection.createRange().t

C#获取Excel表格所有sheet名(Epplus)

原文:C#获取Excel表格所有sheet名(Epplus) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/birdfly2015/article/details/90723215 欢迎加入BIM行业开发交流1群 群号:711844216 一.背景 小伙伴们在C#中使用Excel表格时,可能需要将去获取Excel所有Sheet名 二.思路 1.组件:Epplus epplus组件的安装使

[LeetCode] Excel Sheet Column Title 求Excel表列名称

Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB Credits:Special thanks to @ifanchu for adding this problem and creating all test

【LeetCode】Excel Sheet Column Number

题意: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 思路: 其实就是个进制转换.水水就过.倒是 Python 的代码让我

获取Excel部分数据并很据项目要求计算适宜性等级综合指数判断该地区的土壤适宜性

代码运行前请先导入jxl架包,以下代码仅供学习参考: 下图为项目中的Excel: ExcelTest02类代码如下: // 读取Excel的类 import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; public class ExcelTest02 { /* *该代码需要先获得exce