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

时间:2016-08-25 11:19:51

标签: xml vb.net xml-literals

我需要帮助....     我有Excel数据文件。     我需要使用VB.NET中的XML Literals将其导出到XMLfile中     我需要一些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和Number Number = 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         我在这种情况下使用像变量这样的XElements.But现在我需要类似创建文档的东西,没有像这样的变量:

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>

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

1 个答案:

答案 0 :(得分:0)

这是一个使用LINQ和工作表来读出行1的第一列(1 .. 3)并填充一些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工作表内容。