?
Solved

Help with converting xml file to Excel using VB.NET

Posted on 2015-01-27
13
Medium Priority
?
169 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
Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

 
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 2000 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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 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