使用c#在word文档中查找字符串并替换为HTML内容

Find string and replace it with HTML content in word document using c#

我想用word文档中的字符串替换HTML内容。我已经将 HTML 内容添加到 word 文档,但我想添加 HTML 内容以替换 C# MVC 中的特定字符串。 下面是我的代码:

    public static void addhtmltodocx() //This function add HTML content end of the the word document
    {
        using (WordprocessingDocument myDoc = WordprocessingDocument.Open(@"C:\Users27858\Desktop\test.docx", true))
        {
            string html =
            @"<html>
                <head/>
                <body>
                    <b>Hello</b>
                </body>
            </html>";

            string altChunkId = "AltChunkId"+21;
            MainDocumentPart mainPart = myDoc.MainDocumentPart;
            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart("application/xhtml+xml", altChunkId);

            using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
            using (StreamWriter stringStream = new StreamWriter(chunkStream))
                stringStream.Write(html);

            AltChunk altChunk = new AltChunk();
            altChunk.Id = altChunkId;

            mainPart.Document.Body
                .InsertAfter(altChunk, mainPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last());
            
            mainPart.Document.Save();
        }
    }

假设下面是我的word文件内容:

test1
test2  // I want to replace test2 with the html content.
test3

预期输出如下:

测试1
你好
测试 3

Find the string from word document and replace it with HTML data.

你能帮我找到字符串(test2)并替换它吗html内容。

示例代码:

    // To search and replace content in a document part.
    public static void SearchAndReplace(string document)
    {
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
        {
            string docText = null;
            using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
            {
                docText = sr.ReadToEnd();
            }

            Regex regexText = new Regex("Hello world!");
            docText = regexText.Replace(docText, "Hi Everyone!");

            using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
            {
                sw.Write(docText);
            }
        }
    }

最后,我在 Nuget 包管理器 中找到了第三方 dll。这是示例代码解决方案。更多信息:VISIT

        string filename = @"C:\test.docx";
        Document document = new Document();
        document.LoadFromFile(filename);

        TextSelection[] selections1 = document.FindAllString("test2", true, true);
        //Here in first parameter you need pass the string which you want to replace.
        foreach (TextSelection selection1 in selections1)
        {
            TextRange range1 = selection1.GetAsOneRange();
            Paragraph paragraph = range1.OwnerParagraph;
            int index1 = paragraph.ChildObjects.IndexOf(range1);
            paragraph.AppendHTML("<b>Hello</b>");
            range1.OwnerParagraph.ChildObjects.Remove(range1);
        }
        document.SaveToFile(filename, FileFormat.Docx);
  • 导入以下命名空间。

     using System.Drawing;
     using Spire.Doc;
     using Spire.Doc.Documents;
     using Spire.Doc.Fields;
     using Document = Spire.Doc.Document;
     using Paragraph = Spire.Doc.Documents.Paragraph;
    

要实现此代码,请按照以下步骤操作:
步骤 -1:打开 Nuget 包管理器
步骤-2:在浏览选项卡中搜索 FreeSpire 并安装它。
步骤-3:现在添加上面的代码。就是这样享受代码。