Solved

Opening an XML file in Excel to view it

Posted on 2014-04-24
5
517 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

679 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