我喜欢从北风项目的客户表中创建 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");
}
}
}