Reputation: 39
I am editing doc file using openxml in c# and want to save it. Changes is made but the file could not be save. when I open the file it doesn't show any changes made. I did it with follow code.
using (WordprocessingDocument doc = WordprocessingDocument.Open(source, true))
{
using (StreamReader reader = new StreamReader(doc.MainDocumentPart.GetStream()))
{
documentText = reader.ReadToEnd();
}
Body body = doc.MainDocumentPart.Document.Body;
documentText = documentText.Replace("##date##", "02/02/2014");
documentText = documentText.Replace("##saleno##", "2014");
documentText = documentText.Replace("##Email##", "abc");
documentText = documentText.Replace("##PhoneNo##", "9856321404");
doc.MainDocumentPart.Document.Save();
doc.Close();
}
Please help. Thanks.
Upvotes: 1
Views: 586
Reputation: 48435
You need to write your documentText
string (with the changes) back to the document, you can do this using StreamWriter as follows:
using (WordprocessingDocument doc = WordprocessingDocument.Open(source, true))
{
using (StreamReader reader = new StreamReader(doc.MainDocumentPart.GetStream()))
{
documentText = reader.ReadToEnd();
}
Body body = doc.MainDocumentPart.Document.Body;
documentText = documentText.Replace("##date##", "02/02/2014");
documentText = documentText.Replace("##saleno##", "2014");
documentText = documentText.Replace("##Email##", "abc");
documentText = documentText.Replace("##PhoneNo##", "9856321404");
// Write text to document.
using (StreamWriter writer = new StreamWriter(doc.MainDocumentPart.GetStream(FileMode.Create)))
{
writer.Write(documentText);
}
}
Upvotes: 1
Reputation: 23571
You are never changing the document itself you are replacing a string variable you read from the file with other string variables. The documentText variable is not magically connected to the text in the file. This is basic C#.
Code as shown in this tutorial.
// To search and replace content in a document part.
public static void SearchAndReplace(string document)
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("Hello world!");
docText = regexText.Replace(docText, "Hi Everyone!");
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
}
Note that they use a stream writer at the end to save things back to a file.
It is also worth noting that while this quick and dirty approach works often in simple find & replace situations there are more complex scenarios where you actually need to use the OOXML API. With the OOXML API you can traverse each part of the document as a tree of elements (much like HTML). You can add and remove items to the document. At this point a simple call to Save would be enough. To do this you would have to iterate through the parts, possibly recursively and modify the elements themselves. This is of course much more complex but it allows for things like repeating a template multiple times. You can refer to this SO question for an example of how to do a replace with the actual OOXML API
Upvotes: 2