简体   繁体   English

批量导入 XML 文件到 Excel 工作簿 (VBA)

[英]Batch Import XML Files to Excel Workbook (VBA)

Mission: I have a folder of some 90 XML files received weekly that all need consolidated as individual pages in an excel workbook (excel 2016).任务:我每周收到一个包含大约 90 个 XML 文件的文件夹,所有这些文件都需要合并为 excel 工作簿(excel 2016)中的单个页面。 To make that clear, the resulting workbook should have 90 pages (or however many XML files I need to import that week), with each sheet matching the file name of the XML matched to it.为了清楚起见,生成的工作簿应该有 90 页(或者我那周需要导入多少个 XML 文件),每个工作表都与与其匹配的 XML 的文件名相匹配。

I have found several mentions on how to batch import XMLs into a single sheet or table, but other than the kutools extensions that never work for me, I can't seem to find anything that imports the files as individual sheets.我发现了一些关于如何将 XML 批量导入单个工作表或表格的提及,但除了从来不适合我的 kutools 扩展之外,我似乎找不到任何将文件作为单个工作表导入的内容。

Is this even possible?这甚至可能吗? I am a complete VBA scrub, but if there's a way to do this that doesn't involve me manually clicking Data -> Get Data -> From File -> From XML File and going through all the dialogues for each individual file, I would love to hear it.我是一个完整的 VBA 擦洗,但如果有一种方法可以做到这一点,而不需要我手动单击数据 -> 获取数据 -> 从文件 -> 从 XML 文件并浏览每个单独文件的所有对话,我会喜欢听。

Thank you in advance!先感谢您!

Here's a very basic example:这是一个非常基本的例子:

Sub Tester()
    
    Const FLDR As String = "C:\Tester\tmp\"
    
    Dim f, wb
    Application.DisplayAlerts = False
    f = Dir(FLDR & "*.xml")
    Do While Len(f) > 0
        Debug.Print(f) 'output filename to Immediate panel
        Application.DisplayAlerts = False
        Set wb = Workbooks.OpenXML(Filename:=FLDR & f, LoadOption:=xlXmlLoadImportToList)
        Application.DisplayAlerts = True
        wb.Sheets(1).Name = Replace(f, ".xml", "")
        wb.Sheets(1).Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        wb.Close False
        f = Dir()
    Loop
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM