嵌入式资源word文档怎么打开?
How to open embedded resource word document?
我的项目中有一个嵌入的 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 文档作为嵌入式资源而不是 resx
文件中的 Byte[]
数组嵌入可能是一个想法(出于性能原因),如下所示:
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
我的项目中有一个嵌入的 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 文档作为嵌入式资源而不是 resx
文件中的 Byte[]
数组嵌入可能是一个想法(出于性能原因),如下所示:
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