2016-08-25 59 views
0

我需要幫助這個.... 我有Excel文件的數據。 我需要將它導出到XMLFILE,使用XML文本在VB.NET
我需要一些XMLFILE AA結果:從Excel導出到XML使用XML文字+ VB.NET

<?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,值2,...等等 - 我讓他們從羅恩NUMER細胞i和列數= 2 在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作爲的XElement在主要的XDocument 我在這種情況下XElements使用像Variables.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> 

請給我寫信,我該如何重寫這個Loop?

+0

你不能在那裏放入任何循環語句,你只能使用LINQ'From .. Select'表達式。 –

+0

但我的情況如何看起來像LINQ而不是循環? –

回答

0

下面是使用LINQ和工作表以讀出的行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工作表內容。

+0

我認爲這是我需要的!非常感謝! –