7 个解决方案
#1
建议你在SQLSERVER的导入功能里面生成一个DTS,这样就可以自动生成该DTS的VB的源代码,导入过程里面都有记录.将代码改成公共函数用就可以了.
#2
不好意思,我找不到你说的那个“SQLSERVER的导入功能里面生成一个DTS”,在哪里可以设一下呢?
#3
如果工作表名试不定的那么通过下面的方法可以取出来
这只是个例子,需要自己扩展一下
/// <summary>
/// 将指定Excel文件中读取第一张工作表的名称
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
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(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+=32+12;
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;
}
这只是个例子,需要自己扩展一下
/// <summary>
/// 将指定Excel文件中读取第一张工作表的名称
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
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(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+=32+12;
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;
}
#4
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strFile & ";" & "Extended Properties=Excel 8.0;"
Dim conn As OleDbConnection
Dim oda As OleDbDataAdapter
Dim commb As OleDbCommandBuilder
Dim dt As DataTable
Dim strSheetName As String '工作表的名称
Dim strComText As String 'sql语句
Dim Ds As DataSet '得到的数据集
conn = New OleDbConnection(strConn)
oda = New OleDbDataAdapter(strConn, conn)
conn.Open()
commb = New OleDbCommandBuilder(oda)
dt = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
将dt邦定到DropDownlist,TextField,ValueField应该是"TABLE"中就可以了
剩下的就不用说了吧
'以上为得到Excel中工作表的名字,表的第二列表示工作表的名称
strComText = "SELECT * FROM [" & strSheetName & "]"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strFile & ";" & "Extended Properties=Excel 8.0;"
Dim conn As OleDbConnection
Dim oda As OleDbDataAdapter
Dim commb As OleDbCommandBuilder
Dim dt As DataTable
Dim strSheetName As String '工作表的名称
Dim strComText As String 'sql语句
Dim Ds As DataSet '得到的数据集
conn = New OleDbConnection(strConn)
oda = New OleDbDataAdapter(strConn, conn)
conn.Open()
commb = New OleDbCommandBuilder(oda)
dt = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
将dt邦定到DropDownlist,TextField,ValueField应该是"TABLE"中就可以了
剩下的就不用说了吧
'以上为得到Excel中工作表的名字,表的第二列表示工作表的名称
strComText = "SELECT * FROM [" & strSheetName & "]"
#5
Excel.Application mex = new Excel.ApplicationClass();
Excel.Workbook mwb = mex.Workbooks.Open(@""+file.Value+"",Type.Missing ,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
Excel.Worksheet mws = (Excel.Worksheet)mwb.Sheets[1];
string name = mws.Name ;
mwb.Close(false,Type.Missing ,Type.Missing );
name就是工作表的名字
Excel.Workbook mwb = mex.Workbooks.Open(@""+file.Value+"",Type.Missing ,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
Excel.Worksheet mws = (Excel.Worksheet)mwb.Sheets[1];
string name = mws.Name ;
mwb.Close(false,Type.Missing ,Type.Missing );
name就是工作表的名字
#6
楼上的完全不对,是生成Excel抓取名字,和楼主需要的大相径庭。
Eddie005(暴走005) 可以参考一下。
Eddie005(暴走005) 可以参考一下。
#7
参考这篇文章--
Retrieve Excel Workbook Sheet Names:
http://www.codeproject.com/aspnet/getsheetnames.asp
Retrieve Excel Workbook Sheet Names:
http://www.codeproject.com/aspnet/getsheetnames.asp
#1
建议你在SQLSERVER的导入功能里面生成一个DTS,这样就可以自动生成该DTS的VB的源代码,导入过程里面都有记录.将代码改成公共函数用就可以了.
#2
不好意思,我找不到你说的那个“SQLSERVER的导入功能里面生成一个DTS”,在哪里可以设一下呢?
#3
如果工作表名试不定的那么通过下面的方法可以取出来
这只是个例子,需要自己扩展一下
/// <summary>
/// 将指定Excel文件中读取第一张工作表的名称
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
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(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+=32+12;
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;
}
这只是个例子,需要自己扩展一下
/// <summary>
/// 将指定Excel文件中读取第一张工作表的名称
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
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(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+=32+12;
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;
}
#4
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strFile & ";" & "Extended Properties=Excel 8.0;"
Dim conn As OleDbConnection
Dim oda As OleDbDataAdapter
Dim commb As OleDbCommandBuilder
Dim dt As DataTable
Dim strSheetName As String '工作表的名称
Dim strComText As String 'sql语句
Dim Ds As DataSet '得到的数据集
conn = New OleDbConnection(strConn)
oda = New OleDbDataAdapter(strConn, conn)
conn.Open()
commb = New OleDbCommandBuilder(oda)
dt = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
将dt邦定到DropDownlist,TextField,ValueField应该是"TABLE"中就可以了
剩下的就不用说了吧
'以上为得到Excel中工作表的名字,表的第二列表示工作表的名称
strComText = "SELECT * FROM [" & strSheetName & "]"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & strFile & ";" & "Extended Properties=Excel 8.0;"
Dim conn As OleDbConnection
Dim oda As OleDbDataAdapter
Dim commb As OleDbCommandBuilder
Dim dt As DataTable
Dim strSheetName As String '工作表的名称
Dim strComText As String 'sql语句
Dim Ds As DataSet '得到的数据集
conn = New OleDbConnection(strConn)
oda = New OleDbDataAdapter(strConn, conn)
conn.Open()
commb = New OleDbCommandBuilder(oda)
dt = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
将dt邦定到DropDownlist,TextField,ValueField应该是"TABLE"中就可以了
剩下的就不用说了吧
'以上为得到Excel中工作表的名字,表的第二列表示工作表的名称
strComText = "SELECT * FROM [" & strSheetName & "]"
#5
Excel.Application mex = new Excel.ApplicationClass();
Excel.Workbook mwb = mex.Workbooks.Open(@""+file.Value+"",Type.Missing ,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
Excel.Worksheet mws = (Excel.Worksheet)mwb.Sheets[1];
string name = mws.Name ;
mwb.Close(false,Type.Missing ,Type.Missing );
name就是工作表的名字
Excel.Workbook mwb = mex.Workbooks.Open(@""+file.Value+"",Type.Missing ,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
Excel.Worksheet mws = (Excel.Worksheet)mwb.Sheets[1];
string name = mws.Name ;
mwb.Close(false,Type.Missing ,Type.Missing );
name就是工作表的名字
#6
楼上的完全不对,是生成Excel抓取名字,和楼主需要的大相径庭。
Eddie005(暴走005) 可以参考一下。
Eddie005(暴走005) 可以参考一下。
#7
参考这篇文章--
Retrieve Excel Workbook Sheet Names:
http://www.codeproject.com/aspnet/getsheetnames.asp
Retrieve Excel Workbook Sheet Names:
http://www.codeproject.com/aspnet/getsheetnames.asp