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();
}
C#获取Excel Sheet名称,对特殊字符、重名进行了处理的更多相关文章
- C#中获取Excel文件的第一个表名
// 2.以数据库方式打开并输入数据// 此方式将xls文件所在目录看作数据库,其中的xls文件看作数据库表,表名即文件名(不加扩展名).// 函数importExcelTo ...
- 获取Excel表中各个Sheet的方法
获取Excel表中各个Sheet的方法 private void simpleButton2_Click(object sender, EventArgs e) { OfdBOM.Filter = & ...
- 利用POI获取Excel中图片和图片位置
利用POI获取Excel中图片和图片位置(支持excel2003or2007多sheet) 转自:http://blog.csdn.net/delongcpp/article/details/8833 ...
- VB6.0 获取Excel文件工作表Sheet的名称
获取Excel文件工作表Sheet的名称 '产生Excel文档 Dim xlapp, xlbook As Object Dim sSheetName As String Set xlapp = Cre ...
- 怎么利用Aspose.Cells 获取excel 数据表中sheet的名称
说明:开发环境 vs2012 asp.net mvc4 c# 利用Aspose.Cells 获取Excel数据表的sheet的名称,并把获取的名称赋值给easyUI 的combobox 1.运行效果 ...
- C#获取Excel中所有的Sheet名称
原文地址:http://blog.csdn.net/qq1010726055/article/details/6858849 Excel.Application myExcel = new Excel ...
- python excel练习:新建sheet、修改名称、设定颜色、打印sheet名称,复制,保存
练习: 新建一个sheet 设定一个sheet的插入位置 修改sheet的名称为‘xiaxiaoxu’ 设定该sheet的背景标签的颜色 获取全部sheet的名称,打印每个sheet的名称 copy一 ...
- 获取Excel工作薄中Sheet页(工作表)名集合
#region 获取Excel工作薄中Sheet页(工作表)名集合 02./// <summary> 03./// 获取Excel工作薄中Sheet页(工作表)名集合 04./// < ...
- C#可以获取Excel文件中Sheet的名字
C#可以获取Excel文件中Sheet的名字吗 C#可以获取Excel文件中Sheet的名字吗 我试过WPS的表格可以 可以 要代码么 百度都有 [深圳]Milen(99696619) 14:13: ...
随机推荐
- UITableView 隐藏多余的分割线
//隐藏多余的分割线 - (void)setExtraCellLineHidden: (UITableView *)tableView { UIView *view =[ [UIView alloc] ...
- mysql 时间戳按指定格式(Y-m-d)取出
之前做采集脚本,把采集的时间按unix时间戳的形式取出 那么在写sql语句的时候,需要按时间查询相应的记录,页面传进来的$time 是'2014-01'之类的字符串,那么怎么写sql呢 $sql ...
- JS delete 用法(删除对象属性及变量)
1,对象属性删除 function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.name);//mm delete ...
- AngularJS 深入理解 $scope 转载▼
AngularJS 深入理解 $scope 转载▼ (2015-04-07 14:09:50) $scope 的使用贯穿整个 AngularJS App 应用,它与数据模型相关联,同时也是表达 ...
- 关于json对象的遍历
json格式的数据越来越多的在web开发中起到重要作用.下面介绍对于json对象和数组经常用到解析方法. var obj ={”name”:”冯娟”,”password”:”123456″,”depa ...
- XML新手入门 创建构造良好的XML(2)
本文描述了构建良好的XML需要遵循的规则.作者详细介绍了构建XML需要考虑的元素,如何命名约定.正确的标记嵌套.属性规则.声明和实体,以及DTD和schema的验证,十分便于新手开始学习了解XML. ...
- AlertDialog与DialogFragment
1.AlertDialog 作用:简单的弹出框实现 创建方法: AlertDialog alert = new AlertDialog.Builder(); 使用: new AlertDialog.B ...
- 一段phpcurl代码
$header[] = 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-fl ...
- Leetcode 100 Same Tree python
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- python核心编程-第四章-习题
1.身份.类型.值.其中,身份是每个对象的标识,与内存地址密切相关,可用id()返回:类型决定了对象可以保存什么类型的值,用type()函数.isinstance()函数可以得到对象的类型:值就是对象 ...