Solved

Opening an XML file in Excel to view it

Posted on 2014-04-24
5
516 Views
Last Modified: 2014-04-25
I am trying to load and then excel with an xml file I have created.  I have made a start but I am a bit unfamiliar with excel's interop.

      
Dim Path as String = "C:\TextXML"          
Dim excelApp As New Microsoft.Office.Interop.Excel.Application
excelApp.Workbooks.OpenXML(Path)

Open in new window

0
Comment
Question by:Alyanto
  • 3
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40020396
What happens with this code? Is the file missing extension on purpose?
0
 
LVL 1

Author Comment

by:Alyanto
ID: 40020449
  Dim Path as String = "C:\TextXML.XML"          
  Dim excelApp As New Microsoft.Office.Interop.Excel.Application
  excelApp.Workbooks.OpenXML(Path)

Open in new window


The code seems to do nothing not even throw an error.  I have been looking for code and this format appears several times as a solution in one question or another for opening XML in Excel 2010 via VB.Net.  Obviously there is an element missing or it is inappropriate for this problem
0
 
LVL 12

Accepted Solution

by:
jitendra patil earned 500 total points
ID: 40021909
Hi Alyanto try the below link code
How to create an Excel file from XML

hope this helps.
0
 
LVL 1

Author Comment

by:Alyanto
ID: 40022069
FMI:

Imports System.Xml
Imports System.Data
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        Dim ds As New DataSet
        Dim xmlFile As XmlReader
        Dim i, j As Integer

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        xmlFile = XmlReader.Create("Product.xml", New XmlReaderSettings())
        ds.ReadXml(xmlFile)

        For i = 0 To ds.Tables(0).Rows.Count - 1
            For j = 0 To ds.Tables(0).Columns.Count - 1
                xlWorkSheet.Cells(i + 1, j + 1) = _
                ds.Tables(0).Rows(i).Item(j)
            Next
        Next

        xlWorkSheet.SaveAs("xml2excel.xlsx")
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)
    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class

Open in new window

0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 40022070
Spot in answer, Cheers ;)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question