我有大约 600 个不同的文本字符串需要在 XML 文件中替换(我正在使用 notepad++,但如果这可以完成任务,我也可以使用其他程序)。文本更改列在单独的 Excel 文件中。有没有一种方法可以运行脚本或命令来一次查找/替换所有字符串,而不必单独执行每个字符串?
谢谢
您可以使用一些简单的 VBA 在 Excel 中完成这项工作。通过传入查找替换范围来调用下面的 Sub,例如从 Excel VBA 立即窗口(使用 Alt+F11 访问 VBA 编辑器,然后查看 -> 立即):
ReplaceXML Range("A1:B600")
假设 A1:B600 包含 600 个查找替换字符串。
在a中定义以下内容后module(从 VBA 编辑器 (Alt+F11) 中插入 -> 模块):
Option Explicit ' Use this !
Public Sub ReplaceXML(rFindReplaceRange as Range) ' Pass in the find-replace range
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String
Dim i as Long
' Edit as needed
sFileName = "C:\filepath\filename.xml"
iFileNum = FreeFile
Open sFileName For Input As iFileNum
Do Until EOF(iFileNum)
Line Input #iFileNum, sBuf
sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum
' Loop over the replacements
For i = 1 To rFindReplaceRange.Rows.Count
If rFindReplaceRange.Cells(i, 1) <> "" Then
sTemp = Replace(sTemp, rFindReplaceRange.Cells(i, 1), rFindReplaceRange(i, 2))
End If
Next i
' Save file
iFileNum = FreeFile
' Alter sFileName first to save to a different file e.g.
sFileName = "C:\newfilepath\newfilename.xml"
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)