使用 XML 文字 + VB.NET 从 Excel 导出到 XML

Export from Excel into XML using XML Literals + VB.NET

我需要帮助.... 我有 Excel 个包含数据的文件。 我需要将它导出到 XML 文件中,使用 XML Literals in VB.NET
我需要一些 XML-文件结果:

    <?xml version="1.0" standalone="no"?>
<soapenv:Envelope xmlns:soapenv="http://...">
    <soapenv:Header/>
    <soapenv:Body xmlns:wsu="http://....xsd">           
            <Header>
                <Verb>cancel</Verb> 
            </Header>
            <Request>
                <ID>VALUE-1</ID>
                <ID>VALUE-2</ID>
                <ID>VALUE-3</ID>
                ......
            </Request>
    </soapenv:Body>
</soapenv:Envelope>

其中 VALUE-1、VALUE-2、....等等 - 我从 Ron Numer i 和列号 = 2 的 Cells 中获取它们。 在循环中的 VB.NET 中,我是这样做的:

While Not this condition
    While Not (String.IsNullOrEmpty(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.Cells(iRow, 1).Value))
    'Check next condition with IF
                   If LCase(Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.Cells(iRow, 25).Value) = "y" Then
                   'Add XElement object into another one XML Element from main XDocument
                        objRequestCANCEL.Add(objIDCANCEL)
                        'Add Value for this new Element
                        objIDCANCEL.Value = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet.Cells(iRow, 2).Value
                   End If
                   'Go to next Row and do the same - add XELement if condition is OK here
    iRow = iRow + 1
    End While

此处 objIDCANCEL 作为主 XDocument 中的 XElement 我在这种情况下使用了像 Variables.But 这样的 XElements 现在我需要像没有变量的创建文档这样的东西:

Dim XDoc As XDocument = <?xml version="1.0" standalone="no"?>
                                  <soapenv:Envelope xmlns:soapenv="http://...">
                                        <soapenv:Header/>
                                        <soapenv:Body xmlns:wsu="http://....xsd">   
                                            <Header>
                                                <Verb>create</Verb>    
                                                <ID><%= varValue %> </ID>
                                                .....HERE I NEED ADD IN LOOP ALL VALUES FROM ALL ROWS IN EXCEL FILE.....
                                            </Header>
                                            <Request>
                                            </Request>
                                        </RequestMessage>
                                    </soapenv:Body>
                                </soapenv:Envelope>

请写信给我,我怎样才能在这里重写这个循环?

这是一个使用 LINQ 和工作sheet 读取行 1 .. 3 的第一列 (1) 并填充一些 XML 文字的简单示例:

    Dim excel As Excel.Application = New Excel.Application()
    excel.Workbooks.Open("sheet1.xlsx")
    Dim sheet As Excel._Worksheet = excel.ActiveWorkbook.ActiveSheet
    Dim cells As Excel.Range = sheet.Cells

    Dim doc As XDocument = <?xml version="1.0"?>
                           <root>
                               <values>
                                   <%= From i In Enumerable.Range(1, 3) Select <value>
                                                                                   <%= cells(i, 1).Value %>
                                                                               </value> %>
                               </values>

                           </root>

    doc.Save(Console.Out)

然后输出是例如

<root>
  <values>
    <value>1</value>
    <value>2</value>
    <value>3</value>
  </values>
</root>

根据需要根据您的 XML 格式和 Excel sheet 内容进行调整。