Help with saving xml file as an excel using VB.NET


 Is there a way to save an xml file as an excel file (.xls/.xlsx)?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Are you sure you want to do this?

The XLSX format is very complicated. See here

If you are just saving a table of values, you probably want to just save as a CSV file, which excel natively opens.

If you want to use interop (and you already have excel loaded on the target machines), you'd do somwthing like this

Dim ws As New Microsoft.Office.Interop.Excel.Worksheet
ws.SaveAs("c:\" & whatever)
vcharlesAuthor Commented:

Looking at your code, where  do you include the xml file?

what format is your data in right now?
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

vcharlesAuthor Commented:
It's  in xml.
What I meant is what kind of data is it? For example, you can easily export a 2d array of strings and numbers as CSV. If there are charts and graphs, you will have to massage it into the xlsx format.
vcharlesAuthor Commented:
Included  in the xml are plain text and numbers, no charts and graphs are included.
Can you open the file in excel directly? I mean, Open | Import XML..
vcharlesAuthor Commented:
I'm creating the xml file from the application by running a Query and filtering a table, would than like to save the data (results) in excel format.
ok, so I think the best way to accomplish what you are looking for is to use interop services. Make sure that you have excel loaded on the machine that you are using.

Here is an example, borrowed from

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 = New Microsoft.Office.Interop.Excel.Application()

        If xlApp Is Nothing Then
            MessageBox.Show("Excel is not properly installed!!")
        End If

        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlWorkSheet.Cells(1, 1) = "Sheet 1 content"

        xlWorkBook.SaveAs("d:\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, _
         Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
        xlWorkBook.Close(True, misValue, misValue)


        MessageBox.Show("Excel file created , you can find the file d:\csharp-Excel.xls")
    End Sub

    Private Sub releaseObject(ByVal obj As Object)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        End Try
    End Sub
End Class

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.