我有一点 VBA 可以通过 VBA 加载 XML 文件。但是,当它被导入时,它全部在一列中,而不是拆分成一个表。
当我通过“数据”选项卡手动导入它时,我收到没有架构的警告,但询问我是否希望 Excel 基于源数据创建一个架构。然后将所有数据放在一个漂亮的表格中。
我希望这在我当前的 VBA 代码中自动发生:
VBA 看起来像
Sub refresh()
'--------------------------------1. Profile IDs-----------------------------------'
'date variables
Dim start_period As String
start_period = Sheets("Automated").Cells(1, 6).Value
Dim end_period As String
end_period = Sheets("Automated").Cells(1, 7).Value
'report id variable names
Dim BusinessplanningReportID As String
'--------------------------------REST queries--------------------------------'
Dim Businessplanning As String
'REST query values
Businessplanning = "URL;http://api.trucast.net/2/saved_searches/00000/pivot/content_volume_trend/?apikey=0000000&start=" + start_period + "&end=" + end_period + "&format=xml"
'--------------------------------------------Data connections-----------------------------------'
'key metrics
With Worksheets("Sheet1").QueryTables.Add(Connection:=Businessplanning, Destination:=Worksheets("Sheet1").Range("A1"))
.RefreshStyle = xlOverwriteCells
.SaveData = True
End With
目前,数据是这样呈现的,非结构化的。如何自动将其转换为表格?
<result>
<entry>
<published_date>20130201</published_date>
<post_count>18</post_count>
</entry>
谢谢,
::最终解决方案::
Sub XMLfromPPTExample2()
Dim XDoc As MSXML2.DOMDocument
Dim xresult As MSXML2.IXMLDOMNode
Dim xentry As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode
Dim start_period As String
start_period = Sheets("Automated").Cells(1, 6).Value
Dim end_period As String
end_period = Sheets("Automated").Cells(1, 7).Value
Dim wb As Workbook
Dim Col As Integer
Dim Row As Integer
Set XDoc = New MSXML2.DOMDocument
XDoc.async = False
XDoc.validateOnParse = False
XDoc.Load ("http://api.trucast.net/2/saved_searches/0000/pivot/content_volume_trend/?apikey=00000&start=" + start_period + "&end=" + end_period + "&format=xml")
LoadOption = xlXmlLoadImportToList
Set xresult = XDoc.DocumentElement
Set xentry = xresult.FirstChild
Col = 1
Row = 1
For Each xentry In xresult.ChildNodes
Row = 1
For Each xChild In xentry.ChildNodes
Worksheets("Sheet2").Cells(Col, Row).Value = xChild.Text
'MsgBox xChild.BaseName & " " & xChild.Text
Row = Row + 1
'Col = Col + 1
Next xChild
'Row = Row + 1
Col = Col + 1
Next xentry
End Sub
最佳答案
“硬编码”方式是这样的:
从这里开始
<result>
<entry>
<published_date>20130201</published_date>
<post_count>18</post_count>
</entry>
<entry>
<published_date>20120201</published_date>
<post_count>15</post_count>
</entry>
并且您想获得一个包含两列的 excel:
**published_date** | **post_count**
20130201 | 18
20120201 | 15
这样我们就可以假设在您的 XML 中您将始终拥有
<result><entry><Element>VALUE</Element><Element...n>VALUE</Element...n></entry>
重要提示: 在 PowerPoint、Excel..Word 中打开 VBA 编辑器并添加对“Microsoft XML,v3.0”的引用(此引用适用于 Office 2000...您可能还有其他引用)。
来源:http://vba2vsto.blogspot.it/2008/12/reading-xml-from-vba.html
Employee.XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<EmpDetails>
<Employee>
<Name>ABC</Name>
<Dept>IT-Software</Dept>
<Location>New Delhi</Location>
</Employee>
<Employee>
<Name>XYZ</Name>
<Dept>IT-Software</Dept>
<Location>Chennai</Location>
</Employee>
<Employee>
<Name>IJK</Name>
<Dept>HR Operations</Dept>
<Location>Bangalore</Location>
</Employee>
</EmpDetails>
阅读以上 XML 的代码
Sub XMLfromPPTExample()
Dim XDoc As MSXML2.DOMDocument
Dim xEmpDetails As MSXML2.IXMLDOMNode
Dim xEmployee As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode
Set XDoc = New MSXML2.DOMDocument
XDoc.async = False
XDoc.validateOnParse = False
XDoc.Load ("C:\Emp.xml")
Set xEmpDetails = XDoc.documentElement
Set xEmployee = xEmpDetails.firstChild
For Each xEmployee In xEmpDetails.childNodes
For Each xChild In xEmployee.childNodes
MsgBox xChild.baseName & " " & xChild.Text
Next xChild
Next xEmployee
End Sub
当然,在您的情况下,您需要调整您的日常工作:
result --> EmpDetails in the code provided
entry --> Employee in the code provided
加上任何其他必要的调整。
通过这种方式,您可以拥有任意多的“entry”和“entry child”元素。
事实上,循环遍历“条目”中的所有元素,您将得到您的 COLUMN,然后每个新条目都是一个新的 ROW。
不幸的是,我在 MAC 上没有 excel,所以我只是提出逻辑,你应该自己检查语法...这样你就可以在你想要的工作表上构建一个 EXCEL 表。
Dim col = 1; Dim row=1;
For Each xEmployee In xEmpDetails.childNodes
col = 1
For Each xChild In xEmployee.childNodes
Worksheets("NAMEOFTHESHEET").Cells(col, row).Value = xChild.Text
MsgBox xChild.baseName & " " & xChild.Text
col = col + 1;
Next xChild
row = row+1;
Next xEmployee
正确的方法应该是这样的:
LoadOption:=xlXmlLoadImportToList?
您正在从 URL 调用中获取 XML,但我强烈建议您在开始时尝试使用磁盘上的 XML 文件,并检查它是否正确有效。所以您应该做的是从此“WebService”获取示例 XML,然后将其保存在磁盘上。尝试以下列方式加载它:
Sub ImportXMLtoList()
Dim strTargetFile As String
Dim wb as Workbook
Application.Screenupdating = False
Application.DisplayAlerts = False
strTargetFile = "C:\example.xml"
Set wb = Workbooks.OpenXML(Filename:=strTargetFile, LoadOption:=xlXmlLoadImportToList)
Application.DisplayAlerts = True
wb.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("Sheet2").Range("A1")
wb.Close False
Application.Screenupdating = True
End Sub
关于xml - 通过 VBA 将 XML 加载到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16423644/
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:
从MB升级到新的MBP后,Apple的迁移助手没有移动我的gem。我这次是通过macports安装rubygems,希望在下次升级时避免这种情况。有什么我应该注意的陷阱吗? 最佳答案 如果你想把你的gems安装在你的主目录中(在传输过程中应该复制过来,作为一个附带的好处,会让你以你自己的身份运行geminstall,而不是root),将gemhome:键设置为您在~/.gemrc中的主目录中的路径. 关于通过MacPorts的RubyGems是个好主意吗?,我们在StackOverf