什么是将XML中的112K行转换为Excel视图的最快(最少努力,而不是超级性能)方式.
如果您正在使用Excel 2007并希望使用XSLT,那么您最好的选择可能是使用
EXPath Zip Module功能来修改现有的Excel .xslx文件.
原文链接:https://www.f2er.com/xml/293149.html但是,我首选的选项是使用小型Excel VBA宏.
我在下面列出了一个名为’load’的VBA过程的示例代码 – 这个示例使用XML DOM,因此所有112K行的XML将首先加载到内存中,但如果性能不是问题,它比SAX更简单替代.
您需要修改xpathToExtractRow以适合您的XML输入结构.还假设XML行元素的直接子节点包含要作为文本节点导入的单元格数据,否则,您将需要使用SelectNode调用来获取所需的数据.
私有dom作为DOMDocument60
Public Sub load()
Dim nodeList As IXMLDOMNodeList Dim nodeRow As IXMLDOMNode Dim nodeCell As IXMLDOMNode Dim rowCount As Integer Dim cellCount As Integer Dim rowRange As Range Dim cellRange As Range Dim sheet As Worksheet Dim xpathToExtractRow As String xpathToExtractRow = "/Feed/row" Set dom = New DOMDocument60 dom.load ("c:\test\source.xml") Set sheet = ActiveSheet Set nodeList = dom.SelectNodes(xpathToExtractRow) rowCount = 0 For Each nodeRow In nodeList rowCount = rowCount + 1 cellCount = 0 For Each nodeCell In nodeRow.ChildNodes cellCount = cellCount + 1 Set cellRange = sheet.Cells(rowCount,cellCount) cellRange.Value = nodeCell.Text Next nodeCell Next nodeRow
结束子
示例输入XML:
<?xml version="1.0" encoding="utf-8"?> <Feed> <row> <firstname>joe</firstname> <lastname>smith</lastname> <country>jamaica</country> </row> <row> <firstname>bill</firstname> <lastname>coots</lastname> <country>uk</country> </row> </Feed>