繁体   English   中英

使用.net核心查找和替换现有词.doc

[英]Find and replace using existing word .doc with .net core

我正在尝试查找并替换我创建并添加到.net核心项目的Word文档模板中的特定单词/字符串。

我目前正在使用NPOI进行此操作,但由于我的C#知识不是很好,我似乎无法弄清楚!

从理论上讲,我想阅读现成的.docx模板,然后替换需要的内容然后保存。

我已经尝试了所有可能想到的方法,但是仍然无法正常工作。

这里剩下的代码是:

var docUrl = @"App_Data/Document_Template.docx";

using (FileStream file = new FileStream(docUrl, FileMode.Open, FileAccess.Read))
        {
            XWPFDocument doc = new XWPFDocument(file);

            // get the document template using docUrl and replace string with another string.

            doc.Write(file);

        }

任何帮助都是很棒的,因为我花了很多时间研究并尝试实现这一点,而没有运气!

要替换MS Word中的特定单词,首先应从nuget中添加以下参考:

Install-Package Microsoft.Office.Interop.Word

此示例代码将指定的字符串替换为另一个:

namespace MSWordReplacement
{
    class Program
    {
        static void Main(string[] args)
        {
            Application app = new Application();
            Document doc = app.Documents.Open(@"C:\Sample.docx");

            // Assign a search string to a variable.
            object findText = "Find me.";

            // Clear formatting from previous searches.
            app.Selection.Find.ClearFormatting();

            if (app.Selection.Find.Execute(ref findText))
            {
                // Replace new text.
                app.Selection.Text = "You found me!";
            }
            else
            {
                // Do somthing else.
            }

            doc.Close(true);
            app.Quit();
        }
    }
}

作为警告,您应该知道此程序包正在工作,但可能与点网核心不完全兼容!

有关更多信息,请使用此链接:以编程方式搜索和替换文档中的文本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM