Foxtable(狐表)用户栏目专家坐堂 → 如何将excel文件导入已有数据表


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

主题:如何将excel文件导入已有数据表

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


加好友 发短信
等级:小狐 帖子:376 积分:4189 威望:0 精华:0 注册:2008/9/1 14:18:00
  发帖心情 Post By:2012/5/17 10:39:00 [显示全部帖子]

DataTables("Testlist").DataRows.Clear()
Dim dlg As New OpenFileDialog
dlg.MultiSelect = True
dlg.Filter= "Excel 工作薄(*.xls)|*.xls|Excel 工作薄(*.xlsx)|*.xlsx" '设置筛选器
If dlg.ShowDialog = DialogResult.OK Then
    For Each fl As String In dlg.FileNames
        Dim Book As New XLS.Book(fl)
        Dim Sheet As XLS.Sheet = Book.Sheets(0)
        e.Form.Controls("Label1").text = FileSys.GetName(fl)
        Application.DoEvents()
        For i As Integer = 3 To Sheet.Rows.count - 1
            Dim dr1 As Row = Tables("Testlist").AddNew()
            For ii As Integer = 0 To Sheet.cols.count - 1
                dr1(Sheet(0,ii).value) = sheet(i,ii).Value
            Next
        Next
    Next
    e.Form.Controls("Label1").Text = "已经完成."
    MessageBox.Show("数据汇总完毕,请检查数据!", "汇总提示", MessageBoxButtons.OK, MessageBoxIcon.Question)
   
    Dim dlg1 As New SaveFileDialog '定义一个新的SaveFileDialog
    dlg1.Filter= "Excel文件|*.xls" '设置筛选器
    If dlg1.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
        Dim flg As New SaveExcelFlags
        flg.CellStyle = True
        flg.RowNumber =True
        CurrentTable.SaveExcel(dlg1.FileName,"Testlist",flg)
    End If
End If

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


加好友 发短信
等级:小狐 帖子:376 积分:4189 威望:0 精华:0 注册:2008/9/1 14:18:00
  发帖心情 Post By:2012/5/17 10:40:00 [显示全部帖子]

Dim dlg As New OpenFileDialog
dlg.MultiSelect = True
dlg.Filter= "Excel 工作薄(*.xls)|*.xls|Excel 工作薄(*.xlsx)|*.xlsx" '设置筛选器
If dlg.ShowDialog = DialogResult.OK Then
    For Each fl As String In dlg.FileNames
        Dim Book As New XLS.Book(fl)
        Dim Sheet As XLS.Sheet = Book.Sheets(0)
        Dim Names As New List(Of String)
        For ls As Integer = 0 To Sheet.Cols.count -1 '把目标表中的列名变成集合
            Names.Add(Sheet(0,ls).value)
        Next
        If Names.Contains("统编学籍号")= False Then
            MessageBox.Show(FileSys.GetName(fl) & "表:" & vbcrlf & "不含""统编学号""字段列", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question)
            Return
        Else
            e.Form.Controls("Label1").text = FileSys.GetName(fl)
            Application.DoEvents()
            For i As Integer = 1 To Sheet.Rows.count - 1
                Dim dr As DataRow = DataTables("Testlist").Find("[统编学籍号] = '" & Sheet(i,3).Value & "'")
                If dr IsNot Nothing Then '如果当前表找到此列
                    For ii As Integer = 0 To Sheet.cols.count - 1
                        dr(Sheet(0,ii).value) = sheet(i,ii).Value
                    Next
                Else
                    Dim dr1 As Row = Tables("Testlist").AddNew() '无此列就增加行并复制数据
                    For ii As Integer = 0 To Sheet.cols.count - 1
                        dr1(Sheet(0,ii).value) = sheet(i,ii).Value
                    Next
                End If
            Next
        End If
    Next
    e.Form.Controls("Label1").Text = "已经完成."
    MessageBox.Show("数据汇总完毕,请检查数据!", "汇总提示", MessageBoxButtons.OK, MessageBoxIcon.Question)
   
    Dim dlg1 As New SaveFileDialog '定义一个新的SaveFileDialog
    dlg1.Filter= "Excel文件|*.xls" '设置筛选器
    If dlg1.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
        Dim flg As New SaveExcelFlags
        flg.CellStyle = True
        flg.RowNumber =True
        CurrentTable.SaveExcel(dlg1.FileName,"Testlist",flg)
    End If
End If

 回到顶部