所以,我有一堆以 Excel 电子表格形式提供给我们的内容。我需要获取该内容并将其推送到另一个系统。另一个系统从 XML 文件获取输入。我可以手工完成所有这些工作(相信我,管理人员让我这样做没有任何问题!),但我希望有一种简单的方法来编写一个 Excel 宏来生成我需要的 XML。这对我来说似乎是一个更好的解决方案,因为这是一项需要定期重复的工作(我们将在 Excel 工作表中获得大量内容)并且拥有一个为我们完成它的批处理工具才有意义.
但是,我以前从未尝试过从 Excel 电子表格生成 XML。我有一点 VBA 知识,但我是 XML 的新手。我想我在谷歌搜索中遇到的问题是我什至不知道要谷歌搜索什么。谁能给我一点指导让我开始?我的想法听起来像是解决这个问题的正确方法,还是我忽略了一些显而易见的事情?
感谢 StackOverflow!
最佳答案
您可能想考虑 ADO - 工作表或区域可以用作表格。
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adPersistXML = 1
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
''It wuld probably be better to use the proper name, but this is
''convenient for notes
strFile = Workbooks(1).FullName
''Note HDR=Yes, so you can use the names in the first row of the set
''to refer to columns, note also that you will need a different connection
''string for >=2007
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
cn.Open strCon
rs.Open "Select * from [Sheet1$]", cn, adOpenStatic, adLockOptimistic
If Not rs.EOF Then
rs.MoveFirst
rs.Save "C:\Docs\Table1.xml", adPersistXML
End If
rs.Close
cn.Close
关于xml - 如何从 Excel VBA 宏生成 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2766760/