Hi.
I have successfully used OpenXml (2.5) to use a Word Template and live SQL data to create an Invoice.
I would like to be able to edit the contents of this invoice that have been dynamically generated by the process in the resulting Word document.
How would I go about doing this?
Here's the C#:
// Make a copy of the template file string templatePath = Server.MapPath("/admin/exports/templates/"); string generatedPath = Server.MapPath("/admin/exports/"); File.Copy(templatePath + "Quote_Template.docx", generatedPath + "Quote_" + chooseQuoteDropDownList.SelectedItem.Text + ".docx", true); //Open, populate and save the copied template word doc using (WordprocessingDocument theDoc = WordprocessingDocument.Open(generatedPath + "Quote_" + chooseQuoteDropDownList.SelectedItem.Text + ".docx", true)) { //Connect to database string connectionString = WebConfigurationManager.ConnectionStrings["*****"].ConnectionString; SqlConnection conn = new SqlConnection(connectionString); conn.Open(); // Open the doc MainDocumentPart mainPart = theDoc.MainDocumentPart; // Insert quote reference number: // taken simply from the SelectedValue of the initial DDL. SdtElement inline_01 = mainPart.Document.Body.Descendants<SdtElement>().Where (r => r.SdtProperties.GetFirstChild<Tag>().Val == "quoteRef").Single(); DocumentFormat.OpenXml.Wordprocessing.Text t2 = inline_01.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault(); t2.Text = chooseQuoteDropDownList.SelectedItem.Text; // Insert recipient's email address: // taken from database string commandString1 = "SELECT Email FROM aspnet_Membership INNER JOIN orders ON aspnet_Membership.UserId = orders.customer_id WHERE order_id = @order_id"; SqlCommand cmd1; cmd1 = new SqlCommand(commandString1, conn); cmd1.Parameters.AddWithValue("@order_id", chooseQuoteDropDownList.SelectedValue); string mailAddress = Convert.ToString(cmd1.ExecuteScalar()); SdtElement inline_02 = mainPart.Document.Body.Descendants<SdtElement>().Where (r => r.SdtProperties.GetFirstChild<Tag>().Val == "recipientEmail").Single(); DocumentFormat.OpenXml.Wordprocessing.Text t3 = inline_02.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault(); t3.Text = mailAddress; // Insert recipient's name: // taken from database string commandString2 = "SELECT firstname + ' ' + lastname as firstlast FROM user_extra_info INNER JOIN orders ON user_extra_info.UserId = orders.customer_id WHERE order_id = @order_id"; SqlCommand cmd2; cmd2 = new SqlCommand(commandString2, conn); cmd2.Parameters.AddWithValue("@order_id", chooseQuoteDropDownList.SelectedValue); string name = Convert.ToString(cmd2.ExecuteScalar()); SdtElement inline_03 = mainPart.Document.Body.Descendants<SdtElement>().Where (r => r.SdtProperties.GetFirstChild<Tag>().Val == "recipientTo").Single(); DocumentFormat.OpenXml.Wordprocessing.Text t4 = inline_03.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault(); t4.Text = name; // Insert recipient's company: // taken from database string commandString3 = "SELECT companyname FROM user_extra_info INNER JOIN orders ON user_extra_info.UserId = orders.customer_id WHERE order_id = @order_id"; SqlCommand cmd3; cmd3 = new SqlCommand(commandString3, conn); cmd3.Parameters.AddWithValue("@order_id", chooseQuoteDropDownList.SelectedValue); string company = Convert.ToString(cmd3.ExecuteScalar()); SdtElement inline_04 = mainPart.Document.Body.Descendants<SdtElement>().Where (r => r.SdtProperties.GetFirstChild<Tag>().Val == "recipientCompany").Single(); DocumentFormat.OpenXml.Wordprocessing.Text t5 = inline_04.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault(); t5.Text = company; // Insert recipient's telephone number: // taken from database string commandString4 = "SELECT telephone FROM user_extra_info INNER JOIN orders ON user_extra_info.UserId = orders.customer_id WHERE order_id = @order_id"; SqlCommand cmd4; cmd4 = new SqlCommand(commandString4, conn); cmd4.Parameters.AddWithValue("@order_id", chooseQuoteDropDownList.SelectedValue); string phone = Convert.ToString(cmd4.ExecuteScalar()); SdtElement inline_05 = mainPart.Document.Body.Descendants<SdtElement>().Where (r => r.SdtProperties.GetFirstChild<Tag>().Val == "recipientPhone").Single(); DocumentFormat.OpenXml.Wordprocessing.Text t6 = inline_05.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault(); t6.Text = phone; // Insert recipient's name after "Dear": // taken from database string commandString5 = "SELECT firstname FROM user_extra_info INNER JOIN orders ON user_extra_info.UserId = orders.customer_id WHERE order_id = @order_id"; SqlCommand cmd5; cmd5 = new SqlCommand(commandString5, conn); cmd5.Parameters.AddWithValue("@order_id", chooseQuoteDropDownList.SelectedValue); string firstname = Convert.ToString(cmd5.ExecuteScalar()); SdtElement inline_06 = mainPart.Document.Body.Descendants<SdtElement>().Where (r => r.SdtProperties.GetFirstChild<Tag>().Val == "recipientName").Single(); DocumentFormat.OpenXml.Wordprocessing.Text t7 = inline_06.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault(); t7.Text = firstname; // Insert Conversion Rate mid document: // taken from rate calculation at start of event SdtElement inline_07 = mainPart.Document.Body.Descendants<SdtElement>().Where (r => r.SdtProperties.GetFirstChild<Tag>().Val == "convRate").Single(); DocumentFormat.OpenXml.Wordprocessing.Text t8 = inline_07.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault(); t8.Text = rateEuro.ToString("0.##"); // Populate the main table SdtBlock ccWithTable = mainPart.Document.Body.Descendants<SdtBlock>().Where (r => r.SdtProperties.GetFirstChild<Tag>().Val == "tableMain").Single(); // This should return only one table. DocumentFormat.OpenXml.Wordprocessing.Table theTable = ccWithTable.Descendants<DocumentFormat.OpenXml.Wordprocessing.Table>().Single(); //Connect to database QuoteExportDataContext db = new QuoteExportDataContext(); var productListing = from admin_product in db.admin_products select new { admin_product.code, admin_product.product_title, admin_product.quantity, admin_product.unit_customer_cost, admin_product.total_customer_cost, admin_product.supplier_list_price }; // Get the last row in the table. DocumentFormat.OpenXml.Wordprocessing.TableRow theRow = theTable.Elements<DocumentFormat.OpenXml.Wordprocessing.TableRow>().Last(); decimal total = 0; decimal total_pd = 0; int counter = 1; foreach (var product in productListing) { DocumentFormat.OpenXml.Wordprocessing.TableRow rowCopy = (DocumentFormat.OpenXml.Wordprocessing.TableRow)theRow.CloneNode(true); // Counter rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(0).RemoveAllChildren(); rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(0).Append( new TableCellProperties( new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center } ), new Paragraph( new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), new DocumentFormat.OpenXml.Wordprocessing.Run( new RunFonts { Ascii = "Palatino Linotype" }, new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.FontSize { Val = "20" }), new DocumentFormat.OpenXml.Wordprocessing.Text(counter.ToString() + ")")) ) ); // Product Code rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(1).RemoveAllChildren(); rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(1).Append( new TableCellProperties( new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center } ), new Paragraph( new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), new DocumentFormat.OpenXml.Wordprocessing.Run( new RunFonts { Ascii = "Palatino Linotype" }, new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.FontSize { Val = "20" }), new DocumentFormat.OpenXml.Wordprocessing.Text(product.code.ToString())) ) ); // Product Title rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(2).RemoveAllChildren(); rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(2).Append( new TableCellProperties( new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center } ), new Paragraph( new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), new DocumentFormat.OpenXml.Wordprocessing.Run( new RunFonts { Ascii = "Palatino Linotype" }, new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.FontSize { Val = "20" }), new DocumentFormat.OpenXml.Wordprocessing.Text(product.product_title.ToString())) ) ); // Quantity rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(3).RemoveAllChildren(); rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(3).Append( new TableCellProperties( new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center } ), new Paragraph( new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), new DocumentFormat.OpenXml.Wordprocessing.Run( new RunFonts { Ascii = "Palatino Linotype" }, new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.FontSize { Val = "20" }), new DocumentFormat.OpenXml.Wordprocessing.Text(product.quantity.ToString())) ) ); // Unit Customer Cost rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(4).RemoveAllChildren(); rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(4).Append( new TableCellProperties( new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center } ), new Paragraph( new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), new DocumentFormat.OpenXml.Wordprocessing.Run( new RunFonts { Ascii = "Palatino Linotype" }, new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.FontSize { Val = "20" }), new DocumentFormat.OpenXml.Wordprocessing.Text(string.Format("{0:f2}", currencySymbol + "" + (Convert.ToDecimal(product.supplier_list_price) * rate).ToString("#.##")))) ) ); // Total Customer Cost rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(5).RemoveAllChildren(); rowCopy.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>().ElementAt(5).Append( new TableCellProperties( new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center } ), new Paragraph( new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), new DocumentFormat.OpenXml.Wordprocessing.Run( new RunFonts { Ascii = "Palatino Linotype" }, new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.FontSize { Val = "20" }), new DocumentFormat.OpenXml.Wordprocessing.Text(string.Format("{0:f2}", currencySymbol + "" + ((Convert.ToDecimal(product.supplier_list_price) * Convert.ToDecimal(product.quantity)) * rate).ToString("#.##")))) ) ); theTable.AppendChild(rowCopy); // Add Total Cost total += (Convert.ToDecimal(product.supplier_list_price) * Convert.ToDecimal(product.quantity)) * rate; total_pd += Convert.ToDecimal(product.total_customer_cost) * rate; counter++; } // Delivery Row int delCharge = Convert.ToInt32(deliveryDropDownList.SelectedValue); DocumentFormat.OpenXml.Wordprocessing.TableRow totalRow_Del = new DocumentFormat.OpenXml.Wordprocessing.TableRow(); DocumentFormat.OpenXml.Wordprocessing.TableCell tc1_del = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph()); tc1_del.Append( new TableCellProperties( new TableCellBorders( new DocumentFormat.OpenXml.Wordprocessing.BottomBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.TopBorder { Val = BorderValues.None, Size = (UInt32Value)1U } ))); DocumentFormat.OpenXml.Wordprocessing.TableCell tc2_del = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph()); tc2_del.Append( new TableCellProperties( new TableCellBorders( new DocumentFormat.OpenXml.Wordprocessing.BottomBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.TopBorder { Val = BorderValues.None, Size = (UInt32Value)1U } ))); DocumentFormat.OpenXml.Wordprocessing.TableCell tc3_del = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph()); tc3_del.Append( new TableCellProperties( new TableCellBorders( new DocumentFormat.OpenXml.Wordprocessing.BottomBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.TopBorder { Val = BorderValues.None, Size = (UInt32Value)1U } ))); DocumentFormat.OpenXml.Wordprocessing.TableCell tc4_del = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph()); tc4_del.Append( new TableCellProperties( new TableCellBorders( new DocumentFormat.OpenXml.Wordprocessing.BottomBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.TopBorder { Val = BorderValues.None, Size = (UInt32Value)1U } ))); DocumentFormat.OpenXml.Wordprocessing.TableCell tc5_del = new DocumentFormat.OpenXml.Wordprocessing.TableCell( new TableCellProperties(new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }), new Paragraph( new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), new DocumentFormat.OpenXml.Wordprocessing.Run( new RunFonts { Ascii = "Palatino Linotype" }, new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.FontSize { Val = "20" }), new DocumentFormat.OpenXml.Wordprocessing.Text("Delivery:") ))); DocumentFormat.OpenXml.Wordprocessing.TableCell tc6_del = new DocumentFormat.OpenXml.Wordprocessing.TableCell( new TableCellProperties(new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }), new Paragraph( new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), new DocumentFormat.OpenXml.Wordprocessing.Run( new RunFonts { Ascii = "Palatino Linotype" }, new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.FontSize { Val = "20" }), new DocumentFormat.OpenXml.Wordprocessing.Text(currencySymbol + (string.Format("{0:f2}", delCharge))) ))); totalRow_Del.Append(tc1_del, tc2_del, tc3_del, tc4_del, tc5_del, tc6_del); theTable.AppendChild(totalRow_Del); // Total Row DocumentFormat.OpenXml.Wordprocessing.TableRow totalRow_Tot = new DocumentFormat.OpenXml.Wordprocessing.TableRow(); DocumentFormat.OpenXml.Wordprocessing.TableCell tc1_tot = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph()); tc1_tot.Append( new TableCellProperties( new TableCellBorders( new DocumentFormat.OpenXml.Wordprocessing.BottomBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.TopBorder { Val = BorderValues.None, Size = (UInt32Value)1U } ))); DocumentFormat.OpenXml.Wordprocessing.TableCell tc2_tot = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph()); tc2_tot.Append( new TableCellProperties( new TableCellBorders( new DocumentFormat.OpenXml.Wordprocessing.BottomBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.TopBorder { Val = BorderValues.None, Size = (UInt32Value)1U } ))); DocumentFormat.OpenXml.Wordprocessing.TableCell tc3_tot = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph()); tc3_tot.Append( new TableCellProperties( new TableCellBorders( new DocumentFormat.OpenXml.Wordprocessing.BottomBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.TopBorder { Val = BorderValues.None, Size = (UInt32Value)1U } ))); DocumentFormat.OpenXml.Wordprocessing.TableCell tc4_tot = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph()); tc4_tot.Append( new TableCellProperties( new TableCellBorders( new DocumentFormat.OpenXml.Wordprocessing.BottomBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.LeftBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.RightBorder { Val = BorderValues.None, Size = (UInt32Value)1U }, new DocumentFormat.OpenXml.Wordprocessing.TopBorder { Val = BorderValues.None, Size = (UInt32Value)1U } ))); DocumentFormat.OpenXml.Wordprocessing.TableCell tc5_tot = new DocumentFormat.OpenXml.Wordprocessing.TableCell( new TableCellProperties(new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }), new Paragraph( new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), new DocumentFormat.OpenXml.Wordprocessing.Run( new RunFonts { Ascii = "Palatino Linotype" }, new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.FontSize { Val = "20" }), new DocumentFormat.OpenXml.Wordprocessing.Text("TOTAL:") ))); DocumentFormat.OpenXml.Wordprocessing.TableCell tc6_tot = new DocumentFormat.OpenXml.Wordprocessing.TableCell( new TableCellProperties(new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center }), new Paragraph( new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), new DocumentFormat.OpenXml.Wordprocessing.Run( new RunFonts { Ascii = "Palatino Linotype" }, new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.FontSize { Val = "20" }), new DocumentFormat.OpenXml.Wordprocessing.Text(currencySymbol + (string.Format("{0:f2}", total_pd + delCharge))) ))); totalRow_Tot.Append(tc1_tot, tc2_tot, tc3_tot, tc4_tot, tc5_tot, tc6_tot); theTable.AppendChild(totalRow_Tot); // Remove the empty placeholder row from the table. theTable.RemoveChild(theRow); // Save the changes to the table back into the document. mainPart.Document.Save(); }
Thanks in advance.