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: ...
随机推荐
- PlSql复制角色、权限和添加角色权限
一.登录你想要复制数据库的用户
- linux安装chrome
wget http://chrome.richardlloyd.org.uk/install_chrome.sh chmod u+x install_chrome.sh ./install_chrom ...
- ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别
一:ContextLoaderListener加载内容 二:DispatcherServlt加载内容 ContextLoaderListener和DispatcherServlet都会在Web容器启动 ...
- 在controller间分享数据(第一种办法)
Blood 血腥的方法 每个controller都有自己的scope, 同时也可以共享他们老爸的scope内的数据.如果我们想让两个controller共享数据的化, 有多种方法. 最直接血腥的就是在 ...
- JavaScript总结之单击弹出div
今天也算用了不少手段来实现他们的要求,大概记录一下,下边的代码示例,我全部修改贴出来,争取全部占到自己的代码里就能用. 1.点击同一个div,打开/关闭另一个div. 1 <script typ ...
- spring postconstruct
package com.jdw.service.impl; import java.util.List; import javax.annotation.PostConstruct; import o ...
- MYSQL 插入二进制数的 2 种方法。
方法 1.insert into TableName set column =''; 方法 2.insert into TableName .... values(.....); ---------- ...
- SQL 必备- ORACLE-SQSLSERVER-DB2时间函数及常见函数总结
SQLSERVER 篇: 一.时间函数 --getdate 获取当前时间 select getdate() --dateadd 原有时间加: 2013-02-17 13:20:16 此时间加12个月 ...
- Go语言(golang)开源项目大全
转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析 ...
- ISSkin Pack 3.0 (含预览程序 ISSkinViewer)
原文 http://restools.hanzify.org/article.asp?id=108 包含 250 款皮肤, 预览程序纯粹的由Inno Setup脚本写成. 足以展示Inno Setup ...