Solved

Help with converting xml file to Excel using VB.NET

Posted on 2015-01-27
13
137 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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