0

我喜欢从北风项目的客户表中创建 excel 文件。我已经使用了下面的代码,我想知道我必须如何保存 excel 文件以及它将保存在哪里。

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XNamespace ns = "urn:schemas-microsoft-com:office:spreadsheet";
            XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
            XNamespace x = "urn:schemas-microsoft-com:office:excel";
            XNamespace x2 = "http://schemas.microsoft.com/office/excel/2003/xml";
            XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
            XNamespace o = "urn:schemas-microsoft-com:office:office";
            XNamespace html = "http://www.w3.org/TR/REC-html40";
            XNamespace c = "urn:schemas-microsoft-com:office:component:spreadsheet";
             DataClasses1DataContext _DataContext;
            _DataContext = new DataClasses1DataContext();

            // Linq to XML - Document
            XDocument doc = new XDocument(
                new XDeclaration("1.0", "UTF-8", string.Empty),
                new XProcessingInstruction("mso-application", "progid=\"Excel.Sheet\""),
                new XElement(ns + "Workbook",
                    new XAttribute("xmlns", ns.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "x", x.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "x2", x2.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "ss", ss.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "o", o.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "html", html.NamespaceName),
                    new XAttribute(XNamespace.Xmlns + "c", c.NamespaceName),
                    new XElement(o + "OfficeDocumentSettings",
                        new XAttribute("xmlns", o.NamespaceName)),
                    new XElement(x + "ExcelWorkbook",
                        new XAttribute("xmlns", x.NamespaceName)),
                    new XElement("Worksheet",
                        new XAttribute(ss + "Name", "Sheet1"),
                        new XElement("Table", // 1st Table
                            new XElement("Row", // First Row
                                new XElement("Cell", // First Cell on First Row
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), "CompanyName") // Data in Cell A1
                                ),
                                new XElement("Cell",
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), "ContactTitle") // Data in Cell B1
                               ),
                                new XElement("Cell",
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), "Country") // Data in Cell C1
                                )


                            )
                        )
                    )
                )
            );

            // Loop through a collection. Each iteration is a new row
            foreach (Customer customer in _DataContext.Customers)
            {
                // Linq to XML - Data
                doc.Descendants("Row").First().AddAfterSelf(
                    new XElement("Row",
                        new XElement("Cell", // First Cell on First Row
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), customer.CompanyName) // Data in Cell A1
                                ),
                                new XElement("Cell",
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), customer.ContactTitle) // Data in Cell B1
                               ),
                                new XElement("Cell",
                                    new XElement("Data", new XAttribute(ss + "Type", "String"), customer.Country) // Data in Cell C1
                                )
                    )
                );
            }
            // Namespace fix. Deletes any empty xmlns="" text in every node.
            foreach (XElement e in doc.Root.DescendantsAndSelf())
            {
                if (e.Name.Namespace == string.Empty)
                {
                    e.Name = ns + e.Name.LocalName;
                }
            }
            doc.Save("e:\ahmed.xml");

        }
    }
}
4

1 回答 1

0

因此,您希望将 XML 数据保存在 Excel 工作表中。请参阅以下代码。您必须首先引用 Excel Libaray。

 object misValue = System.Reflection.Missing.Value;            
 Excel.Application myApp=new Excel.Application() ;
 Excel.Workbook myWbk = myApp.Workbooks.Add(misValue);
 Excel.Worksheet myWst = (Excel.Worksheet)myWbk.Worksheets.get_Item(1);

 //Load your XML
 DataSet ds = new DataSet();
 XmlReader xmlFile ;
 xmlFile = XmlReader.Create("e:\ahmed.xml", new XmlReaderSettings());
 ds.ReadXml(xmlFile);

 //Write your XML to Excel
 int i = 0;
 int j = 0;
 for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
    {
       for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
           {
              myWst.Cells[i + 1, j + 1] = ds.Tables[0].Rows[i].ItemArray[j].ToString();
           }
       }

链接: http: //www.codeproject.com/Tips/371595/Generate-Excel-from-XML-and-Generate-XML-from-Exce

于 2012-12-12T07:21:39.437 回答