Solved

Help with converting xml file to Excel using VB.NET

Posted on 2015-01-27
13
61 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 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

17 Experts available now in Live!

Get 1:1 Help Now