我的1.txt文件里是aaa bbb ccc 2.txt文件里是www aaa bbb 是不是我文本文件里面随便写的数据不对啊???求解决
Dim sdict
Dim i
Dim keys
Call GetInfo(sdict)
Set ExcelApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelBook = ExcelApp.Workbooks.Add
Set ExcelSheet = ExcelBook.Worksheets(1) '添加工作页
ExcelSheet.Activate
ExcelApp.DisplayAlerts = False
ExcelSheet.Name="sheet1"
ExcelSheet.range("A1").Value = "blockname"
ExcelSheet.range("B1").value="1.txt"
ExcelSheet.range("C1").value="2.txt"
'取出所有关键字
keys = sdict.Keys
'取出所有值
items =sdict.Items
'循环dir逐条输出到excel中
For i = 0 To sdict.Count - 1
ExcelSheet.cells(i+2,1).value=keys(i)
'输出所有的值
value=value&items(i)&vbCrlf
if items(i) = 0 then
ExcelSheet.cells(i+2,2).value="Y"
ExcelSheet.cells(i+2,3).value="Y"
elseif items(i) =1 then
ExcelSheet.cells(i+2,2).value="Y"
ExcelSheet.cells(i+2,3).value="N"
elseif items(i)=2 then
ExcelSheet.cells(i+2,2).value="N"
ExcelSheet.cells(i+2,3).value="Y"
end if
Next
ExcelBook.SaveAs "f:\1.xlsx"
ExcelBook.close
set excelApp=nothing
set ExcelBook=nothing
set ExcelSheet=nothing
Public Function GetInfo(sdict)
Dim strarry1
Dim strarry2
Dim Count1
Dim Count2
Dim i
'读取txt1
Call ReadFile("f:\1.txt",strarry1)
'读取txt2
Call ReadFile("f:\2.txt",strarry2)
'数组长度
Count1 = ubound(strarry1) + 1
Count2 = ubound(strarry2) + 1
set sdict=CreateObject("Scripting.Dictionary")
'如果仅在txt1中存在时,标记为1
For i = 0 To Count1 - 1
sdict.add strarry1(i),1
Next
For i = 0 To Count2 - 1
'如果同时在txt1和txt2中存在时,标记为0
if sdict.Exists(strarry2(i)) then
sdict.add strarry2(i),0
'如果仅在txt2中存在时,标记为2
else
sdict.add strarry2(i),2
end if
Next
End Function
Public Function ReadFile(filePath,strarry)
'读取txt文件
Const ForReading = 1
Dim fs
Dim file
Dim str
Set fs = CreateObject("Scripting.FileSystemObject")
Set file = fs.OpenTextFile(filePath, ForReading)
Set str = file.readall
fs.close
If str=" " then
msgbox "txt为空"
Exit Function
End if
strarry = split(str,vbCrlf)
Set fs = nothing
End Function