2016-12-23 43 views
-3

文書を開いてストリームにコピーします。OpenXMLを使用して文書内のテキストを置換する

ストリームの前にドキュメントのテキストを置き換えることはできますか?

//wordTemplate - var with path to my word template 
byte[] result = null; 
byte[] templateBytes = System.IO.File.ReadAllBytes(wordTemplate); 
using (MemoryStream templateStream = new MemoryStream()) 
{ 
    templateStream.Write(templateBytes, 0, (int)templateBytes.Length); 
    using (WordprocessingDocument doc = WordprocessingDocument.Open(templateStream, true)) 
    { 
     MainDocumentPart mainPart = doc.MainDocumentPart; 

     mainPart.Document.Save(); 
     templateStream.Position = 0; 
     using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      templateStream.CopyTo(memoryStream); 
      result = memoryStream.ToArray(); 
     } 
    } 
} 
+0

この情報はお役に立ちましたか? .. http://stackoverflow.com/questions/18744660/find-and-replace-text-in-xml-file-using-c-sharp – NMathur

+0

ターゲット - オープンワード文書、いくつかの文字列を変更し、ユーザーのためにダウンロードのために送信する – Fullbalanced

+0

'Regex'を使ってテキストを置き換えることができます。それを 'WordprocessingDocument'の' StreamReader.ReadToEnd() 'に渡してください。注:これのほとんどを書き直す必要があります。あなたが最も簡単な方法に接近していないように見えるからです。 – gravity

答えて

0

代わりのFile.ReadAllBytes()を呼び出すと、なぜ代わりにFile.ReadAllLines(string)を呼び出しませんか?それはあなたにあなたの心の喜びを検索して置き換えることができる文字列の配列を提供します。

+0

ドキュメントをdocx形式で開き、ReadAllLinesを使用してWord文書の文字列を置き換える方法を教えてください。 – Fullbalanced

関連する問題