替换 Word 文档中的字符串 - 适用于 MainDocumentPart,不适用于 HeaderPart

发布于 2024-08-15 12:49:29 字数 1995 浏览 4 评论 0原文

我需要对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);
                }
            }
        }
    }

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

谢拉尔西的帮助

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)

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

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.

Thanks for any help

Larsi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

謌踐踏愛綪 2024-08-22 12:49:29

我最终使用了 DocX。这是一个很棒的库,并且有一个简单的替换功能。

http://docx.codeplex.com/

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

http://docx.codeplex.com/

霞映澄塘 2024-08-22 12:49:29

当您在部件上调用 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>

So 如果您尝试替换“Hello”,它可能无法使用简单的搜索和替换找到它。也许您的标题部分中的文本以这样奇怪的方式被分割。

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

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.

萝莉病 2024-08-22 12:49:29

就像“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();

        }

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

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

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文