使用 VBA Excel 宏读取 XML 并自定义生成的表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4873830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read XML using VBA Excel Macro and customize the generated table
提问by Nathan
I am new to Excel VBA. My problem is that I have an XML file and I need to show its contents in a tabular format in Excel.
我是 Excel VBA 的新手。我的问题是我有一个 XML 文件,我需要在 Excel 中以表格格式显示其内容。
I have written a sample VBA code as follows:
我编写了一个示例 VBA 代码,如下所示:
Dim testFile As Workbook
Set testFile = Workbooks.OpenXML(
Filename:="D:\sample.xml",LoadOption:=XlXmlLoadOption.xlXmlLoadImportToList)
It generates the contents in a tabular format in Excel, but there are a lot of empty cells and some garbage data (data relevant in XML but not needed here). I need to delete the empty cells (both in rows and columns) and also remove some unwanted columns.
它在 Excel 中以表格格式生成内容,但有很多空单元格和一些垃圾数据(与 XML 相关但此处不需要的数据)。我需要删除空单元格(在行和列中)并删除一些不需要的列。
Can someone please suggest some approach on how to proceed in this?
有人可以建议一些关于如何进行的方法吗?
采纳答案by Simon Cowen
Depending on how comfortable with VBA you are, you could parse the file yourself. A reference to "Microsoft XML" (msxml#.dll) and a look at this questionshould be all you need to get you on your way!
根据您对 VBA 的熟悉程度,您可以自己解析文件。对“Microsoft XML”( msxml#.dll)的引用和对这个问题的了解应该是您上路所需的全部内容!