以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  从EXCEL中提取数据  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=117636)

--  作者:nxqtxwz
--  发布时间:2018/4/16 16:01:00
--  从EXCEL中提取数据

FOXTABLE表的第一列是姓名, EXCEL也有姓名列,想按FOXTABLE姓名列的姓名从EXCEL中提取相同姓名的人员信息写入FOXTABLE的指定列中。请问能代码如何写呢?


--  作者:有点甜
--  发布时间:2018/4/16 16:03:00
--  

参考

 

http://www.foxtable.com/webhelp/scr/2334.htm

 

http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=101056&skin=0

 


--  作者:nxqtxwz
--  发布时间:2018/4/16 16:46:00
--  
 

为什么不行呢?显示执行0行。

 

Dim dlg As New OpenFileDialog

dlg.Filter = "Excel文件|*.xls;*.xlsx"

If dlg.ShowDialog =DialogResult.OK Then

    Dim t As Table = Tables("账号密码")

    t.StopRedraw()

    Dim Book As New XLS.Book(dlg.FileName)

    Dim Sheet As XLS.Sheet = Book.Sheets(1)

    Dim newcount As Integer = 0

    Dim oldcount As Integer = 0

    For n As Integer = 1 To Sheet.Rows.Count -1

        Dim r As DataRow = t.DataTable.Find("姓名 = \'" & sheet(n, 0).text & "\'")

        If r Is Nothing Then

            r = t.DataTable.AddNew()

            newcount += 1

        Else

            oldcount += 1

        End If

        For i As Integer = 0 To sheet.Cols.Count -1

            Dim cname As String = sheet(0, i).text

            If t.Cols.Contains(cname) Then

                r(cname) = sheet(n, i).Text

            End If

        Next

    Next

    msgbox("新增" & newcount & "    " & "更新旧数据" & oldcount)

    t.ResumeRedraw()

End If


--  作者:有点甜
--  发布时间:2018/4/16 17:17:00
--  

是不是你表名弄成了?

 

Dim dlg As New OpenFileDialog

dlg.Filter = "Excel文件|*.xls;*.xlsx"

If dlg.ShowDialog =DialogResult.OK Then

    Dim t As Table = Tables("账号密码")

    t.StopRedraw()

    Dim Book As New XLS.Book(dlg.FileName)

    Dim Sheet As XLS.Sheet = Book.Sheets(0) \'或者 Book.Sheets("sheet1")

    Dim newcount As Integer = 0

    Dim oldcount As Integer = 0

    For n As Integer = 1 To Sheet.Rows.Count -1

        Dim r As DataRow = t.DataTable.Find("姓名 = \'" & sheet(n, 0).text & "\'")

        If r Is Nothing Then

            r = t.DataTable.AddNew()

            newcount += 1

        Else

            oldcount += 1

        End If

        For i As Integer = 0 To sheet.Cols.Count -1

            Dim cname As String = sheet(0, i).text

            If t.Cols.Contains(cname) Then

                r(cname) = sheet(n, i).Text

            End If

        Next

    Next

    msgbox("新增" & newcount & "    " & "更新旧数据" & oldcount)

    t.ResumeRedraw()

End If

[此贴子已经被作者于2018/4/16 17:17:22编辑过]

--  作者:nxqtxwz
--  发布时间:2018/4/18 14:08:00
--  
请问列怎么控制,也就是要把EXCEL的AB列的内容写入表A的KK列,怎么办?
--  作者:有点甜
--  发布时间:2018/4/18 14:24:00
--  

参考这种做法

 

http://www.foxtable.com/webhelp/scr/2334.htm

 

或者

 

        For i As Integer = 0 To sheet.Cols.Count -1

            Dim cname As String = sheet(0, i).text

            If t.Cols.Contains(cname) Then

                r(cname) = sheet(n, i).Text

            ElseIf cname = "KK" Then

                r("ab") = sheet(n, i).Text

            End If

        Next