| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1525 人关注过本帖
标题:各位大哥大姐求教一下Excel导入到了Access但datagridview上不能显示?
只看楼主 加入收藏
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
结帖率:100%
收藏
已结贴  问题点数:0 回复次数:13 
各位大哥大姐求教一下Excel导入到了Access但datagridview上不能显示?
Private Sub 数据导入ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 数据导入ToolStripMenuItem.Click
        Dim 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上还显示原来的数据表,插入这些数据后就什么不显示了!
最近发帖频繁 没有分了 不知道这个能不能增加悬赏  如果我下次有分了,一定补上  呵呵呵
搜索更多相关主题的帖子: Excel 大哥 
2011-07-06 15:13
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
还有为什么数据是成倍的增加呢?导入10个记录 增加了20个  看来我的确很菜  各位高手不要烦 为我排忧解难  谢谢
2011-07-06 16:40
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
收藏
得分:10 
把文件发上来,我看能不能帮到你

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2011-07-06 18:17
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
2011-07-06 18:40
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
回复 3楼 不说也罢
已经发了,在楼上,谢谢!
2011-07-06 18:40
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
收藏
得分:0 
拿去和你的代码做比较吧。你并没有读懂代码的逻辑关系。我是以你的解压目录下“要导入的数据.xls”为导入源进行导入的,共12条记录。下面的代码只是对datagridview1的数据进行更新,并不具备导入功能。——导入功能其实是在源数据中增加现在的数据中没有的记录。与你代码比对下,研究下为什么删了一些你的代码
程序代码:
'将下面的代码替换对应的代码即可。如果你将MDB表数据清空后,再按下这个按钮,你就更能感觉到了。你为什么会写那么多代码??
    Private Sub 数据导入ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 数据导入ToolStripMenuItem.Click
        Dim 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
            End If
            Dim ds As New DataSet()
            ds = xsldata(filepath)
            DataGridView1.DataSource = ds.Tables(0)
        End If
    End Sub


===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2011-07-06 20:21
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
回复 6楼 不说也罢
谢谢你啊  你还是帮我解决第二个问题吧,增加记录为何都是成倍的,比如12条记录,在数据库里就变成了24条,每条重复一次,至于你说的我不是太懂,我想最后DataGridView显示的是更新后的数据库里的数据!
2011-07-06 21:06
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
收藏
得分:0 
不可能给你完全的代码,这样会害你的。想编程就仔细琢磨哦。仔细理解每一行代码的意思。
你没有认真看我的回复:如果你将MDB表数据清空后,再按下这个按钮,你就更能感觉到了————————你这样做了吗?

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2011-07-06 21:20
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
回复 8楼 不说也罢
我也知道直接要代码不好,我不知道你要我感觉什么 我的悟性比较差,我看这些代码看半天也没看出个所以然 我以前一点编程基础也没有 我最后也不是有DataGridView1.DataSource = MyTable吗 为什么不显示呢  为什么会多增加一倍的重复记录  我一点头绪都没有  我这几天天天看代码 头都大了 老板(导师)还催着要,他自己不懂编程,还以为编程很简单,每次都怀疑我偷懒!O__O"…
2011-07-06 22:50
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
回复 8楼 不说也罢
  我现在找到症结了 谢谢你啊  下面是我修改后的代码
 Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection
                    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & System.Windows.Forms.Application.StartupPath & "\zkshujuku.mdb;"
                    Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
                    conn.Open() '打开连接
                    cmd.Connection = conn
                    Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(Str, conn)
                    da.Fill(ZkshujukuDataSet, "zuankong")
                    DataGridView1.DataSource = ZkshujukuDataSet.Tables(0) '更换datagridview1的数据源
                    DataGridView1.Refresh()
但还有一个问题 就是dataGridView上的记录还是不变 显示的还是原来的 BindingNavigator控件上的记录数当然也保持不变 怎么做才能增加数据记录后控件上的记录数也跟着增加呢?

[ 本帖最后由 zhangzhen 于 2011-7-7 21:36 编辑 ]
2011-07-07 21:28
快速回复:各位大哥大姐求教一下Excel导入到了Access但datagridview上不能显示?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036198 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved