开发者

Display values in Excel Spreadhseet rather than to console, Linq to xml, c#

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");
                    }
                }
            }
        }
    }
}


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


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.


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜