/// <summary> /// Generate WordprocessingML for Sales Report. /// The resulting XML will be saved as document.xml. /// </summary> /// <param name="stream">MainDocumentPart stream</param> public void CreateWordProcessingML(Stream stream) { // Get sales person data from AdventureWorks database // You will write this data to the document.xml file. AdventureWorksSalesData salesData = new AdventureWorksSalesData(); StringDictionary SalesPerson = salesData.GetSalesPersonData(_salesPersonID); // Create an XmlWriter using UTF8 encoding. XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; settings.Indent = true; // This file represents the WordprocessingML of the Sales Report. XmlWriter writer = XmlWriter.Create(stream, settings); try { writer.WriteStartDocument(true); writer.WriteComment("This file represents the WordProcessingML of our Sales Report"); writer.WriteStartElement(wordPrefix, "document", wordNamespace); writer.WriteStartElement(wordPrefix, "body", wordNamespace); WriteHeaderImage(writer); WriteDocumentTitle(writer, SalesPerson["FullName"]); WriteDocumentContactInfo(writer, SalesPerson["FullName"], SalesPerson["Phone"], SalesPerson["Email"]); WriteSalesSummaryInfo(writer, SalesPerson["SalesYTD"], SalesPerson["SalesQuota"]); WriteTerritoriesTable(writer, SalesPerson["TerritoryName"]); writer.WriteEndElement(); //body writer.WriteEndElement(); //document } catch (Exception e) { throw; } finally { //Write the XML to file and close the writer. writer.Flush(); writer.Close(); } return; }
/// <summary> /// Writes the territory sales totals as a table to the WordprocessingML document. /// </summary> /// <param name="writer">The XmlWriter to write the page break to.</param> private void WriteTerritoriesTable(XmlWriter writer, string territory) { string wordNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; string wordPrefix = "w"; // Create the territory section header. // <w:p> // <w:r> // <w:t>Sales by Territory</w:t> // </w:r> // </w:p> writer.WriteStartElement(wordPrefix, "p", wordNamespace); WriteSubtitleParagraphProperties(writer); ApplyParagraphStyle(writer, "Heading 3"); writer.WriteStartElement(wordPrefix, "r", wordNamespace); WriteSubtitleRunProperties(writer); writer.WriteElementString(wordPrefix, "t", wordNamespace, "Sales by Territory - " + territory); writer.WriteEndElement(); writer.WriteEndElement(); // Open the table element. writer.WriteStartElement(wordPrefix, "tbl", wordNamespace); ApplyTableStyle(writer, "LightList-Accent2"); // Write table headings. writer.WriteStartElement(wordPrefix, "tr", wordNamespace); writer.WriteStartElement(wordPrefix, "tc", wordNamespace); writer.WriteStartElement(wordPrefix, "p", wordNamespace); writer.WriteStartElement(wordPrefix, "r", wordNamespace); writer.WriteElementString(wordPrefix, "t", wordNamespace, "Employee"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteStartElement(wordPrefix, "tc", wordNamespace); writer.WriteStartElement(wordPrefix, "p", wordNamespace); writer.WriteStartElement(wordPrefix, "r", wordNamespace); writer.WriteElementString(wordPrefix, "t", wordNamespace, "2003"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteStartElement(wordPrefix, "tc", wordNamespace); writer.WriteStartElement(wordPrefix, "p", wordNamespace); writer.WriteStartElement(wordPrefix, "r", wordNamespace); writer.WriteElementString(wordPrefix, "t", wordNamespace, "2004"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); // write the close row writer.WriteEndElement(); // write a row for each territory AdventureWorksSalesData salesData = new AdventureWorksSalesData(); DataTable dt = salesData.GetSalesByTerritory(territory); foreach (DataRow myRow in dt.Rows) { writer.WriteStartElement(wordPrefix, "tr", wordNamespace); foreach (DataColumn myCol in dt.Columns) { writer.WriteStartElement(wordPrefix, "tc", wordNamespace); writer.WriteStartElement(wordPrefix, "p", wordNamespace); writer.WriteStartElement(wordPrefix, "r", wordNamespace); writer.WriteElementString(wordPrefix, "t", wordNamespace, myRow[myCol].ToString()); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); } // Write the close row. writer.WriteEndElement(); } // end the table element writer.WriteEndElement(); }