【发布时间】:2021-08-11 21:59:33
【问题描述】:
我想用 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:\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();
}
}
假设下面是我的word文件内容:
test1
test2 // I want to replace test2 with the html content.
test3
预期输出如下:
test1
你好
测试3
从word文档中查找字符串并用HTML数据替换。
您能帮我找到字符串(test2)并将其替换为 html 内容吗?
【问题讨论】:
标签: c# .net asp.net-mvc model-view-controller