代码之家  ›  专栏  ›  技术社区  ›  Larsi

替换Word文档中的字符串-在主文档中工作部件不在HeaderPart中

  •  2
  • Larsi  · 技术社区  · 15 年前

    我需要做一个简单的搜索并替换Word文档中的字符串。我以为这很容易,但不是(至少对我来说)

    签出此代码(它获取流,打开文档的不同部分,搜索字符串,然后替换它)。

    问题是,只保存主文档部分和页脚部分中的内容。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);
                    }
                }
            }
        }
    

    是的,如果有更简单的方法来替换Word文档中的字符串,请告诉我。

    谢谢你的帮助

    拉西

    3 回复  |  直到 13 年前
        1
  •  4
  •   Larsi    15 年前

    我最终使用了docx。它是一个很好的lib,并且有一个简单的替换函数。

    http://docx.codeplex.com/

        2
  •  2
  •   Adam Sheehan    15 年前

    当您对该部分调用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”,它可能无法通过简单的搜索和替换找到它。也许你的标题部分的文字是以一种奇怪的方式分开的。

        3
  •  2
  •   Edvaldo Silva    13 年前

    就像“maindocumentpart”有一个保存方法: maindocumentpart.document.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();
    
            }