我使用Excel VBA代码从大型zip存档中读取每个xml文件;但是执行需要3到5分钟。我在Java中有类似的代码,可以在2到3秒内运行。最大的区别是Excel VBA实际上是将每个文件复制到另一个文件夹。然后它读取解压缩的文件。那么我就不要把硬盘弄得乱七八糟,它会把它们删掉。我的Java版本将读取xml文件而不复制它们,节省了创建新文件的时间,以便稍后删除它们。是否有可能在VBA中模仿这种行为? Java程序运行良好,但提取的数据不在excel文件中,这实际上是我需要的格式。
以下是我目前使用的代码:
C = A*B = [2,6,12,20]
从下面的评论中,我做了一个调整,分别给出了每个文件。我只需要将内容读作字符串。
Dim map As XmlMap
Dim BusDate As Date
Dim ZipFile As Variant
Dim ZipPath As Variant
Dim File As String
Dim Path As String
Dim App As Object
BusDate = InputBox(Prompt:="Enter the business date:", Title:="Date", Default:=Format(Date - 1, "mm/dd/yyyy"))
Set map = ActiveWorkbook.XmlMaps.Add("C:\POSDataForChev\Import\naxml.xsd")
ZipFile = "C:\NPollWin\002\" & Format(BusDate, "mm") & "\JRN" & Format(BusDate, "mm") & Format(BusDate, "dd") & ".zip"
ZipPath = "C:\POSDataForChev\Import\"
Set App = CreateObject("Shell.Application")
App.Namespace(ZipPath).CopyHere App.Namespace(ZipFile).items
RmDir ZipPath & "JRN" & Format(BusDate, "mm") & Format(BusDate, "dd")
Path = "C:\POSDataForChev\Import\"
File = Dir(Path & "JRN" & Format(BusDate, "yyyy") & Format(BusDate, "mm") & Format(BusDate, "dd") & "*.xml")
Do While Len(File) > 0
map.Import URL:=Path & File
Kill Path & File
File = Dir
Loop