以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 如何将XLS.Book改成MSEXCEL? (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=89755) |
-- 作者:wh420 -- 发布时间:2016/8/29 12:05:00 -- 如何将XLS.Book改成MSEXCEL? 见如下代码,是用XLS.BOOK实现的,如何改成MSEXCEL,并且ary(i,j)数组的形式 For j As Integer = 0 To val.Value If Sheet1(i, j).Text > "" AndAlso dic.ContainsKey(Sheet1(i, j).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) Then If Forms("窗口1").Controls("RBt5").checked =True Then Sheet1(i, j).Value = dic(Sheet1(i, j).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) Else If Forms("窗口1").Controls("RBt6").checked =True Then If Forms("窗口1").Controls("RBt1").checked =True Then Sheet1(i, j).Value = Sheet1(i, j).Text & "|" & dic(Sheet1(i, j).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) Else If Forms("窗口1").Controls("RBt2").checked=True Then Sheet1(i, j).value = Sheet1(i, j).Text & vbcrlf & dic(Sheet1(i, j).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) Else If Forms("窗口1").Controls("RBt4").checked=True Then Sheet1(i, j).Value = Sheet1(i, j).Text Sheet1(i, j+1).Value = dic(Sheet1(i, j).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) End If End If End If Next |
-- 作者:大红袍 -- 发布时间:2016/8/29 12:12:00 -- Sheet1(i, j).Text 改成 ary(i+1,j+1)
Sheet1(i, j).Value 改成 sheet表.Cells(i+1, j+1).Value |
-- 作者:大红袍 -- 发布时间:2016/8/29 12:13:00 -- 如果要提高速度,参考
http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=28089&skin=0
|
-- 作者:wh420 -- 发布时间:2016/8/29 14:38:00 -- 我看了提高效率的那个,但还不是我想要的。我要实现的功能是把一个字典文件的内容与另外一EXCEL做对比,把字典里有的填充到另外一个EXCEL中(该功能已经实现)但不想处理隐藏的行和列,原来用XLS.BOOK来判断<>0的列,但是不准确,所以要改成MSEXCEL方式,麻烦老师给看看下面的代码如何更改。这是完整的代码 Dim path As String = args(0) \'//准备字典信息 \'try 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 As String In FileSys.GetFiles(path) If file.EndsWith(".xls") OrElse file.EndsWith(".xlsx") Then Dim Book1 As New XLS.Book(file) For k As Integer = 0 To Book1.Sheets.Count-1 \'//多个sheet的处理 Dim Sheet1 = Book1.Sheets(k) For i As Integer = 0 To Sheet1.Rows.Count-1 For j As Integer = 0 To sheet1.Cols.Count-1 If Sheet1(i, j).Text > "" AndAlso dic.ContainsKey(Sheet1(i, j).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) Then If Forms("窗口1").Controls("RBt1").checked Then Sheet1(i, j).Value = Sheet1(i, j).Text & "|" & dic(Sheet1(i, j).Text.ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) End If End If Next Next Next End If Next For Each p As String In FileSys.GetDirectories(path) Functions.Execute("对比函数", p) Next
|
-- 作者:wh420 -- 发布时间:2016/8/29 14:40:00 -- 注:以上代码最好能用高效率的方式实现,因为处理的数据较多。 |
-- 作者:大红袍 -- 发布时间:2016/8/29 15:12:00 -- 看2楼、3楼,看以前写给你的代码。 |
-- 作者:wh420 -- 发布时间:2016/9/1 16:41:00 -- 老师帮忙看看哪里不对,报错“索引超出了数组界限” Dim path As String = args(0) 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 As String In FileSys.GetFiles(path) If file.EndsWith(".xls") OrElse file.EndsWith(".xlsx") Then Dim wb=app.WorkBooks.Open(file) For k As Integer = 1 To wb.worksheets.count \'//多个sheet的处理 Dim ws As msexcel.worksheet=wb.worksheets(k) Dim rg As msexcel.range=ws.usedrange Dim ary =rg.value \'Dim Sheet1 = Book1.Sheets(k) For i As Integer = 1 To rg.Rows.Count For j As Integer = 1 To rg.Columns.Count If ary(i+1,j+1) > "" AndAlso dic.ContainsKey(ary(i+1,j+1).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) Then If Forms("窗口1").Controls("RBt1").checked Then ws.Cells(i+1, j+1).Value = ary(i+1,j+1) & "|" & dic(ary(i+1,j+1).ToLower.Replace(" ","").Replace(chr(10), "").Replace(chr(13), "")) msgbox(ws.Cells(i+1, j+1).Value ) End If End If Next Next Next wb.save() app.quit() End If Next For Each p As String In FileSys.GetDirectories(path) Functions.Execute("对比函数", p) Next
|
-- 作者:wh420 -- 发布时间:2016/9/1 16:59:00 -- 老师帮忙看看 |
-- 作者:有点蓝 -- 发布时间:2016/9/1 17:29:00 -- 如果是最后一行或者最后一列,i+1和j+1就超出范围了 |
-- 作者:wh420 -- 发布时间:2016/9/2 0:22:00 -- 是啊,那应该如何改一下? |