以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- 处理EXCEL (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=164874) |
-- 作者:wh420 -- 发布时间:2021/5/7 15:05:00 -- 处理EXCEL 跟踪代码发现标红处报错:从字符串“”到类型“Double”的转换无效。输入字符串的格式不正确。 Dim st As Date = Date.Now Dim txt4 As WinForm.TextBox = Forms("翻译器").Controls("TextBox4") Dim cm As WinForm.ComboBox = Forms("翻译器").Controls("ComboBox1") Dim lbl As WinForm.Label = Forms("翻译器").Controls("Label6") Dim lbl8 As WinForm.Label = Forms("翻译器").Controls("Label8") Dim lbl9 As WinForm.Label = Forms("翻译器").Controls("Label9") Dim lbl11 As WinForm.Label = Forms("翻译器").Controls("Label11") Dim Numcb1 As WinForm.NumericComboBox = Forms("翻译器").Controls("NumComBox1") Dim Numcb2 As WinForm.NumericComboBox = Forms("翻译器").Controls("NumComBox2") Dim path As String = args(0) Dim file As Object Dim app As new MSExcel.Application \'app.visible = True \'//准备字典信息 \'try Dim dic As new Dictionary(of String,String) Dim dicfile As String = Forms("翻译器").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) \'try 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 output.show(1) If Wb.WorkSheets(k).Visible <> 0 Then \'如果sheet表为不隐藏时处理(0隐藏,-1为不隐藏 output.show(2) Dim Ws As MSExcel.WorkSheet=Wb.WorkSheets(k) output.show(3) 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 \'Output.Show("rowsmax =" & rowsmax ) \'获取最大列 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 output.show(RowsMax & " " & ColsMax) End If Dim rg As MSExcel.Range = Ws.Range(Ws.Cells(1,1), Ws.Cells(RowsMax,ColsMax)) output.show(4) If rg.Count = 1 And rg(1).Value = "" Then \'If rg.Count = 1 And rg(1).Value Is Nothing Then Continue For End If Dim ary = rg.value output.show(5) For i As Integer=1 To RowsMax \'If rg.Rows(i).height <> 0 Then For j As Integer = 1 To ColsMax \'If rg.Columns(j).width <> 0 Then 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(6) rg(i, j).Value= dic(ary(i,j).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) output.show("Sheet名称:" & ws.name & rg(i,j).value) End If \'End If Next \'End If Next End If \'不处理隐藏sheet Next Dim txt1 As WinForm.TextBox = Forms("翻译器").Controls("TextBox3") txt1.text = file & vbcrlf & txt1.text & vbcrlf Application.DoEvents() output.show("暂停") FileCount=FileCount+1 lbl.Text="共处理" & FileCount & "个文件" wb.Save app.quit End If Next For Each p As String In FileSys.GetDirectories(path) Functions.Execute("G纯译文_翻译隐藏行列", p) Next lbl9.Text="计算结束, 耗时: " & (Date.Now - st).TotalSeconds & "秒"
|
-- 作者:有点蓝 -- 发布时间:2021/5/7 15:29:00 -- 应该不是红色代码的问题,Value 是对象,应该使用Is Nothing判断 If rg.Count = 1 And rg(1).Value Is Nothing Then
|
-- 作者:wh420 -- 发布时间:2021/5/7 15:48:00 -- 果然是那的问题,请问老师:如何忽略那个错误而让程序能继续执行呢?因为EXCEL情况太复杂 |
-- 作者:有点蓝 -- 发布时间:2021/5/7 15:56:00 -- 参考:http://www.foxtable.com/webhelp/topics/2070.htm |
-- 作者:wh420 -- 发布时间:2021/5/7 16:15:00 -- 1、rg(1).Value Is Nothing 如果对象是数字、时间等用IS NOTHING判断会准确吗? 2、用ISNULL来判断EXCEL单元格是否为空是否准确?
|
-- 作者:有点蓝 -- 发布时间:2021/5/7 16:23:00 -- 1、Value是对象类型,也就是object,数字、时间等一样可以用IS NOTHING判断 2、没有ISNULL这种用法
|
-- 作者:wh420 -- 发布时间:2021/5/7 16:33:00 -- 收到,谢谢。 |
-- 作者:wh420 -- 发布时间:2021/5/7 17:32:00 -- lbl11.text= "Sheet名称:" & ws.name & vbcrlf & ary(i,j) & rg(i,j).value 这种写法把每个单元格内容实时输出到LABEL标签中,是否会影响代码执行效率? 因为我在测试的时候同样的文档每次结果都不太一样,所以有点晕。 |
-- 作者:有点蓝 -- 发布时间:2021/5/7 17:37:00 -- 有没有影响要看量,单元格多遍历起来肯定有效率问题。 至于结果都不太一样请截图详细说明一下
|
-- 作者:wh420 -- 发布时间:2021/5/7 18:07:00 -- 那有什么办法在不影响效率的情况下还能看到代码执行的进度呢?麻烦老师给个思路 |