我的项目中有一个嵌入的 Word 模板文档,我将其添加为资源(Resources.resx -> 添加资源 -> 添加现有文件),现在我想打开它,如下所示
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 设置为它。
Word 只能打开文件系统中存在的文件,它不能完全从内存中工作。
做这样的事情:
String fileName = Path.GetTempFileName();
File.WriteAllBytes( fileName , Properties.Resources.MyDoc );
application.Documents.Open( fileName );
然后,当您检测到 Word 已关闭时,删除该文件:
File.Delete( fileName );
将 Word 文档嵌入为嵌入式资源而不是Byte[]
数组内的resx
文件,像这样:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)