将interop对象的Word转换为byte 而不保存物理
我在内存中使用Microsoft.Office.Interop和Microsoft.Office.Word以及所有创建的,段落,表格等创建了一个对象。 我需要这个对象生成一个内容byte []来为表中的一个相同类型的字段提供信息。 我无法以任何方式使用oDoc.Save(“path”)以任何方式保存它以便使用FileStream并解决我的问题。
尝试了几种解决方案以及如何使用剪贴板,并没有奏效。 有解决方案吗
你真的必须使用Microsoft.Office.Interop
和Microsoft.Office.Word
吗?
如果没有必要,可以使用OpenXML SDK库来操作WordDocument的内容。
OpenXML SDK包含一个类WordprocessingDocument
,可以操作包含WordDocument内容的内存流。 MemoryStream
可以使用ToArray()
转换为byte[]
。
作为代码示例:
byte[] templateContent = File.ReadAllBytes(templateFile); MemoryStream stream = new MemoryStream(); stream.Write(templateContent, 0, templateContent.Length); WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true); // When done byte[] contentOfWordFile = stream.toArray();