首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-12-14 
关于C#读取Excel表中sheet1为任意名字的问题希望读取excel文件的sheet1,sheet1可以为任意名字,参考了一个

关于C#读取Excel表中sheet1为任意名字的问题
希望读取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;
        }
[最优解释]
....没这么麻烦吧,给你个类,直接来获取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;
            }
        }
    }
}


[其他解释]
这个是Excel的限制,不是程序的限制。自己在Excel改个看看。
[其他解释]
you can have a loot at about npoi
http://npoi.codeplex.com/
http://tonyqus.sinaapp.com/ 
http://www.cnblogs.com/tonyqus/category/182110.html
[其他解释]
引用:
引用: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 截取限制字符数以内。

没明白你卡在什么地方?
[其他解释]
引用:

这个是Excel的限制,不是程序的限制。自己在Excel改个看看。

没错,就是这个原因。
[其他解释]
引用:
....没这么麻烦吧,给你个类,直接来获取sheet名,然后......你应该搞的定了
我用你的sheet1的名字是“ArgosData_2012_11_23_08_32_09”试过,没问题


C# code?



123456789101112131415161718192021222324252627282930313233343536373839404142434445
……

[其他解释]
不知道是不是 tmpByte 这里定义的时候限制了长度,改长了以后,运行还是有问题。继续求助
[其他解释]
求助帮忙解答下
[其他解释]
引用:
这个是Excel的限制,不是程序的限制。自己在Excel改个看看。
我把原来的名字过长的sheet1删掉后面几个字符后就可以运行了。这样的话还是excel的问题么?
[其他解释]
引用:
you can have a loot at about npoi http://npoi.codeplex.com/ http://tonyqus.sinaapp.com/  http://www.cnblogs.com/tonyqus/category/182110.html
看了下你这个,先谢过了,不过我这个暂时不需要那么高级的功能,就是希望对现有程序进行简单的修改然后搞定,不知道可以帮忙看看么
[其他解释]
引用:
引用:引用: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”就可以顺利打开,名字长出来几个字符就会报错,不太清楚该修改哪里。谢谢帮助,新手见谅!
[其他解释]
引用:
....没这么麻烦吧,给你个类,直接来获取sheet名,然后......你应该搞的定了
我用你的sheet1的名字是“ArgosData_2012_11_23_08_32_09”试过,没问题
C# code?123456789101112131415161718192021222324252627282930313233343536373839404142434445……
可以留个联系方式具体请教下么
[其他解释]
利用反射获取表名吧,楼上有代码了

热点排行