c# - Visual Studio 2012: How do I open an excel file that is associated with my project? -
so i'm working on project uses excel file add "descriptions user controls. of right now, code opens excel file using absolute path,
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 fine.
i tried renaming file (just make sure c:\ file wasn't being opened odd reason) , added project wouldn't have depend on being able access particular drive (i.e file opened because it's associated project). tried 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' not found. check spelling of file name, , verify file location correct."
i wondering correct way add excel file project , open using relative path name?
if want associate file project need resources. here can see how add resource project:
how create , use resources in .net
resources stream. stored in memory. recommend use class takes stream. not path of file.
because workbooks.open creates stream path of file. better overload or alternative class takes stream directly.
var doc = spreadsheetdocument.open(properties.resources.yourfile , iseditable); iseditable bool. may use true or false. if set false readonly.
i recommend take @ topic to:
how to: open spreadsheet document stream (open xml sdk)
any way if want use current class. have temporary save stream disk , path it. works there lot of work since can use stream in straight way.
string path = path.gettemppath(); // creates temp path temporary file. properties.resources.yourfile.save(path); // save file path. // can use path load file xlapp.workbooks.open again! excel.workbook xlworkbook = xlapp.workbooks.open(path,...);
Comments
Post a Comment