Foxtable(狐表)用户栏目专家坐堂 → [求助]excel表格导入


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

主题:[求助]excel表格导入

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


加好友 发短信
等级:管理员 帖子:47497 积分:251403 威望:0 精华:91 注册:2008/6/17 17:14:00
  发帖心情 Post By:2013/4/2 14:16:00 [显示全部帖子]

Dim dlg As New OpenFileDialog
dlg.Filter= "Excel文件|*.xls;*.xlsx"
If dlg.ShowDialog = DialogResult.Ok Then
    'MessageBox.Show("你选择的是:" & dlg.FileName,"提示")
    Dim Book As New XLS.Book(dlg.FileName)
    Dim Sheet As XLS.Sheet = Book.Sheets(0)
    Dim colNames As New List(Of String)
    For i As Integer = 0 To sheet.Cols.Count - 1
        If sheet(0, i).Text > "" Then
            colNames.Add(sheet(0, i).Text)
        Else
            messagebox.show("Excel文件的第" & (i+1) & "列的列名为空,要罢工")
            Return
        End If
    Next
    Dim dt As DataTable = DataTables("机电设备表")
    For i As Integer = 1 To sheet.Rows.Count - 1
        If dt.Find("序号 = '" & sheet(i, 0).Text & "'") Is Nothing Then
            Dim ndr As DataRow = dt.AddNew
            For j As Integer = 0 To sheet.Cols.Count - 1
                ndr(colNames(j)) = sheet(i, j).Text
            Next
        End If
    Next
End If

 回到顶部