打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
VBA宏,可以用Excel来生成XML文件

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Set xmlDoc_pvt = CreateObject("MSXML2.DOMDocument")
        Set rootNode = xmlDoc_pvt.createElement("HP_Scan_Iterm")
        Set xmlDoc_pvt.DocumentElement = rootNode
        Set Header = xmlDoc_pvt.createProcessingInstruction("xml", "version='1.0' encoding='Unicode'")
        xmlDoc_pvt.InsertBefore Header, xmlDoc_pvt.ChildNodes(0)
      
        xmlDoc_pvt.Save ThisWorkbook.Path & "\HP_Scan_Iterm.xml"
        Set xmlDoc_pvt = Nothing
       
        writeSummaryData (ThisWorkbook.Path & "\HP_Scan_Iterm.xml")
       
        Call StrConv(ThisWorkbook.Path & "\HP_Scan_Iterm.xml", vbUnicode)
End Sub

Public Function writeSummaryData(strXMLfileSpec)

        Set xmlDoc_pvt = CreateObject("MSXML2.DOMDocument")
        xmlDoc_pvt.Load strXMLfileSpec
        Set Root = xmlDoc_pvt.DocumentElement
         maxCol = Worksheets(1).Range("IV1").End(xlToLeft).Column
         For colIndex = 2 To maxCol
                strTestSum = "OS"
                strAtt = Worksheets(1).Cells(1, colIndex).Value
                'Set parent_Comp = xmlDoc_pvt.DocumentElement.SelectSingleNode(strTestSum)
                        Set new_node = xmlDoc_pvt.createElement(strTestSum)
                        Set Att = xmlDoc_pvt.createAttribute("Version")
                        Att.Value = strAtt
                        new_node.setAttributeNode (Att)
                        Set parent_Comp = xmlDoc_pvt.DocumentElement.appendChild(new_node)
                sheetCount = Worksheets.Count
                For i = 1 To sheetCount
                Dim tabName As String
                tabName = Worksheets(i).Name
                Call writeArgField(xmlDoc_pvt, parent_Comp, tabName, strAtt)
                Next
        Next
        xmlDoc_pvt.Save strXMLfileSpec
        Set xmlDoc_pvt = Nothing
End Function

Public Sub writeArgField(xmlDoc_pvt, parent_Comp, tabName, deviceName)
        'Create the new Field node.
        Set new_node = xmlDoc_pvt.createElement(tabName)
       Set parent_Step = parent_Comp.appendChild(new_node)
        ' Create the sub-Field nodes.
        maxRow = Worksheets(tabName).Range("A65535").End(xlUp).Row
        colIndex = getColNumber(deviceName)
        For i = 2 To maxRow
       
        strTestSum = "Value"
                strAtt = Worksheets(tabName).Cells(i, 1).Value
              ' Set Parent = xmlDoc_pvt.DocumentElement.SelectSingleNode(strTestSum)
                        Set MyNode = xmlDoc_pvt.createElement(strTestSum)
                       Set Att = xmlDoc_pvt.createAttribute("Name")
                        Att.Value = strAtt
                       MyNode.setAttributeNode (Att)
       
        MyNode.Text = Worksheets(tabName).Cells(i, colIndex).Value
       parent_Step.appendChild MyNode
        Next
End Sub

Public Function getColNumber(DName)
maxCol = Worksheets(1).Range("IV1").End(xlToLeft).Column
CIndex = 1
For i = 2 To maxCol
CIndex = CIndex + 1
If DName = Worksheets(1).Cells(1, i).Value Then
getColNumber = CIndex
Exit Function
End If
Next
End Function

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
XMLHTTP资料
XML DOm方法备忘录
使用js解析xml文档和xml字符串(ie和火狐)
主题:JavaScript解析XML知识点总结
我的XML学习笔记
简单了解JavaScript操作XPath的一些基本方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服