将互操作对象的 Word 转换为字节 [],无需物理保存 [英] Convert Word of interop object to byte [] without saving physically

查看:35
本文介绍了将互操作对象的 Word 转换为字节 [],无需物理保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Microsoft.Office.InteropMicrosoft.Office.Word 在内存中创建了一个对象,并使用所有创建的段落、表格等.我需要这个对象来生成一个内容字节 [] 来提供表中相同类型的一个字段.我无法使用 oDoc.Save(路径")以任何方式物理保存它以便使用 FileStream 并解决我的问题的问题.

I have an object created in memory using Microsoft.Office.Interop and Microsoft.Office.Word and with all created, paragraphs, tables and the like. I need this object to generate a content byte [] to feed one field of the same type in a table. The problem that I can not save it in any way physically with a oDoc.Save ("path") in order to use a FileStream and solve my problem.

尝试了几种解决方案以及如何使用剪贴板,都没有奏效.有什么解决办法吗?

Have tried several solutions and how to use the clipboard, and did not work. Any solution?

推荐答案

你真的需要使用 Microsoft.Office.InteropMicrosoft.Office.Word 吗?

Do you really have to use the Microsoft.Office.Interop and Microsoft.Office.Word?

如果不是真的需要,您可以使用 OpenXML SDK 库 用于操作 WordDocument 的内容.

If it is not really necessary, you could use the OpenXML SDK libraries for manipulating the content of the WordDocument.

OpenXML SDK 包含一个 WordprocessingDocument 类,它可以操作包含 WordDocument 内容的内存流.并且 MemoryStream 可以使用 ToArray() 转换为 byte[].

OpenXML SDK contains a class WordprocessingDocument that can manipulate a memory stream containing a WordDocument content. And MemoryStream can be converted using ToArray() to a 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();

这篇关于将互操作对象的 Word 转换为字节 [],无需物理保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆