Dim fileName As String
Me.OpenFileDialog1.Filter = "Excle文件(*.xls)|*.xls"
If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
fileName = Me.OpenFileDialog1.FileName
'Excel的链接字符串
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;"
'将Excel的数据读取到DataSet中
Dim myDataset As New DataSet
Dim da As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn)
da.TableMappings.Add("Table", "ExcelTest")
Try
da.Fill(myDataset)
da.Update(myDataset.Tables(0))
myDataset.AcceptChanges()
MsgBox("导入成功")
Me.DataGridView1.DataSource = myDataset.Tables(0)
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End If
代码如上,我觉得没错啊,但是运行起来总说找不到表0?郁闷,请高手看下
6 个解决方案
#1
没怎么看懂,思路:
1、你的EXCEL表名是否为Sheet1?
如果正确,请看一下数据取出来是否正确?
2、我没看懂你导入ACCESS数据库的语句?不知道你的连接在哪里?ACCESS是否已经打开?
?da.Update(myDataset.Tables(0))
1、你的EXCEL表名是否为Sheet1?
如果正确,请看一下数据取出来是否正确?
2、我没看懂你导入ACCESS数据库的语句?不知道你的连接在哪里?ACCESS是否已经打开?
?da.Update(myDataset.Tables(0))
#2
我也不是太懂,你就照你的意思写一个吧。表明确实是Sheet1
至于说导入ACCESS的语句,我自己都不知道在哪里
#3
#4
跟踪看下myDataset.Tables.count的值
#5
看下我的:
Dim fileName As String
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim myDataset As New DataSet
Dim da As New OleDbDataAdapter
Me.OpenFileDialog1.Filter = "Excle文件(*.xls)|*.xls"
If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
fileName = Me.OpenFileDialog1.FileName
'Excel的链接字符串
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;"
'将Excel的数据读取到DataSet中
cn.ConnectionString = strConn
cm.CommandText = "SELECT * FROM [Sheet1$]"
cm.Connection = cn
da.SelectCommand = cm
myDataset.Tables.Clear()
myDataset.Tables.Add()
cn.Open()
Try
da.Fill(myDataset.Tables(0))
MsgBox("导入成功")
Me.DataGridView1.DataSource = myDataset.Tables(0)
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
cn.Close()
Dim fileName As String
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim myDataset As New DataSet
Dim da As New OleDbDataAdapter
Me.OpenFileDialog1.Filter = "Excle文件(*.xls)|*.xls"
If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
fileName = Me.OpenFileDialog1.FileName
'Excel的链接字符串
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;"
'将Excel的数据读取到DataSet中
cn.ConnectionString = strConn
cm.CommandText = "SELECT * FROM [Sheet1$]"
cm.Connection = cn
da.SelectCommand = cm
myDataset.Tables.Clear()
myDataset.Tables.Add()
cn.Open()
Try
da.Fill(myDataset.Tables(0))
MsgBox("导入成功")
Me.DataGridView1.DataSource = myDataset.Tables(0)
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
cn.Close()
#6
你的代码也没错我能运行到结果。
#1
没怎么看懂,思路:
1、你的EXCEL表名是否为Sheet1?
如果正确,请看一下数据取出来是否正确?
2、我没看懂你导入ACCESS数据库的语句?不知道你的连接在哪里?ACCESS是否已经打开?
?da.Update(myDataset.Tables(0))
1、你的EXCEL表名是否为Sheet1?
如果正确,请看一下数据取出来是否正确?
2、我没看懂你导入ACCESS数据库的语句?不知道你的连接在哪里?ACCESS是否已经打开?
?da.Update(myDataset.Tables(0))
#2
我也不是太懂,你就照你的意思写一个吧。表明确实是Sheet1
至于说导入ACCESS的语句,我自己都不知道在哪里
#3
http://sxy.01ww.com/sxy01wwcom/ShowArticle.asp?ArticleID=189
http://topic.csdn.net/u/20081216/20/CBD1AEE0-BA6C-4268-B19B-0764A33CC191.html
参考
http://topic.csdn.net/u/20081216/20/CBD1AEE0-BA6C-4268-B19B-0764A33CC191.html
参考
#4
跟踪看下myDataset.Tables.count的值
#5
看下我的:
Dim fileName As String
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim myDataset As New DataSet
Dim da As New OleDbDataAdapter
Me.OpenFileDialog1.Filter = "Excle文件(*.xls)|*.xls"
If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
fileName = Me.OpenFileDialog1.FileName
'Excel的链接字符串
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;"
'将Excel的数据读取到DataSet中
cn.ConnectionString = strConn
cm.CommandText = "SELECT * FROM [Sheet1$]"
cm.Connection = cn
da.SelectCommand = cm
myDataset.Tables.Clear()
myDataset.Tables.Add()
cn.Open()
Try
da.Fill(myDataset.Tables(0))
MsgBox("导入成功")
Me.DataGridView1.DataSource = myDataset.Tables(0)
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
cn.Close()
Dim fileName As String
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim myDataset As New DataSet
Dim da As New OleDbDataAdapter
Me.OpenFileDialog1.Filter = "Excle文件(*.xls)|*.xls"
If Me.OpenFileDialog1.ShowDialog() = DialogResult.OK Then
fileName = Me.OpenFileDialog1.FileName
'Excel的链接字符串
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;"
'将Excel的数据读取到DataSet中
cn.ConnectionString = strConn
cm.CommandText = "SELECT * FROM [Sheet1$]"
cm.Connection = cn
da.SelectCommand = cm
myDataset.Tables.Clear()
myDataset.Tables.Add()
cn.Open()
Try
da.Fill(myDataset.Tables(0))
MsgBox("导入成功")
Me.DataGridView1.DataSource = myDataset.Tables(0)
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
cn.Close()
#6
你的代码也没错我能运行到结果。