使用 Open XML SDK 实现 html 富文本转换为 docx 格式文档相对复杂。下面是一个示例。手动检测 <strong>和 <em> 标签并应用相应的文本格式。
using System; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; class Program { static void Main() { string htmlContent = "<p>This is <strong>bold</strong> and <em>italic</em> text.</p>"; // 创建一个新的docx文档 using (WordprocessingDocument doc = WordprocessingDocument.Create("output.docx", WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = doc.AddMainDocumentPart(); mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); // 解析HTML并创建docx段落 string[] paragraphs = htmlContent.Split(new[] { "<p>", "</p>" }, StringSplitOptions.RemoveEmptyEntries); foreach (string paragraphContent in paragraphs) { Paragraph paragraph = new Paragraph(); Run run = new Run(); string[] tags = paragraphContent.Split(new[] { "<strong>", "</strong>", "<em>", "</em>" }, StringSplitOptions.None); foreach (string tag in tags) { RunProperties runProperties = new RunProperties(); if (tag.Contains("<strong>")) { runProperties.Bold = new Bold(); } if (tag.Contains("<em>")) { runProperties.Italic = new Italic(); } run.Append(runProperties); run.Append(new Text(tag)); } paragraph.Append(run); body.Append(paragraph); } } Console.WriteLine("HTML to docx conversion complete."); } }
需要根据 HTML 标记的不同来创建相应的 docx元素,例如将<p>标签映射到docx段落,将<strong>标签映射到粗体等。
using System; using System.IO; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; class Program { static void Main() { string htmlContent = "<p>This is <strong>bold</strong> and <em>italic</em> text.</p>"; // 创建一个新的docx文档 using (WordprocessingDocument doc = WordprocessingDocument.Create("output.docx", WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = doc.AddMainDocumentPart(); mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); // 解析HTML内容并创建相应的docx元素 ProcessHtmlContent(htmlContent, body); doc.Save(); } Console.WriteLine("HTML to docx conversion complete."); } static void ProcessHtmlContent(string htmlContent, OpenXmlElement parentElement) { // 解析HTML内容并将其映射到docx元素 // 这里需要根据HTML标记的不同来创建相应的docx元素 // 例如,<p>标签可以映射到段落,<strong>可以映射到粗体文本等 // 示例:将HTML段落转换为docx段落 if (htmlContent.StartsWith("<p>") && htmlContent.EndsWith("</p>")) { string paragraphText = htmlContent.Substring(3, htmlContent.Length - 7); Paragraph paragraph = new Paragraph(new Run(new Text(paragraphText))); parentElement.Append(paragraph); } // 添加更多的HTML标记处理逻辑以满足你的需求 } }
标签:XML,docx,示例,htmlContent,HTML,using,new,Document From: https://www.cnblogs.com/citycomputing/p/17760582.html