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

C# 使用c在word文档中查找字符串并将其替换为HTML内容#,c#,.net,asp.net-mvc,model-view-controller,C#,.net,Asp.net Mvc,Model View Controller,我想用word文档中的字符串替换HTML内容。我已经在word文档中添加了HTML内容,但我想添加HTML内容来替换C#MVC中的特定字符串。 下面是我的代码: public static void addhtmltodocx() //This function add HTML content end of the the word document { using (WordprocessingDocument myDoc = WordprocessingDo

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

    public static void addhtmltodocx() //This function add HTML content end of the the word document
    {
        using (WordprocessingDocument myDoc = WordprocessingDocument.Open(@"C:\Users\1527858\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();
        }
    }
预期产出如下:

测试1
你好
测试3

从word文档中查找字符串并将其替换为HTML数据

您能否帮助我查找字符串(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);
            }
        }
    }

最后,我找到了第三方dll,它在Nuget package manager中可用。下面是示例代码解决方案。更多信息:

要实现此代码,请执行以下步骤:
步骤1:打开Nuget软件包管理器
步骤2:在浏览选项卡中搜索并安装FreeSpire
步骤3:现在添加上面的代码。这就是我喜欢的代码。

我试过这个代码。使用这段代码如果我找到test2,然后用hello替换,那么word文件就会损坏。这部分代码用于查找和替换docx中的简单字符串。我想将HTML内容替换为word文件。你能帮我在word文件中添加HTML内容并替换test2吗@我不确定您想要实现什么,但是单词是XML而不是HTML。只需将word重命名为.zip,打开它并找到文本所在的document.xml,您就会看到节点。如果您想替换标记,请尝试使用如下正则表达式找到它们:.*?|并创建一个条件,以确保存在“html”“body”“div”或任何您正在查找的内容,否则您会弄乱您的单词,因为XML也使用<>标记格式化。我已经提供了将html内容添加到word文件中的部分代码。但在我的代码中,我在页面的最后一部分添加了HTML内容,但我希望它位于页面的中间@JuangMaDad在文档中间有一个标签{HTMLCyt},找到HTML并用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);
            }
        }
    }
        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;