替换Word文档中的字符串——在主文档部分而不是在HeaderPart中

时间:2021-03-05 19:26:20

I need to do a simple search and replace of a string in a word document. I thought it would be pretty easy, but it's not (at least for me)

我需要做一个简单的搜索并替换word文档中的字符串。我原以为这很容易,但事实并非如此(至少对我而言)

Check out this code (It takes a stream, opens the different part of the doc, searches for the string, and then it replaces it).

检查这段代码(它获取一个流,打开doc的不同部分,搜索字符串,然后替换它)。

Problem is that only whats inside the MainDocumentPart and the FooterPart is saved. The HeaderPart is not saved. Strange...

问题是只保存主文档部分和页脚部分中的内容。头部没有保存。奇怪的……

        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);
                }
            }
        }
    }

And yes if there are simpler ways of replacing a string in a word doc, please let me know.

如果有更简单的方法来替换word doc中的字符串,请告诉我。

Thanks for any help

感谢任何帮助

Larsi

Larsi

3 个解决方案

#1


4  

I ended up using DocX. It's a great lib, and has a simple Replace function.

最后我用了DocX。它是一个很棒的库,并且有一个简单的替换函数。

http://docx.codeplex.com/

http://docx.codeplex.com/

#2


2  

When you call GetStream() on the part, I believe it returns the entire XML structure of the part, not just the text area. And Microsoft Word sometimes splits words in strange places, so a string like

当您在该部分调用GetStream()时,我相信它将返回该部分的整个XML结构,而不仅仅是文本区域。微软Word有时会在陌生的地方分裂单词,就像字符串一样

Hello World!

might look like

的样子

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

So if you're trying to replace "Hello", it might not find it using a simple search and replace. Maybe the text in your header part is split up in a strange way like that.

因此,如果您试图替换“Hello”,它可能不会使用简单的搜索和替换找到它。也许你的标题部分的文字以一种奇怪的方式被分割。

#3


2  

Like the "MainDocumentPart" has a method to save: MainDocumentPart.Document.Save ();

就像“MainDocumentPart”有一个保存方法:MainDocumentPart. document。Save();

You should also call the Save method for Header: header.Header.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();

        }

#1


4  

I ended up using DocX. It's a great lib, and has a simple Replace function.

最后我用了DocX。它是一个很棒的库,并且有一个简单的替换函数。

http://docx.codeplex.com/

http://docx.codeplex.com/

#2


2  

When you call GetStream() on the part, I believe it returns the entire XML structure of the part, not just the text area. And Microsoft Word sometimes splits words in strange places, so a string like

当您在该部分调用GetStream()时,我相信它将返回该部分的整个XML结构,而不仅仅是文本区域。微软Word有时会在陌生的地方分裂单词,就像字符串一样

Hello World!

might look like

的样子

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

So if you're trying to replace "Hello", it might not find it using a simple search and replace. Maybe the text in your header part is split up in a strange way like that.

因此,如果您试图替换“Hello”,它可能不会使用简单的搜索和替换找到它。也许你的标题部分的文字以一种奇怪的方式被分割。

#3


2  

Like the "MainDocumentPart" has a method to save: MainDocumentPart.Document.Save ();

就像“MainDocumentPart”有一个保存方法:MainDocumentPart. document。Save();

You should also call the Save method for Header: header.Header.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();

        }