Foxtable(狐表)用户栏目专家坐堂 → 我的代码可以简化吗?


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

主题:我的代码可以简化吗?

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


加好友 发短信
等级:小狐 帖子:376 积分:4189 威望:0 精华:0 注册:2008/9/1 14:18:00
我的代码可以简化吗?  发帖心情 Post By:2012/5/18 9:20:00 [只看该作者]

Dim str As String
Dim tj As Integer
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 i As Integer = 0 To Sheet.cols.count -1
            Names.Add(Sheet(0,i).value)
        Next
        If Names.Contains("统编学籍号")= False Then
            MessageBox.Show("导入表中不含""统编学籍号""列", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question)
            Return
        End If
        tj = Nothing
        For i As Integer = 0 To Sheet.cols.count -1
            If Sheet(0,i).value = "统编学籍号" Then
                tj = i
                Exit For
            End If
        Next
        str = Nothing
        For i As Integer = 0 To Sheet.cols.count -1
            If str = "" Then
                str = Sheet(0,i).value
            Else
                str = str & "|" & Sheet(0,i).value
            End If
        Next
        If str = "" Then
            MessageBox.Show("无匹配字段", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question)
            Return
        End If
        Dim Values() As String = str.split("|")
        e.Form.Controls("Label1").text = FileSys.GetName(fl)
        Application.DoEvents()
        For ii As Integer = 1 To Sheet.Rows.count - 1
            Dim dr As DataRow = DataTables("Testlist").Find("[统编学籍号] = '" & Sheet(ii,tj).value & "'")
            If dr IsNot Nothing Then
                For i As Integer = 0 To Values.Length - 1
                    dr(Values(i)) = Sheet(ii,i).value
                Next
            Else
                Dim dr1 As DataRow = DataTables("Testlist").AddNew()
                For i As Integer = 0 To Values.Length - 1
                    dr1(Values(i)) = Sheet(ii,i).value
                Next
            End If
        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

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


加好友 发短信
等级:狐神 帖子:6885 积分:43595 威望:0 精华:0 注册:2009/3/2 14:07:00
  发帖心情 Post By:2012/5/18 9:59:00 [只看该作者]

应该可以简化,只是你没有附件,想帮你简化的人帮不了你。

传附件要有原始数据和你的目的。


 回到顶部