In the previous post, we gained access to the Open Office object model. In this post we will take the different parts and access XML to modify the contents of the document.
Once you have a part of the document, such as the MainDocumentPart or HeaderPart item, you can access the XML in a similar fashion:
StreamReader sr = new StreamReader(mainDoc.GetStream());
If you want to simply replace a block of text with another block of text, you can pull back the XML as a string:
String docText;
docText = sr.ReadToEnd();
Then create a Regular Expression to search for the text you want to replace:
Regex regexText = new Regex("Hello world!");
docText = regexText.Replace(docText, "Hi Everyone!");
To write back the results of your XML change, use an StreamWriter:
StreamWriter sw = new StreamWriter(mainDoc.GetStream(FileMode.Create));
sw.Write(docText);
You can also use XPath queries to gain access to certain nodes to delete blocks of text or add new blocks of text. XPath is a way to travel up and down an XML document and pull back the data you want. XPath is too deep a subject in this post, possibly in another post. With the release of .Net 3.5, in Visual Basic you can type inline XML so you could build the nodes of your document that way as well.
Once you write the data to the streams, don't forget to close the packages and then flush them to make sure that the data is written to the underlying stream and/or file.
Pack.Close();
Pack.Flush();
It is an involved process to build a word document programmatically but the benefits that the Office Open formats provide make it worthwhile to learn this new API and take advantage of it.
No comments:
Post a Comment