[英]Visual Studio 2012: How do I open an excel file that is associated with my project?
So I'm working on a project that uses an excel file to add "descriptions to user controls. As of right now, my code opens the excel file using an absolute path, like so 因此,我正在开发一个项目,该项目使用excel文件向用户控件添加“说明”。截至目前,我的代码使用绝对路径打开excel文件,如下所示
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\absolute path\filename.xlsx", Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing);
which works just fine. 效果很好。
I then tried renaming the file (just to make sure that the C:\\ file wasn't being opened for some odd reason) and added it to my project so that I wouldn't have to depend on being able to access a particular drive (ie the file could be opened because it's associated with the project). 然后,我尝试重命名该文件(只是为了确保未出于某些奇怪的原因打开C:\\文件)并将其添加到我的项目中,这样我就不必依赖能够访问特定驱动器了(即文件可以打开,因为它与项目相关联)。 Then I tried the following code: 然后,我尝试了以下代码:
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"newfileName.xlsx", Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing, Type.Missing, Type.Missing
, Type.Missing, Type.Missing);
which gave me this: 这给了我这个:
"Additional information: 'newfileName.xlsx' could not be found. Check the spelling of the file name, and verify that the file location is correct." “其他信息:找不到'newfileName.xlsx'。请检查文件名的拼写,并验证文件位置是否正确。”
I was wondering what is the correct way to add an excel file to a project and open it using a relative path name? 我想知道将excel文件添加到项目并使用相对路径名打开它的正确方法是什么?
The second example is trying to open this file at the work directory. 第二个示例尝试在工作目录中打开此文件。 So if you want to copy this file on build, just change "Copy to Output Directory" property to "true" for this file 因此,如果要在构建时复制此文件,只需将此文件的“复制到输出目录”属性更改为“ true”
If you want to associate file with your Project then you need Resources. 如果要将文件与项目关联,则需要资源。 Here you can see how to add resource to your project: 在这里,您可以了解如何向项目添加资源:
How to create and use resources in .NET 如何在.NET中创建和使用资源
Resources are Stream. 资源是流。 They are stored in memory. 它们存储在内存中。 So i will recommend you to use a class Which takes Stream. 因此,我建议您使用需要Stream的类。 Not path of the File. 不是文件的路径。
Because Workbooks.Open
creates Stream from path of the file. 因为Workbooks.Open
从文件的路径创建Stream。 Its better to look for an overload or alternative class that takes stream directly. 寻找直接使用流的重载或替代类更好。
var doc = SpreadSheetDocument.Open(Properties.Resources.YourFile , isEditable);
isEditable
is bool. isEditable
是布尔值。 You may use true
or false
. 您可以使用true
或false
。 If you set it to false It will be readonly. 如果将其设置为false,它将是只读的。
I recommend you to take a look at this topic to: 我建议您看一下该主题以:
How to: Open a spreadsheet document from a stream (Open XML SDK) 如何:从流中打开电子表格文档(Open XML SDK)
Any way If you want to use your current class. 任何方式如果您想使用当前类。 You have to temporary save Stream into Disk and get the path from it. 您必须将Stream临时保存到Disk并从中获取路径。 It works but there is lot of extra work to do since you can use the Stream in straight way. 它可以工作,但是还有很多工作要做,因为您可以直接使用Stream。
string path = Path.GetTempPath(); // Creates a Temp path for temporary file.
Properties.Resources.Yourfile.Save(path); // Save the file into path.
// Now you can use path to load file with xlApp.Workbooks.Open again!
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(path,...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.