C#读取Excel,取得不同的sheet名称

ContractedBlock.gif ExpandedBlockStart.gif Code
--返回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 < 1return -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 + "$]"

转载于:https://www.cnblogs.com/ChuttySonic/archive/2009/10/25/1589455.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值