是否可以將 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”應該會出現大量討論、博客文章等。
- 1 回答
- 0 關注
- 146 瀏覽
添加回答
舉報
0/150
提交
取消