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

Hello,

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

Thanks,

 Victor
vcharlesAsked:
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.

mankowitzCommented:
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)
0
vcharlesAuthor Commented:
Hi,

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

Thanks.
0
mankowitzCommented:
what format is your data in right now?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

vcharlesAuthor Commented:
It's  in xml.
0
mankowitzCommented:
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.
0
vcharlesAuthor Commented:
Included  in the xml are plain text and numbers, no charts and graphs are included.
0
mankowitzCommented:
Can you open the file in excel directly? I mean, Open | Import XML..
0
vcharlesAuthor Commented:
Hi,
.
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.
0
mankowitzCommented:
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 http://vb.net-informations.com/excel-2007/vb.net_excel_2007_create_file.htm

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!!")
            Return
        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)
        xlApp.Quit()

        releaseObject(xlWorkSheet)
        releaseObject(xlWorkBook)
        releaseObject(xlApp)

        MessageBox.Show("Excel file created , you can find the file d:\csharp-Excel.xls")
    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

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.
0
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.