如何打开嵌入式资源字文件? [英] How to open embedded resource word document?

查看:81
本文介绍了如何打开嵌入式资源字文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个嵌入式Word模板文档,我将其添加为资源(Resources.resx->添加资源->添加现有文件),现在我想以类似方式打开它

I have a embedded word template document in my project, I added it as resource (Resources.resx -> Add resource -> Add existing file) and now I want to open it something like this

Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Document document = application.Documents.Open(Properties.Resources.MyDoc);

但是不幸的是,Microsoft.Office.Interop.Word.Application无法与字节数组一起使用,并且我无法将MyDoc设置为它.

But unfortunately the Microsoft.Office.Interop.Word.Application doesn't work with byte arrays and I can't set MyDoc to it.

推荐答案

Word只能打开文件系统中存在的文件,它不能完全从内存运行.

Word can only open files that exist in the filesystem, it cannot work entirely from-memory.

执行以下操作:

String fileName = Path.GetTempFileName();
File.WriteAllBytes( fileName , Properties.Resources.MyDoc );
application.Documents.Open( fileName  );

然后,当您检测到Word已关闭时,请删除文件:

Then when you've detected Word has been closed, delete the file:

File.Delete( fileName );

出于性能原因,将Word文档作为嵌入式资源而不是resx文件内的Byte[]数组嵌入是一个主意,如下所示:

It might be an idea (for performance reasons) to embed the Word document as an Embedded Resource instead of a Byte[] array within a resx file, like so:

Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream resourceStream = thisExe.GetManifestResourceStream("MyDoc.docx");
// copy the stream to a new FileStream, then open Word as-before

这篇关于如何打开嵌入式资源字文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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