2011-05-16 46 views
1

我使用Microsoft.Office.InteropMicrosoft.Office.Word,并与所有创建,段落,表格等在内存中创建一个对象。我需要这个对象来生成一个内容字节[]来提供一个表中相同类型的字段。 我无法以任何方式使用oDoc.Save(“路径”)以物理方式将其保存以便使用FileStream并解决我的问题。转换Word互操作对象为byte [],不保存物理

已经尝试了几种解决方案,以及如何使用剪贴板,并没有工作。任何解决方案

+1

你得到什么错误?你能显示一些代码吗? – keyboardP 2011-05-16 20:53:37

+0

我不知道是否保存到命名管道可能工作...但 - 写入TEMP是更容易... – 2011-05-16 21:32:19

+0

马克,我不写TEMP,因为没有任何权限写在FS中。 – Iceknight 2011-05-17 17:59:35

回答

0

您是否真的必须使用Microsoft.Office.InteropMicrosoft.Office.Word

如果没有必要,可以使用OpenXML SDK libraries来操作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();