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
Solved

Help with converting xml file to Excel using VB.NET

Posted on 2015-01-27
13
84 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

791 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