简体   繁体   English

如何使用VBA将数据从XML文件读入Excel文件

[英]How to read in data from XML file into a Excel file using VBA

I have the following problem, I have a very big XML file, which have to be read in Excel via VBA to fill in the different columns of the Excel sheet table. 我有以下问题,我有一个很大的XML文件,必须通过VBA在Excel中读取该文件,以填写Excel工作表的不同列。

I wrote the following code to open the XML file 我编写了以下代码来打开XML文件

Sub openxXML()
    Dim xml As Workbook
    Dim varxml As Variant

    varxml = Application.GetOpenFilename("Alle Data (*.*), *.*,XML-Data(*.xml),*xml*", 2, "Choose XML-File", _
      "choose", False)

    If varxml = False Then
        MsgBox "No Data selected"
    End If     
End Sub

But now I don't know how I turn this data into my Excel table with many columns. 但是现在我不知道如何将这些数据转换为具有许多列的Excel表。
Can anybody tell me if the code above is okay and how to continue? 有人可以告诉我上面的代码是否正确以及如何继续吗?

Thank u and sorry for my English, I am from France 谢谢你,抱歉我的英语,我来自法国

With Application.GetOpenFilename you only get the filename of the file a user selected. 使用Application.GetOpenFilename只能获取用户选择的文件的文件名。 It does not open the file itself, just select a file from your hard drive. 打开文件本身,只需选择您的硬盘驱动器中的文件。

So varxml contains something like C:\\Temp\\test.xml , a filename with its full path. 因此, varxml包含类似C:\\Temp\\test.xml的文件,该文件名包含完整路径。

You then need to open/import/whatever you want to do. 然后,您需要打开/导入/执行任何操作。

For example to import: 例如导入:

Workbooks.OpenXML Filename:=varxml, LoadOption:=xlXmlLoadImportToList

An easy way would be using the macro recorder in Excel, and then perform the import manually once, so you get an idea of how, the import or whatever might look like in VBA. 一种简单的方法是在Excel中使用宏记录器,然后手动执行一次导入,这样您就可以了解如何在VBA中进行导入或其他操作。

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

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