打开Excel工作簿文件使用嵌入的资源 [英] Open Excel Workbook file Using Embedded Resource
问题描述
我使用Microsoft Office互操作来打开Excel文件。该文件被放置在一个特殊的文件夹,从那里工作簿码读取文件。现在,要求是,Excel文件可以放置在任何地方。我认为最好的办法是到Excel文件附加为嵌入的资源。但是,如果我附上嵌入式资源我将如何使用Excel工作簿读取该文件。
I am using Microsoft Office Interop to open Excel files. The file is placed in a special folder from where it the Workbook code reads the file. Now, the requirement is that the Excel file can be placed anywhere. I believe the best approach is to attach the Excel file as an embedded resource. But if I attach as embedded resource how will I read the file using Excel workbook.
_excelapplication.Workbooks.Open(Filename: pExcelTemplatePath);
什么是读取Excel模板文件,而不管该文件的位置的最佳方式?
What is the best way to read the excel template file regardless of the location of the file?
推荐答案
您不能直接从嵌入的资源打开它 - 至少不能与互操作...
You can't open it directly from the embedded resources - at least not with Interop...
您将需要在一些地方保存为一个文件 - 即首先由 Assembly.GetExecutingAssembly()GetManifestResourceStream(resourceNameOfEmbeddedExcelFile)从资源(例如阅读code>),然后写流如
的ApplicationData
/ CommonApplicationData
/ LocalApplicationData
/ 我的文档
/ CommonDocuments
从 Environment.SpecialFolder
You will need to save it somewhere as a file - i.e. first read it from the resource (for example by Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceNameOfEmbeddedExcelFile)
) and then write that stream to some location like ApplicationData
/ CommonApplicationData
/ LocalApplicationData
/ MyDocuments
/ CommonDocuments
from Environment.SpecialFolder
另一种选择是使用能够打开库/从流编辑Excel文件 - 让我知道,如果你需要一些链接库...
Another option would be to use a library capable of opening/editing Excel files from a stream - let me know if you need some links to libraries...
这篇关于打开Excel工作簿文件使用嵌入的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!