Foxtable(狐表)用户栏目专家坐堂 → [原创]初学者问下Excel导入代码优化


  共有3023人关注过本帖树形打印复制链接

主题:[原创]初学者问下Excel导入代码优化

帅哥哟,离线,有人找我吗?
hrlong
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:幼狐 帖子:80 积分:924 威望:0 精华:0 注册:2018/2/9 21:32:00
[原创]初学者问下Excel导入代码优化  发帖心情 Post By:2018/2/19 23:14:00 [只看该作者]

以下代码的excel文件是在固定位置的,
请问各位老师
怎么把代码优化成
Dim dlg As New OpenFileDialog
dlg.Filter = "Excel文件|*.xls;*.xlsx"
类似这样的 打开电脑任意位置的excel文件的模式呢


------------------------------------------------------------------------------
Dim Book As New XLS.Book("c:\test\订单.xls")

Dim Sheet As XLS.Sheet = Book.Sheets(0)
Tables("订单").StopRedraw()
Dim nms1() As String = {"产品","客户","雇员"}
Dim nms2() As String = {"表列名产品","表列名客户","表列名雇员"}

For n As Integer = 1 To Sheet.Rows.Count -1
    Dim r As Row = Tables("订单").AddNew()
    For i As Integer = 0 To sheet.Cols.Count -1
        Dim idx As Integer = array.indexof(nms1, sheet(0,i).Text)
        If idx >= 0 Then
            r(nms2(idx)) = sheet(n, i).Text
        End If
    Next
Next
Tables("订单").ResumeRedraw()

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/2/21 18:00:00 [只看该作者]

Dim dlg As New OpenFileDialog
dlg.Filter = "Excel文件|*.xls;*.xlsx"
If dlg.ShowDialog = DialogResult.OK Then
    Dim Book As New XLS.Book(dlg.FileName)
    Dim Sheet As XLS.Sheet = Book.Sheets(0)
    Tables("订单").StopRedraw()
    Dim nms1() As String = {"产品","客户","雇员"}
    Dim nms2() As String = {"表列名产品","表列名客户","表列名雇员"}
    For n As Integer = 1 To Sheet.Rows.Count -1
        Dim r As Row = Tables("订单").AddNew()
        For i As Integer = 0 To sheet.Cols.Count -1
            Dim idx As Integer = array.indexof(nms1, sheet(0,i).Text)
            If idx >= 0 Then
                r(nms2(idx)) = sheet(n, i).Text
            End If
        Next
    Next
    Tables("订单").ResumeRedraw()
End If

 回到顶部