Solved

Opening an XML file in Excel to view it

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

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
 

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
 

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now