2

Word 文書内の文字列を単純に検索して置換する必要があります。私はそれがかなり簡単だと思っていましたが、そうではありません(少なくとも私にとっては)

このコードを確認してください (ストリームを受け取り、ドキュメントの別の部分を開き、文字列を検索してから置換します)。

問題は、MainDocumentPart と FooterPart の内部のみが保存されることです。HeaderPart は保存されません。変...

        public static void ProcessDocument(Dictionary<string, string> properties, Stream fs)
    {
        using (WordprocessingDocument doc = WordprocessingDocument.Open(fs, true))
        {
            string docText = null;
            using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
            {
                docText = sr.ReadToEnd();
            }
            docText = DoTheReplace(properties, docText);
            using (StreamWriter sw = new StreamWriter(doc.MainDocumentPart.GetStream(FileMode.Create)))
            {
                sw.Write(docText);
            }
            foreach (FooterPart footer in doc.MainDocumentPart.FooterParts)
            {
                string footerText = null;
                using (StreamReader sr = new StreamReader(footer.GetStream()))
                {
                    footerText = sr.ReadToEnd();
                }
                footerText = DoTheReplace(properties, footerText);
                using (StreamWriter sw = new StreamWriter(footer.GetStream(FileMode.Create)))
                {
                    sw.Write(footerText);
                }
            }
            foreach (HeaderPart header in doc.MainDocumentPart.HeaderParts)
            {
                string headerText = null;
                using (StreamReader sr = new StreamReader(header.GetStream()))
                {
                    headerText = sr.ReadToEnd();
                }
                headerText = DoTheReplace(properties, headerText);
                using (StreamWriter sw = new StreamWriter(header.GetStream(FileMode.Create)))
                {
                    sw.Write(headerText);
                }
            }
        }
    }

はい、単語ドキュメントの文字列を置き換える簡単な方法があれば、教えてください。

助けてくれてありがとう

ラルシ

4

3 に答える 3

3

最終的にDocXを使用しました。これは優れたライブラリであり、単純な置換機能があります。

http://docx.codeplex.com/

于 2009-12-18T13:40:28.780 に答える
2

「MainDocumentPart」のように、保存するメソッドがあります。MainDocumentPart.Document.Save();

また、HeaderのSaveメソッドを呼び出す必要があります:header.Header.Save();

        foreach (HeaderPart header in doc.MainDocumentPart.HeaderParts)
        {
            string headerText = null;
            using (StreamReader sr = new StreamReader(header.GetStream()))
            {
                headerText = sr.ReadToEnd();
            }
            headerText = DoTheReplace(properties, headerText);
            using (StreamWriter sw = new StreamWriter(header.GetStream(FileMode.Create)))
            {
                sw.Write(headerText);
            }

            //Save Header
            header.Header.Save();

        }
于 2011-07-29T00:46:52.340 に答える
2

パーツで GetStream() を呼び出すと、テキスト領域だけでなく、パーツの XML 構造全体が返されると思います。また、Microsoft Word は単語を変な場所で分割することがあるので、次のような文字列

Hello World!

のように見えるかもしれません

<w:p><w:r><w:t>Hel</w:t><w:t>lo </w:t><w:t>World!</w:t></w:r><w:p>

そのため、「Hello」を置き換えようとしても、単純な検索と置換では見つからない可能性があります。ヘッダー部分のテキストがそのように奇妙な方法で分割されている可能性があります。

于 2009-12-16T21:52:15.343 に答える