using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Text.RegularExpressions; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; namespace ETS_Data { /// /// gestioen documetni office/xml /// public class officeXmlMan { /// /// fa sostituzioni di testo in un doc .docx /// /// /// /// public static void replaceDocxText(string fullPathFile, string textOrig, string textNew) { using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fullPathFile, true)) { #if false // lettura string docText = null; using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { docText = sr.ReadToEnd(); } // sostituzione Regex regexText = new Regex(textOrig); docText = regexText.Replace(docText, textNew); // scrittura using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) { sw.Write(docText); } #endif // uso questo codice: http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2011/05/12/148357.aspx SearchAndReplacer.SearchAndReplace(wordDoc, textOrig, textNew, true); } } } }