Solved

Help with converting xml file to Excel using VB.NET

Posted on 2015-01-27
13
80 Views
Last Modified: 2015-09-12
Hi,

How do you convert an xml file to excel using VB.NET?

Thanks,

Victor
0
Comment
Question by:vcharles
  • 6
  • 6
13 Comments
 
LVL 12

Expert Comment

by:FarWest
ID: 40573191
check this url, I think it will be helpful

https://msdn.microsoft.com/en-us/library/office/hh180830%28v=office.14%29.aspx

there is other ways of course such as using VISTO
0
 

Expert Comment

by:Kaushik Panja
ID: 40573212
Hi,
      This is a simple example to do the same.


XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
            XDocument sheet = XDocument.Load(@"map1.xml");
            foreach (XElement worksheet in sheet.Root.Elements(ss + "Worksheet"))
            {
                Console.WriteLine("Worksheet: {0}", worksheet.Attribute(ss + "Name").Value);
                foreach (XElement row in worksheet.Element(ss + "Table").Elements(ss + "Row"))
                {
                    foreach (XElement cell in row.Elements(ss + "Cell"))
                    {
                        Console.Write("{0}\t", cell.Value);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
0
 

Author Comment

by:vcharles
ID: 40573321
Thanks for all the comments, is it possible to send me a VB.NET version of the code from the last post.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 12

Expert Comment

by:FarWest
ID: 40573362
Dim ss As XNamespace = "urn:schemas-microsoft-com:office:spreadsheet"
Dim sheet As XDocument = XDocument.Load("map1.xml")
For Each worksheet As XElement In sheet.Root.Elements(ss + "Worksheet")
	Console.WriteLine("Worksheet: {0}", worksheet.Attribute(ss + "Name").Value)
	For Each row As XElement In worksheet.Element(ss + "Table").Elements(ss + "Row")
		For Each cell As XElement In row.Elements(ss + "Cell")
			Console.Write("{0}" & vbTab, cell.Value)
		Next
		Console.WriteLine()
	Next
	Console.WriteLine()
Next

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

Open in new window

0
 

Author Comment

by:vcharles
ID: 40573448
Hi,

Unfortunately the link was not much help, I'm using VB.NET. I tried the code from the last post but the Excel file does not display. How do I modify the code to create the excel file and save it as TextExcel.xls.

Thanks,

Victor
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40573637
by the way excel can open xml if it has a style sheet xlst, I'm away from computer now, I will get back to you few hours later, if you did not recieve any accepted solution

Open in new window

0
 

Author Comment

by:vcharles
ID: 40573678
I look forward to your solution.
Thanks,
V.
0
 
LVL 12

Accepted Solution

by:
FarWest earned 500 total points
ID: 40574679
Hi, check this url
http://support.microsoft.com/kb/319180
you can ignore all parts related to dataset, and start working from xml part

if you need any help, just send me a sample of your xml file
please note the solution depends on the xslt that describe your data
but if you need generic conversion (you don't know what this xml is)  then we need to go another approach that read xml and generate xlst from it automatically
BTW: there is a way that based on making a report that is bonded to xml data  and export that report to xml
0
 

Author Comment

by:vcharles
ID: 40574743
Thank You. How do you create the xlst?
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40574761
for a specific transform:
it depends, based on your xml source, if it is a dataset then can be auto generated
otherwise you should use the sample in the url and modify it manually,
0
 

Author Comment

by:vcharles
ID: 40575152
Hi,

I'm using a dataset to create the xml file, how do I create the xlst using the dataset?

 Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\APACCESS.mdb"
        ''Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\APT2002org.mdb;Persist Security Info=False;Jet OLEDB:Database Password=p9878tt"
        Dim objConnection As New OleDb.OleDbConnection(ConnectionString)
        Dim objDataAdapter1 As New OleDb.OleDbDataAdapter("select * from AP40Test order by NSN", objConnection)
        'dataset object
        Dim objDataSet1 As New DataSet
        'fill dataset
        '  objConnection.Open()
        objDataAdapter1.Fill(objDataSet1, "AOP40Test")
        'set dgv
        C1TrueDBGrid2.DataSource = objDataSet1
        C1TrueDBGrid2.DataMember = "AP40Test"

        For i As Integer = 0 To C1TrueDBGrid2.RowCount - 1
            On Error Resume Next
            For j As Integer = 0 To C1TrueDBGrid2.Columns.Count - 1
                If C1TrueDBGrid2(i, j).ToString() = Nothing Then
                    C1TrueDBGrid2(i, j) = "N/A"
                End If
            Next
        Next
        objDataSet1.WriteXml("C:\aop6test\AP40.xml")

Open in new window


Thanks,
Victor
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40575300
you can use DataTable.WriteXmlSchema method
0
 

Author Comment

by:vcharles
ID: 40575868
Hi,

Can you please send me an example.

Thanks,

Victor
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
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 …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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