There is a document with content like "Hello [[username]]". You need to programmatically replace "[[username]]" with your text without spoiling the formatting and without using word.
- • github.com/OfficeDev/Open-XML-SDK • github.com/VahidN/EPPlus.Core • github.com/tonyqus/npoi - Alex78191
- oneWhy are you closing? The normal question is the same. - Pavel Mayorov
- "Without using Word" what does this mean? Below is the code using COM office. If you meant it, then smoke in OpenXML, and so the code works, it is checked. - Dmitry Chistik
- @Dmitry Chistik, I meant so that you can use the web application without the Word installed. - mirypoko
- if this is a docx file, then unpack it as a zip, look for the entry in xml files and change it to your own, pack it as a zip. It seems so, no openXML is needed at all then ... via the clumsy method - Dmitry Chistik
|
2 answers
A function that replaces text in a file by a regular expression. OpenXML used
public static void SearchAndReplace(string document, string regex, string newText) { 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(regex); docText = regexText.Replace(docText, newText); using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) { sw.Write(docText); } } } |
Add Microsoft.Office.Interop.Word to References and use replace(string fileName, string findText, string replaceText) from the code below
using Word = Microsoft.Office.Interop.Word; ... protected static void ReplaceWord(Word.Range range, object findText, object replaceText) { object matchCase = false; object matchWholeWord = true; object matchWildCards = false; object matchSoundsLike = false; object matchAllWordForms = false; object forward = true; object format = false; object matchKashida = false; object matchDiacritics = false; object matchAlefHamza = false; object matchControl = false; object read_only = false; object visible = true; object replace = Word.WdReplace.wdReplaceAll; object wrap = Word.WdFindWrap.wdFindContinue; range.Find.Execute(ref findText, ref matchCase, ref matchWholeWord, ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceText, ref replace, ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl); foreach (Word.Shape shape in range.ShapeRange) if (Convert.ToBoolean(shape.TextFrame.HasText).Equals(true)) ReplaceWord(shape.TextFrame.TextRange, findText, replaceText); } public static replace(string fileName, string findText, string replaceText) { Word.Application objWord = new Word.Application(); try { Word.Document doc = objWord.Documents.Open(fileName); foreach (Word.Range range in doc.StoryRanges) ReplaceWord(range, findText, replaceText); foreach (Word.Section section in doc.Sections) { foreach (Word.HeaderFooter headerFooter in section.Headers) ReplaceWord(headerFooter.Range, findText, replaceText); foreach (Word.HeaderFooter headerFooter in section.Footers) ReplaceWord(headerFooter.Range, findText, replaceText); } doc.Save(); object save = false; object missing = null; doc.Close(ref save, ref missing, ref missing); } catch (Exception ex) { } objWord.Quit(); } - one
catch (Exception ex) { }- that's why you need to tear off such hands - Pavel Mayorov - @PavelMayorov left for creativity ... - Dmitry Chistik
- Your example unfortunately does not fit the asp.net application - mirypoko
- -1 In no case do not use Office COM for websites. There is terrible performance and memory leaks. - Vadim Ovchinnikov
- one@Dmitry Chistik Here is an official source . Quote: "Microsoft doesn’t support, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and / or deadlock when Office is run in this environment. " - Vadim Ovchinnikov
|