关于C#读取Excel表中sheet1为任意名字的问题

dvdlim 2012-11-26 01:27:22
希望读取excel文件的sheet1,sheet1可以为任意名字,参考了一个如下的代码,可以实现。但是当sheet1的名字过长的时候就报错了,请教一下修改哪里可以使读取35个字符以内的sheet1名称。新手编程,谢谢了
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;
}
...全文
405 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
机器人 2012-11-27
  • 打赏
  • 举报
回复
引用 6 楼 dvdlim 的回复:
引用 5 楼 liulanghe 的回复:you can have a loot at about npoi http://npoi.codeplex.com/ http://tonyqus.sinaapp.com/ http://www.cnblogs.com/tonyqus/category/182110.html看了下你这个,先谢过了,不过我这个暂时不需要那么高级……
自己在 return sheetName 用 SubString 截取限制字符数以内。 没明白你卡在什么地方?
TobeNotTobe 2012-11-27
  • 打赏
  • 举报
回复
利用反射获取表名吧,楼上有代码了
dvdlim 2012-11-27
  • 打赏
  • 举报
回复
引用 10 楼 jh56864591a 的回复:
....没这么麻烦吧,给你个类,直接来获取sheet名,然后......你应该搞的定了 我用你的sheet1的名字是“ArgosData_2012_11_23_08_32_09”试过,没问题 C# code?123456789101112131415161718192021222324252627282930313233343536373839404142434445……
可以留个联系方式具体请教下么
  • 打赏
  • 举报
回复
引用 10 楼 jh56864591a 的回复:
....没这么麻烦吧,给你个类,直接来获取sheet名,然后......你应该搞的定了 我用你的sheet1的名字是“ArgosData_2012_11_23_08_32_09”试过,没问题 C# code? 123456789101112131415161718192021222324252627282930313233343536373839404142434445 ……
jh56864591a 2012-11-27
  • 打赏
  • 举报
回复
....没这么麻烦吧,给你个类,直接来获取sheet名,然后......你应该搞的定了 我用你的sheet1的名字是“ArgosData_2012_11_23_08_32_09”试过,没问题
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.OleDb;
using System.Runtime;
using System.IO;

namespace TXRECE
{

    /// <summary>
    /// 获取Excel中表的名称
    /// </summary>
    /// <param name="p_ExcelFile">Excel的路径</param>
    /// <returns></returns>
    class sheet
    {
        public static DataTable GetExcelTableName(string p_ExcelFile)
        {
            try
            {
                if (System.IO.File.Exists(p_ExcelFile))
                {
                    OleDbConnection _ExcelConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + p_ExcelFile);
                    _ExcelConn.Open();
                    DataTable _Table = _ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    _ExcelConn.Close();
                    return _Table;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
    }
}
dvdlim 2012-11-27
  • 打赏
  • 举报
回复
引用 7 楼 fangxinggood 的回复:
引用 6 楼 dvdlim 的回复:引用 5 楼 liulanghe 的回复:you can have a loot at about npoi http://npoi.codeplex.com/ http://tonyqus.sinaapp.com/ http://www.cnblogs.com/tonyqus/category/182110.html看了下你这个,先……
是这样的,我的目标excel的sheet1的名字是“ArgosData_2012_11_23_08_32_09”,使用这个程序无法打开,会提示'$' 不是一个有效名称。请确认它不包含无效的字符或标点,且名称不太长。 但是如果把名字改成“ArgosData_2012_11_23_08_32”就可以顺利打开,名字长出来几个字符就会报错,不太清楚该修改哪里。谢谢帮助,新手见谅!
devmiao 2012-11-27
  • 打赏
  • 举报
回复
引用 3 楼 fangxinggood 的回复:
这个是Excel的限制,不是程序的限制。自己在Excel改个看看。
没错,就是这个原因。
dvdlim 2012-11-26
  • 打赏
  • 举报
回复
引用 5 楼 liulanghe 的回复:
you can have a loot at about npoi http://npoi.codeplex.com/ http://tonyqus.sinaapp.com/ http://www.cnblogs.com/tonyqus/category/182110.html
看了下你这个,先谢过了,不过我这个暂时不需要那么高级的功能,就是希望对现有程序进行简单的修改然后搞定,不知道可以帮忙看看么
流浪河 2012-11-26
  • 打赏
  • 举报
回复
you can have a loot at about npoi http://npoi.codeplex.com/ http://tonyqus.sinaapp.com/ http://www.cnblogs.com/tonyqus/category/182110.html
dvdlim 2012-11-26
  • 打赏
  • 举报
回复
引用 3 楼 fangxinggood 的回复:
这个是Excel的限制,不是程序的限制。自己在Excel改个看看。
我把原来的名字过长的sheet1删掉后面几个字符后就可以运行了。这样的话还是excel的问题么?
机器人 2012-11-26
  • 打赏
  • 举报
回复
这个是Excel的限制,不是程序的限制。自己在Excel改个看看。
dvdlim 2012-11-26
  • 打赏
  • 举报
回复
求助帮忙解答下
dvdlim 2012-11-26
  • 打赏
  • 举报
回复
不知道是不是 tmpByte 这里定义的时候限制了长度,改长了以后,运行还是有问题。继续求助

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧