C#读取Excel,如果Excel表中的名字Sheet1 这个如果变了怎么办?
下面是写读取Excel的一段代码----------------------------------------------------------------strin...
下面是写读取Excel的一段代码
----------------------------------------------------------------
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = "+openFileDialog1.FileName.ToString()+";Extended Properties=Excel 8.0";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [Sheet1$] ";
myConn.Open();
-----------------------------------------------------------------
这里string strCom = " SELECT * FROM [Sheet1$] ";
Sheet1$这个名字假如变了
比如用户自己改成了 page1
或者用户把内容写在了 Sheet2$ 中
这里改怎么解决这几个问题呢??? 展开
----------------------------------------------------------------
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = "+openFileDialog1.FileName.ToString()+";Extended Properties=Excel 8.0";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [Sheet1$] ";
myConn.Open();
-----------------------------------------------------------------
这里string strCom = " SELECT * FROM [Sheet1$] ";
Sheet1$这个名字假如变了
比如用户自己改成了 page1
或者用户把内容写在了 Sheet2$ 中
这里改怎么解决这几个问题呢??? 展开
2个回答
展开全部
--返回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 + "$]";
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 + "$]";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询