为了账号安全,请及时绑定邮箱和手机立即绑定

c# OpenXML WordprocessingDocumentType 在 TableCell

c# OpenXML WordprocessingDocumentType 在 TableCell

米脂 2023-08-21 17:55:27
是否可以将 HTML 片段插入到使用 OpenXML 创建的 WordProcessingDocument 的 TableCell 中?例如,当我使用:public void WriteWordFile(){    var fileName = HttpContext.Current.Request.PhysicalApplicationPath + "/temp/" + HttpContext.Current.Session.SessionID + ".docx";    using (var wordDocument = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document, true))    {        var mainPart = wordDocument.AddMainDocumentPart();        mainPart.Document = new Document();        var body = mainPart.Document.AppendChild(new Body());        var table = new Table();        var tr = new TableRow();        var tc = new TableCell();        tc.Append(new Paragraph(new Run(new Text("1"))));        tr.Append(tc);        table.Append(tr);        body.Append(table);    }Word 文档打印一个简单的“1”,没有预期的格式。然而,我想做的是写:public void WriteWordFile(){    var fileName = HttpContext.Current.Request.PhysicalApplicationPath + "/temp/" + HttpContext.Current.Session.SessionID + ".docx";    using (var wordDocument = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document, true))    {        var mainPart = wordDocument.AddMainDocumentPart();        mainPart.Document = new Document();        var body = mainPart.Document.AppendChild(new Body());        var table = new Table();        var tr = new TableRow();        var tc = new TableCell();        tc.Append(new Paragraph(new Run(new Text("<span style=\"bold\">1</span>"))));        tr.Append(tc);        table.Append(tr);        body.Append(table);    }}Word 文档打印 HTML 标记“ <span style="bold">1</span>”而不是粗体“ 1 ”。
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

不,Word 不支持问题中所描述的内容。

HTML 不是 Word 的本机格式 -需要转换器才能将 HTML 合并到 Word 内容中。

在 UI 中,这是通过从文件粘贴或插入来完成的。

在 Open XML 中,可以使用该altChunk方法将外部格式的内容“嵌入”到文档包中。当 Word 打开文档并遇到一个文件时,altChunk它会调用适当的转换器将其转换为本机 Word 内容 (Word Open XML);在此过程中,原始嵌入内容将被删除。但是,不能保证结果就是本机环境(在浏览器的 HTML 情况下)返回的结果。

搜索“altChunk”应该会出现大量讨论、博客文章等。


查看完整回答
反对 回复 2023-08-21
  • 1 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信