各位大哥大姐求教一下Excel导入到了Access但datagridview上不能显示?
Private Sub 数据导入ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 数据导入ToolStripMenuItem.ClickDim dresult As DialogResult = Me.openFileDialog1.ShowDialog()
If dresult = System.Windows.Forms.DialogResult.OK Then
Dim filepath As String = openFileDialog1.FileName
If filepath = "" Then
MessageBox.Show("请选择要导入的Excel文档!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
Else
Dim ds As New DataSet()
'取得数据集
'调用下面的函数
ds = xsldata(filepath)
'dataGridView2.DataSource = ds.Tables[0];
Dim errorcount As Integer = 0 '记录错误信息条数
Dim updatecount As Integer = 0 '更新信息条数
Dim insertcount As Integer = 0 '记录插入成功条数
Dim mess As String = ""
Dim i As Integer
For i = 0 To (ds.Tables(0).Rows.Count) - 1
Dim mc As String = ds.Tables(0).Rows(i).Item(0).ToString '读取每一行的第一列数据
Dim kbc As Double = CType(ds.Tables(0).Rows(i).Item(1), Double)
Dim hsc As Double = CType(ds.Tables(0).Rows(i).Item(2), Double)
Dim shtc As Double = CType(ds.Tables(0).Rows(i).Item(3), Double)
Dim ysc As Double = CType(ds.Tables(0).Rows(i).Item(4), Double)
Dim djc As Double = CType(ds.Tables(0).Rows(i).Item(5), Double)
Dim stc As Double = CType(ds.Tables(0).Rows(i).Item(6), Double)
Dim dzc As Double = CType(ds.Tables(0).Rows(i).Item(7), Double)
Dim hhc As Double = CType(ds.Tables(0).Rows(i).Item(8), Double)
Dim dhc As Double = CType(ds.Tables(0).Rows(i).Item(9), Double)
Dim zjc As Double = CType(ds.Tables(0).Rows(i).Item(10), Double)
Dim yx As Double = CType(ds.Tables(0).Rows(i).Item(11), Double)
Dim bgzl As Double = CType(ds.Tables(0).Rows(i).Item(12), Double)
Dim gjzs As Double = CType(ds.Tables(0).Rows(i).Item(13), Double)
Dim kxb As Double = CType(ds.Tables(0).Rows(i).Item(14).ToString, Double)
Dim yxzs As Double = CType(ds.Tables(0).Rows(i).Item(15).ToString, Double)
Dim Str As String = "insert into zuankong(名称,空白层,含水层,砂土层,岩石层,堆积层,死土层,地质层,火化层,地核层,中间层,岩性,比贯入阻力,标贯击数,孔隙比,液性指数) Values ('" + mc + "','" & Trim(kbc) & "','" & Trim(hsc) & "','" & Trim(shtc) & "','" & Trim(ysc) & "','" & Trim(djc) & "','" & Trim(stc) & "','" & Trim(dzc) & "','" & Trim(hhc) & "','" & Trim(dhc) & "','" & Trim(zjc) & "','" & Trim(yx) & "','" & Trim(bgzl) & "','" & Trim(gjzs) & "','" & Trim(kxb) & "','" & Trim(yxzs) & "') "
Dim connection As OleDb.OleDbConnection = New OleDb.OleDbConnection
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & System.Windows.Forms.Application.StartupPath & "\zkshujuku.mdb;"
Dim shujubiaoadapter As New OleDb.OleDbDataAdapter
shujubiaoadapter.SelectCommand = New OleDb.OleDbCommand(Str, connection)
Dim builder As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(shujubiaoadapter)
shujubiaoadapter. = Str
connection.Open()
shujubiaoadapter.Fill(ZkshujukuDataSet, "zuankong")
DataGridView1.DataSource = ZkshujukuDataSet.Tables("zuankong")
Dim MyTable As New System.Data.DataTable()
MyTable.Clear()
shujubiaoadapter.Fill(MyTable)
DataGridView1.DataSource = MyTable
Next
End If
End If
End Sub
Private Function xsldata(ByVal filepath As String) As DataSet
Dim strCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'"
Dim Conn As New System.Data.OleDb.OleDbConnection(strCon)
Dim strCom As String = "SELECT * FROM [Sheet1$]"
Conn.Open()
Dim myCommand As New System.Data.OleDb.OleDbDataAdapter(strCom, Conn)
Dim ds As New DataSet()
myCommand.Fill(ds, "[Sheet1$]")
Conn.Close()
Return ds
End Function 'xsldata
上面可以实现Excel导入到Access功能,但是导入成功后DataGridView控件上就变成空白了,不能显示数据表?本来DataGridView上还显示原来的数据表,插入这些数据后就什么不显示了!
最近发帖频繁 没有分了 不知道这个能不能增加悬赏 如果我下次有分了,一定补上 呵呵呵