0

I have this working code that parses values from XML files. Instead of writing the data to the console, how can I write data to an Excel spreadsheet? Any help please.

namespace TestCFG
{
    class Program
    {
        public class XAxisCalib
        {
            public int Max1 { get; set; }
            public int Min2 { get; set; }
            public int Max3 { get; set; }
            public int Min4 { get; set; }
            public int Max5 { get; set; }
            public int Min6 { get; set; }
        }

        static void Main(string[] args)
        {
            string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
            foreach (string fileName in fileEntries)
            {
                XDocument doc = XDocument.Load(fileName);
                var query = from x in doc.Descendants("XAxisCalib")
                            select new
                            {
                                //Max1 = x.Attribute("Max").Value,
                                //Min2 = x.Attribute("Min").Value
                                MaxChild = x.Descendants("Max"),
                                MinChild = x.Descendants("Min")
                            };

                foreach (var x in query)
                {
                    foreach (var nextLevel in x.MaxChild)
                    {
                        Console.WriteLine("XMax: " + nextLevel.Value);
                    }
                    foreach (var nextLevel in x.MinChild)
                    {
                        Console.WriteLine("XMin: " + nextLevel.Value);
                    }
                    //Console.WriteLine("XAxisCalib");
                }

                var query2 = from y in doc.Descendants("YAxisCalib")
                             select new
                             {
                                 //Max3 = x.Attribute("Max").Value,
                                 //Min4 = x.Attribute("Min").Value
                                 MaxChild = y.Descendants("Max"),
                                 MinChild = y.Descendants("Min")

                             };


                foreach (var y in query2)
                {
                    foreach (var nextLevel in y.MaxChild)
                    {
                        Console.WriteLine("YMax: " + nextLevel.Value);
                    }
                    foreach (var nextLevel in y.MinChild)
                    {
                        Console.WriteLine("YMin: " + nextLevel.Value);
                    }

                    //Console.WriteLine("YAxisCalib");

                    var query3 = from z in doc.Descendants("ZAxisCalib")
                                 select new
                                 {
                                     //Max5 = x.Attribute("Max").Value,
                                     //Min6 = x.Attribute("Min").Value
                                     MaxChild = z.Descendants("Max"),
                                     MinChild = z.Descendants("Min")
                                 };

                    foreach (var z in query3)
                    {
                        foreach (var nextLevel in z.MaxChild)
                        {
                            Console.WriteLine("ZMax: " + nextLevel.Value);
                        }
                        foreach (var nextLevel in z.MinChild)
                        {
                            Console.WriteLine("ZMin: " + nextLevel.Value);
                        }

                        //Console.WriteLine("ZAxisCalib");
                    }
                }
            }
        }
    }
}
Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • possible duplicate of [Create Excel (.XLS and .XLSX) file from C#](http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c) – stuartd Sep 09 '11 at 13:02

3 Answers3

0

Use the office API for that something like

using System;
using System.Reflection; 
using Microsoft.Office.Interop.Excel;

public class CreateExcelWorksheet
{
    static void Main()
    {
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

        if (xlApp == null)
        {
            Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
            return;
        }
        xlApp.Visible = true;

        Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        Worksheet ws = (Worksheet)wb.Worksheets[1];

        if (ws == null)
        {
            Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
        }

        // Select the Excel cells, in the range c1 to c7 in the worksheet.
        Range aRange = ws.get_Range("C1", "C7");

        if (aRange == null)
        {
            Console.WriteLine("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
        }

        // Fill the cells in the C1 to C7 range of the worksheet with the number 6.
        Object[] args = new Object[1];
        args[0] = 6;
        aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args);

        // Change the cells in the C1 to C7 range of the worksheet to the number 8.
        aRange.Value2 = 8;
    }
}

From

Kimtho6
  • 6,154
  • 9
  • 40
  • 56
0

There are several ways to create a spreadsheet from XML. - Use the Office API. This is a good, if heavy, approach. The API is very complex, overkill for simple operations, but necessary if you need formulas. - Write out Excel XML, even more complex. - Write out a CSV file, good for simple, non-formatted output. Watch-out for values with commas, etc. - Write out an HTML table, Excel will open everything in cells.

William Walseth
  • 2,803
  • 1
  • 23
  • 25
  • CSV = Comma Separated Value. It's a really simple text file format that you can create with notepad or .NET. ! record per row, fields separated by commas (,) put values in quotes ("") if you need to embed a comma in a field. See http://en.wikipedia.org/wiki/Comma-separated_values – William Walseth Sep 14 '11 at 14:32
0

Using Json.NET, I believe you can do something like this:

string json = JsonConvert.SerializeObject(table, Formatting.Indented);

Here's a link to Json.NET: http://json.codeplex.com/

James Johnson
  • 45,496
  • 8
  • 73
  • 110