VBA 方式如下:
1.複製貼上以下程式碼
2.修改第1列到第5列以配合你的電腦
3.執行 MainLoop (按 F5)
4.結果會存在 A 欄
Const gPath = "D:\data\html\" 'HTML 檔的路徑
Const gExtension = ".html" 'HTML 檔的副檔名
Const gFind = "公司" '要搜尋的關鍵字
Const gOffset = 10 '往前算 10 個字元,即位置
Const gGetLength = 10 '要抓幾個字元,即長度
Sub MainLoop()
arrFiles = ListFiles(gPath)
For nI = 1 To UBound(arrFiles) - 1
sCompany = ReadOneFile(gPath & arrFiles(nI))
Debug.Print sCompany
PutOneWord (sCompany)
Next nI
End Sub
Sub PutOneWord(ByVal sWord)
Range("A65535").End(xlUp).Offset(1, 0).Select
ActiveCell.Value = sWord
End Sub
Function ReadOneFile(ByVal strFilename) As String
Dim strFileContent As String
Dim iFile As Integer: iFile = FreeFile
Open strFilename For Input As #iFile
Dim barr() As Byte
barr = InputB(LOF(1), 1)
S = ""
For i = LBound(barr) To UBound(barr)
If barr(i) >= 128 Then
S = S + Chr(CLng(barr(i)) * 256 + barr(i + 1))
i = i + 1
Else
S = S + Chr(barr(i))
End If
Next
strFileContent = S
mPos = InStr(1, strFileContent, gFind, vbTextCompare)
If mPos <> 0 Then
ReadOneFile = Mid(strFileContent, mPos - gOffset, gGetLength)
Else
ReadOneFile = "找不到 " & gFind & " 文字"
End If
Close #iFile
End Function
Function ListFiles(ByVal sPath As String)
Dim vaArray As Variant
Dim i As Integer
Dim oFile As Object
Dim oFSO As Object
Dim oFolder As Object
Dim oFiles As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
Set oFiles = oFolder.Files
If oFiles.Count = 0 Then Exit Function
ReDim vaArray(1 To oFiles.Count)
i = 1
For Each oFile In oFiles
If Right(oFile.Name, Len(gExtension)) = gExtension Then
vaArray(i) = oFile.Name
i = i + 1
End If
Next
ReDim Preserve vaArray(1 To i)
ListFiles = vaArray
End Function