none
How to convert an XLSX File to XML

    Question

  • I've made a project that reads xlsx files, but now I need to convert it to XML but I have no clue about how to do this.

    I would appreciate if you guys help me.

    Thank you.

    (if needed I can post my full code/project)

    Monday, February 25, 2013 1:49 PM

Answers

  • namespace Xls2Xml
    
    {
        class Program
        {
            static void Main(string[] args)
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile("test.xlsx", ExcelVersion.Version2010);
                workbook.SaveAsXml("sample.xml");
            }
        }
    }
    I use a C# Excel Library.
    • Marked as answer by guibovo Tuesday, February 26, 2013 2:04 PM
    Tuesday, February 26, 2013 3:31 AM

All replies

  • Basically an XLSX is already XML. Just use a zipper like 7-zip and unpack the file.. Otherwise you should specify what you exactly mean with "convert". E.g. load the data from your Excel via ADO.NET and store it directly as XML: How to transfer XML data to Microsoft Excel 2002 or Microsoft Excel 2003 by using Visual C#.
    Monday, February 25, 2013 2:08 PM
  • Basically an XLSX is already XML. Just use a zipper like 7-zip and unpack the file.. Otherwise you should specify what you exactly mean with "convert". E.g. load the data from your Excel via ADO.NET and store it directly as XML:

    Actually I just need the info I have in the excel file in xml format like this :

    excel file :

    EstimatedBeginTime | EstimatedEndTime       | VehiclePlate | ....
    21/07/2011 10:30:00 | 21/07/2011 16:00:00  | BMW-0001 | ....

    and returns XML like this :

    <Trip EstimatedBeginTime="21/07/2011 10:30:00"
     EstimatedEndTime="21/07/2011 16:00:00" VehiclePlate="BMW-0001">

    .....

       



    Observation : I need to do this programatcly, since in my application people will upload xlsx files and retrieve xml
    • Edited by guibovo Monday, February 25, 2013 2:36 PM
    Monday, February 25, 2013 2:28 PM
  • there will be different ways, simplest will be you read the excel file (either by excel dlls available in framework OR by OpenXML dlls), then read line by line. Then you can create your own format or XML document (by using XMLDocument or so). You can create attributes, elements etc based on your own requirment.

    regards

    joon

    Monday, February 25, 2013 2:57 PM
  • there will be different ways, simplest will be you read the excel file (either by excel dlls available in framework OR by OpenXML dlls), then read line by line. Then you can create your own format or XML document (by using XMLDocument or so). You can create attributes, elements etc based on your own requirment.

    regards

    joon

    I'm reading using OLEBD, using this line to connect

    "connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                        fileName + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";"

    The problem is, when I try to save as XML using this line 

       "             OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.12.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
                    OleDbDataAdapter da = new OleDbDataAdapter("select * from MyObject", con);
                    DataTable dt = new DataTable();
                    da.Fill(dt); "

    when I Debug with this lines, it gives me the error : Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

    but when I debug without this line, this error does not appear.

    note that I use the same line to connect befor, and no error is giving

    Monday, February 25, 2013 5:01 PM
  • Isn't this a completely different issue, than as your wrote in your first post? Where is your code actually failing?
    Monday, February 25, 2013 11:13 PM
  • Forget OLEDB.

    Use OpenXML (wich you can distribute easily to your clients) or at worst use ZipArchive wich let's you treat a zip file like a Folder.

    If you need the ability to process both XLS and XLSX, then the MS Interop Object is the way to go.

    If you want to use XML as Source for a Datatable, use the "ReadXML" and "WriteXML" overloads for that.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    Tuesday, February 26, 2013 12:03 AM
  • namespace Xls2Xml
    
    {
        class Program
        {
            static void Main(string[] args)
            {
                Workbook workbook = new Workbook();
                workbook.LoadFromFile("test.xlsx", ExcelVersion.Version2010);
                workbook.SaveAsXml("sample.xml");
            }
        }
    }
    I use a C# Excel Library.
    • Marked as answer by guibovo Tuesday, February 26, 2013 2:04 PM
    Tuesday, February 26, 2013 3:31 AM
  • >>when I Debug with this lines, it gives me the error : Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

    Is a 64 bit limitation.

    Try to set up the project to compile in x86 mode.

    ;-)

    Davide.

    Tuesday, March 05, 2013 1:58 PM