Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Generating Microsoft Office Documents with the Open XML SDK : Page 3

The Open XML SDK provides a comprehensive set of classes that make generating and manipulating Microsoft Office documents much simpler and faster than was possible with older Office file formats.


advertisement

Creating Styled Documents

This next example generates a slightly more complex Word document with two paragraphs, each containing text in a different font. Here's the process:

  1. Create the Word processing document package and add the main document part to it as in the previous example.
  2. Create the first paragraph containing the text "Hello World," but don't add it to the body yet:
  3. Text textFirstLine1 = new Text("Hello World"); Run run1 = new Run(textFirstLine1); Paragraph para1 = new Paragraph(run1);

  4. Create a second paragraph containing the text "Hello Open XML Community:"


  5. Text textFirstLine2 = new Text("Hello Open XML Community"); Run run2 = new Run(textFirstLine2); Paragraph para2 = new Paragraph(run2);

  6. You apply font formatting to a Run. Runs can contain a RunProperties object, which control the formatting applied to the text contained in that Run. Runs that don't have RunProperties use Word's default font. You can create RunProperties objects independently and then apply them to any Run. The following code creates a RunProperties object that causes the Run to display its text using the Arial Black font:
  7. RunProperties runProp = new RunProperties(); RunFonts runFont = new RunFonts(); runFont.Ascii = "Arial Black"; runProp.Append(runFont);

  8. Apply the RunProperties object you just created to the Run run2 you created earlier:
  9. run2.PrependChild<RunProperties>(runProp);

  10. Create a new Body instance and append both paragraphs:
  11. Body body = new Body(); body.Append(para1); body.Append(para2);

  12. Add the body to the document. Save and close the package:
  13. mainPart.Document.Append(body); mainPart.Document.Save(); doc.Close();

When you execute this program, it will create a document containing two paragraphs with different fonts (see Figure 2).

 
Figure 2. Styled Text: This two-paragraph document contains formatted text.

Search and Replace Text in a Word Document

Creating new documents addresses only one aspect of working with OOXML documents. This next example opens an existing document, searches for some text in that document, and replaces it with other text. This is typical of scenarios where you want to generate a large number of documents based on a small template: You'd read the template, replace some portion of the template with custom content, and then save the altered document. (You'll see a large-template scenario in the next section).

First, open the document using WordprocessingDocument class and get the MainDocumentPart. The document in this example is named SearchAndReplace.docx:

WordprocessingDocument doc = WordprocessingDocument.Open( @"SearchAndReplace.docx", true); MainDocumentPart mainPart = doc.MainDocumentPart;

Read the entire document contents using the GetStream method:

using (StreamReader sr = new StreamReader( doc.MainDocumentPart.GetStream())) { docText = sr.ReadToEnd(); }

At the end of this process, the docText variable contains all the XML for the document text. Next, replace contents in the docText variable as needed. For this example the template contains the text "The current version of [sdk] is [VersionNumber]."

The task is to replace the [sdk] and [VersionNumber] placeholders with actual values. You can use standard .NET string-manipulation code to make the replacement, so I won't show it here. After replacing the text, write the complete text back to the Main Document part using the following code:

using (StreamWriter sw = new StreamWriter(doc.MainDocumentPart.GetStream( FileMode.Create))) { sw.Write(docText); }



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap