C#读取Excel表中的名字 如:Sheet1
来源:互联网 发布:淘宝海报图片男装 编辑:程序博客网 时间:2024/06/01 07:33
--返回Excel表中的名称(传入路径)
private static string GetSheetName(string filePath)
{
string sheetName = "";
System.IO.FileStream tmpStream = File.OpenRead(filePath);
byte[] fileByte = new byte[tmpStream.Length];
tmpStream.Read(fileByte, 0, fileByte.Length);
tmpStream.Close();
byte[] tmpByte = new byte[]{Convert.ToByte(11),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),
Convert.ToByte(11),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),Convert.ToByte(0),
Convert.ToByte(30),Convert.ToByte(16),Convert.ToByte(0),Convert.ToByte(0)};
int index = GetSheetIndex(fileByte, tmpByte);
if (index > -1)
{
index += 16 + 12;
System.Collections.ArrayList sheetNameList = new System.Collections.ArrayList();
for (int i = index; i < fileByte.Length - 1; i++)
{
byte temp = fileByte[i];
if (temp != Convert.ToByte(0))
sheetNameList.Add(temp);
else
break;
}
byte[] sheetNameByte = new byte[sheetNameList.Count];
for (int i = 0; i < sheetNameList.Count; i++)
sheetNameByte[i] = Convert.ToByte(sheetNameList[i]);
sheetName = System.Text.Encoding.Default.GetString(sheetNameByte);
}
return sheetName;
}
/// <summary>
/// 只供方法GetSheetName()使用
/// </summary>
/// <returns></returns>
private static int GetSheetIndex(byte[] FindTarget, byte[] FindItem)
{
int index = -1;
int FindItemLength = FindItem.Length;
if (FindItemLength < 1) return -1;
int FindTargetLength = FindTarget.Length;
if ((FindTargetLength - 1) < FindItemLength) return -1;
for (int i = FindTargetLength - FindItemLength - 1; i > -1; i--)
{
System.Collections.ArrayList tmpList = new System.Collections.ArrayList();
int find = 0;
for (int j = 0; j < FindItemLength; j++)
{
if (FindTarget[i + j] == FindItem[j]) find += 1;
}
if (find == FindItemLength)
{
index = i;
break;
}
}
return index;
}
}
有上可得到 sheetName
string strCom = "SELECT * FROM [" + sheetName + "$]";
- C#读取Excel表中的名字 如:Sheet1
- C#读取EXCEL中工作表不是Sheet1等默认名称
- C#读取 excel中的表名sheet名不是默认的sheet1,shee2就取不到值了 c# 如何读取自定义的sheet值?c#读取excel最佳解决方案
- C#读取excel中的数据
- C# 读取Excel中的数据
- C#读取Excel中的数据
- C#读取excel表
- C# 读取excel表
- 将工作表Sheet1的内容读取到DataSet
- 如何用VC读取Excel表格中的数据详细介绍
- 如何用VC读取Excel表格中的数据详细介绍
- [C#新手入门 九] C#读取Excel表中的数据时,有的数据读取不到
- C#读取EXCEL中的数据(2)
- 使用c#读取excel中的数据
- C# 读取 Excel 中的合并单元格内容。
- C# 读取Excel表格中的内容
- 【C#】C#读取Excel中的数据
- C# 读取Excel工作表
- 解决Apache日志文件ACCESS.LOG日益膨胀的一个办法
- Spreadsheet::ParseExcel::SaveParser 和 Spreadsheet::ParseExcel的内存问题
- 美国青年:整个中国就是一个山寨
- 2009-02-11
- 从debug版本的NK启动信息了解wince的启动流程
- C#读取Excel表中的名字 如:Sheet1
- 《Agile Software Development》关于Design Principle阅读笔记
- 成功软件项目的七个关键因素
- 如何在MySQL数据库中定义外键
- 使用 OpenRowSet 和 OpenDataSource 访问 Excel 97-2007
- 8 ways to be a better programmer in 6 minutes.
- code complete(1)
- 各个VFP运行库
- EJB注入后的空指针异常(NullPointerException)解决办法