Foxtable(狐表)用户栏目专家坐堂 → 处理EXCEL


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

主题:处理EXCEL

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


加好友 发短信
等级:五尾狐 帖子:1107 积分:10586 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2021/5/10 16:19:00 [显示全部帖子]

如:这段代码放到ABC函数,应该怎么改怎么调用参数?
If Forms("翻译器").Controls("NumComBox1").Text = Nothing And Forms("翻译器").Controls("NumComBox2").Text = Nothing Then
    RowsMax = 0
    ColsMax = ws.UsedRange.columns.count
    '获取最大行
    For i As Integer = 1 To ColsMax
        Dim r = ws.cells(excelrows,i).End(MsExcel.XlDirection.xlUp).Row
        If r > RowsMax Then
            RowsMax = r
        End If
    Next
    '获取最大列
    For i As Integer = 1 To RowsMax
        Dim r = ws.cells(i,excelcol).End(MsExcel.XlDirection.xlToLeft).Column
        If r > ColsMax Then
            ColsMax = r
        End If
    Next
Else
    RowsMax = Numcb1.Value
    ColsMax = Numcb2.Value
End If
Dim  rg As MSExcel.Range =  Ws.Range(Ws.Cells(1,1), Ws.Cells(RowsMax,ColsMax))
If rg.Count = 1 And rg(1).Value Is Nothing  Then
    Continue For
End If
Dim ary = rg.value
For i As Integer=1 To RowsMax
    For j As Integer = 1 To ColsMax
        If ary(i, j) <> Nothing  AndAlso Typeof ary(i,j) Is String AndAlso dic.ContainsKey(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) Then
            lbl11.text= "Sheet名称:" & ws.name
            rg(i, j).Value= dic(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), ""))
        End If
    Next
Next
End If

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


加好友 发短信
等级:五尾狐 帖子:1107 积分:10586 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2021/5/10 16:41:00 [显示全部帖子]

比如标红这几处怎么定义变量?在外面会定义,但函数里与函数外是怎么个对应关系搞不懂。
如:要定义WS和WB怎么定义?

Dim Ws As MSExcel.WorkSheet=Wb.WorkSheets(k) '这个变量K是函数外面For k As Integer=1 To wb.worksheets.Count循环的,而WB是Dim wb=app.WorkBooks.open(file)来的,
怎么带进到函数中,老师给举个例子呗。

    RowsMax = 0
    ColsMax = ws.UsedRange.columns.count
    '获取最大行
    For i As Integer = 1 To ColsMax
        Dim r = ws.cells(excelrows,i).End(MsExcel.XlDirection.xlUp).Row
        If r > RowsMax Then
            RowsMax = r
        End If
    Next
    '获取最大列
    For i As Integer = 1 To RowsMax
        Dim r = ws.cells(i,excelcol).End(MsExcel.XlDirection.xlToLeft).Column
        If r > ColsMax Then
            ColsMax = r
        End If
    Next
Else
    RowsMax = Numcb1.Value
    ColsMax = Numcb2.Value
End If
Dim  rg As MSExcel.Range =  Ws.Range(Ws.Cells(1,1), Ws.Cells(RowsMax,ColsMax))
If rg.Count = 1 And rg(1).Value Is Nothing  Then
    Continue For
End If
Dim ary = rg.value
For i As Integer=1 To RowsMax
    For j As Integer = 1 To ColsMax
        If ary(i, j) <> Nothing  AndAlso Typeof ary(i,j) Is String AndAlso dic.ContainsKey(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) Then
            lbl11.text= "Sheet名称:" & ws.name
            rg(i, j).Value= dic(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), ""))
        End If
    Next
Next


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


加好友 发短信
等级:五尾狐 帖子:1107 积分:10586 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2021/5/11 14:53:00 [显示全部帖子]

这个我看过,但达到灵活运用还是差点意思。麻烦老师拿我的代码给举个例子吧,谢谢!

如:要定义WS和WB怎么定义?
Dim Ws As MSExcel.WorkSheet=Wb.WorkSheets(k) '这个变量K是函数外面For k As Integer=1 To wb.worksheets.Count循环的,而WB是Dim wb=app.WorkBooks.open(file)来的,

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


加好友 发短信
等级:五尾狐 帖子:1107 积分:10586 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2021/5/11 16:49:00 [显示全部帖子]

只放这段,如何定义参数?标红的都是变量(下面这段代码本身就在自定义函数里)

For i As Integer=1 To RowsMax
    For j As Integer = 1 To ColsMax
        If ary(i, j) <> Nothing  AndAlso Typeof ary(i,j) Is String AndAlso dic.ContainsKey(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) Then

            rg(i, j).Value= dic(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), ""))
        End If
    Next
Next
[此贴子已经被作者于2021/5/11 16:50:02编辑过]

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


加好友 发短信
等级:五尾狐 帖子:1107 积分:10586 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2021/5/11 20:15:00 [显示全部帖子]

有什么规律吗?哪些变量要做为参数定义?

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


加好友 发短信
等级:五尾狐 帖子:1107 积分:10586 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2021/5/12 11:36:00 [显示全部帖子]

1、这样写对吗?
2、我认为标红处的代码最费时间,所以放到异步执行,这个思路对不?
3、标红代码如果放异步执行,里面变量RowsMax 、ColsMax 执行时是固定不变的吗?不会受外面的代码影响吧?(每次处理不同SHEET时RowsMax 、ColsMax都不同)
dim ws  = args(0)
dim wb  = args(1)
dim RowsMax as integer
dim ColsMax as integer
For k As Integer=1 To wb.worksheets.Count
            If Wb.WorkSheets(k).Visible <> 0 Then
                Dim Ws As MSExcel.WorkSheet=Wb.WorkSheets(k)
                If Numcb1.Text = Nothing And Numcb2.Text = Nothing Then
                    RowsMax = 0
                    ColsMax = ws.UsedRange.columns.count
                    For i As Integer = 1 To ColsMax
                        Dim r = ws.cells(excelrows,i).End(MsExcel.XlDirection.xlUp).Row
                        If r > RowsMax Then
                            RowsMax = r
                        End If
                    Next
                    For i As Integer = 1 To RowsMax
                        Dim r = ws.cells(i,excelcol).End(MsExcel.XlDirection.xlToLeft).Column
                        If r > ColsMax Then
                            ColsMax = r
                        End If
                    Next
                Else
                    RowsMax = Numcb1.Value
                    ColsMax = Numcb2.Value
                End If
                Dim  rg As MSExcel.Range =  Ws.Range(Ws.Cells(1,1), Ws.Cells(RowsMax,ColsMax))
                Dim ary = rg.value
                For i As Integer=1 To RowsMax
                    For j As Integer = 1 To ColsMax

                         rg(i, j).Value= dic(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), ""))

                    Next
                Next
            End If 
Next

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


加好友 发短信
等级:五尾狐 帖子:1107 积分:10586 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2021/5/12 13:49:00 [显示全部帖子]

如果只把标红处放到异步函数执行的话,那RowsMax和ColsMax的值是在上面的代码中取到的,怎么代到函数中去?
dim ws  = args(0)
dim wb  = args(1)
dim RowsMax as integer
dim ColsMax as integer
For k As Integer=1 To wb.worksheets.Count
            If Wb.WorkSheets(k).Visible <> 0 Then
                Dim Ws As MSExcel.WorkSheet=Wb.WorkSheets(k)
                If Numcb1.Text = Nothing And Numcb2.Text = Nothing Then
                    RowsMax = 0
                    ColsMax = ws.UsedRange.columns.count
                    For i As Integer = 1 To ColsMax
                        Dim r = ws.cells(excelrows,i).End(MsExcel.XlDirection.xlUp).Row
                        If r > RowsMax Then
                            RowsMax = r
                        End If
                    Next
                    For i As Integer = 1 To RowsMax
                        Dim r = ws.cells(i,excelcol).End(MsExcel.XlDirection.xlToLeft).Column
                        If r > ColsMax Then
                            ColsMax = r
                        End If
                    Next
                Else
                    RowsMax = Numcb1.Value
                    ColsMax = Numcb2.Value
                End If
                Dim  rg As MSExcel.Range =  Ws.Range(Ws.Cells(1,1), Ws.Cells(RowsMax,ColsMax))
                Dim ary = rg.value
                For i As Integer=1 To RowsMax
                    For j As Integer = 1 To ColsMax

                         rg(i, j).Value= dic(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), ""))

                    Next
                Next
            End If 
Next

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


加好友 发短信
等级:五尾狐 帖子:1107 积分:10586 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2021/5/12 16:29:00 [显示全部帖子]

怎么传?
For i As Integer=1 To RowsMax
      For j As Integer = 1 To ColsMax
       rg(i, j).Value= dic(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), ""))
      Next
Next

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


加好友 发短信
等级:五尾狐 帖子:1107 积分:10586 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2021/5/13 14:59:00 [显示全部帖子]

程序执行了一会后报错,老师帮忙看看

图片点击可在新窗口打开查看此主题相关图片如下:513.png
图片点击可在新窗口打开查看


1、下列代码执行的时候能执行一会后报
button按钮调用代码:

Dim FilePath As String =  e.Form.Controls("TextBox1").value
Functions.Execute("处理SHEET",FilePath)
——————————————
处理SHEET函数代码:

Dim st As Date = Date.Now
Dim txt4 As WinForm.TextBox = Forms("窗口1").Controls("TextBox4")
Dim cm As WinForm.ComboBox = Forms("窗口1").Controls("ComboBox1")
Dim lbl As WinForm.Label = Forms("窗口1").Controls("Label6")
Dim lbl8 As WinForm.Label = Forms("窗口1").Controls("Label8")
Dim lbl9 As WinForm.Label = Forms("窗口1").Controls("Label9")
Dim lbl11 As WinForm.Label = Forms("窗口1").Controls("Label11")
Dim Numcb1 As WinForm.NumericComboBox = Forms("窗口1").Controls("NumComBox1")
Dim Numcb2 As WinForm.NumericComboBox = Forms("窗口1").Controls("NumComBox2")
Dim path As String = args(0)
Dim file As Object
Dim app As new MSExcel.Application

Dim dic As new Dictionary(of String,String)
Dim dicfile As String =  Forms("窗口1").Controls("TextBox2").value
Dim Book As New XLS.Book(dicfile) '定义一个Excel工作簿
Dim Sheet As XLS.Sheet = Book.Sheets(0) '引用工作簿的第一个工作表

For i As Integer = 0 To Sheet.Rows.Count-1
    If dic.ContainsKey(Sheet(i, 0).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) = False Then
        dic.add(Sheet(i, 0).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), ""),Sheet(i, 1).Text)
    End If
Next

For Each file In FileSys.GetFiles(path)
    Dim excelcol As Integer
    Dim excelrows As Integer
    If file.EndsWith(".xls") OrElse file.EndsWith(".XLS") OrElse file.EndsWith(".xlsx") OrElse file.EndsWith(".XLSX")   Then
        If file.EndsWith(".xls") OrElse file.EndsWith(".XLS")  Then
            excelcol = 256
            excelrows  =65536
        Else
            excelcol = 16384
            excelrows  =1048576
        End If
        Dim wb=app.WorkBooks.open(file)
        For k As Integer=1 To wb.worksheets.Count
            If Wb.WorkSheets(k).Visible <> 0 Then        '如果sheet表为不隐藏时处理(0隐藏,-1为不隐藏
                Dim Ws As MSExcel.WorkSheet=Wb.WorkSheets(k)
                If Numcb1.Text = Nothing And Numcb2.Text = Nothing Then
                    RowsMax = 0
                    ColsMax = ws.UsedRange.columns.count
                    '获取最大行
                    For i As Integer = 1 To ColsMax
                        Dim r = ws.cells(excelrows,i).End(MsExcel.XlDirection.xlUp).Row
                        If r > RowsMax Then
                            RowsMax = r
                        End If
                    Next

                    '获取最大列
                    For i As Integer = 1 To RowsMax
                        Dim r = ws.cells(i,excelcol).End(MsExcel.XlDirection.xlToLeft).Column
                        If r > ColsMax Then
                            ColsMax = r
                        End If
                    Next
                Else
                    RowsMax = Numcb1.Value
                    ColsMax = Numcb2.Value
                End If
                
                Dim  rg As MSExcel.Range =  Ws.Range(Ws.Cells(1,1), Ws.Cells(RowsMax,ColsMax))
                If rg.Count = 1 And rg(1).Value Is Nothing Then
                    Continue For
                End If

                Functions.AsyncExecute("异数函数",app,wb,ws,dic,RowsMax,ColsMax,rg) '异步函数

            End If '不处理隐藏sheet
        Next
        
        Dim txt1 As WinForm.TextBox = Forms("窗口1").Controls("TextBox3")
        txt1.text = file & vbcrlf & txt1.text & vbcrlf
        Application.DoEvents()
        FileCount=FileCount+1
        lbl.Text="共处理" & FileCount & "个文件"
        wb.Save
        app.quit
    End If
    
Next
lbl9.Text="计算结束, 耗时: " & (Date.Now - st).TotalSeconds & "秒"
————————————————
异数函数代码:
Dim app = args(0)
Dim wb  = args(1)
Dim ws  = args(2)
Dim dic  = args(3)
Dim RowsMax = args(4)
Dim ColsMax = args(5)
Dim rg = args(6)
Dim ary = rg.value
For i As Integer=1 To RowsMax
    For j As Integer = 1 To ColsMax
        If ary(i, j) <> Nothing  AndAlso Typeof ary(i,j) Is String AndAlso dic.ContainsKey(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) Then
            'lbl11.text= "Sheet名称:" & ws.name
            output.show(i & "," & j)
            rg(i, j).Value= dic(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), ""))
        End If
    Next
Next

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


加好友 发短信
等级:五尾狐 帖子:1107 积分:10586 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2021/5/13 15:28:00 [显示全部帖子]

关键是要处理单个文件的多个SHEET啊,一个文中有100多个SHEET,不用异步太慢了。还有其他办法吗?

 回到顶部
总数 59 上一页 1 2 3 4 5 6 下一页