简体   繁体   中英

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.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). 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."

I was wondering what is the correct way to add an excel file to a project and open it using a relative path name?

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

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

Resources are Stream. They are stored in memory. So i will recommend you to use a class Which takes Stream. Not path of the File.

Because Workbooks.Open creates Stream from path of the file. 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. You may use true or false . If you set it to false It will be readonly.

I recommend you to take a look at this topic to:

How to: Open a spreadsheet document from a stream (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. It works but there is lot of extra work to do since you can use the Stream in straight way.

 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,...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM