Visual Studio 2012:如何打开与项目关联的Excel文件?

时间:2023-01-17 10:55:27

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 (i.e the file could be opened because it's associated with the project). Then I tried the following code:

然后我尝试重命名文件(只是为了确保C:\文件没有因为某些奇怪的原因被打开)并将其添加到我的项目中,这样我就不必依赖于能够访问特定的驱动器了(即文件可以打开,因为它与项目相关联)。然后我尝试了以下代码:

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文件添加到项目并使用相对路径名打开它的正确方法是什么?

2 个解决方案

#1


0  

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. Not path of the File.

资源是Stream。它们存储在内存中。所以我建议你使用一个接受Stream的课程。不是文件的路径。

Because Workbooks.Open creates Stream from path of the file. Its better to look for an overload or alternative class that takes stream directly.

因为Workbooks.Open从文件的路径创建Stream。最好是寻找直接接收流的重载或替代类。

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.

isEditable是bool。您可以使用true或false。如果将其设置为false它将是readonly。

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. It works but there is lot of extra work to do since you can use the Stream in straight way.

任何方式如果你想使用你当前的类。您必须临时将Stream保存到磁盘并从中获取路径。它可以工作但是还有很多额外的工作要做,因为你可以直接使用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,...);

#2


0  

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”即可

#1


0  

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. Not path of the File.

资源是Stream。它们存储在内存中。所以我建议你使用一个接受Stream的课程。不是文件的路径。

Because Workbooks.Open creates Stream from path of the file. Its better to look for an overload or alternative class that takes stream directly.

因为Workbooks.Open从文件的路径创建Stream。最好是寻找直接接收流的重载或替代类。

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.

isEditable是bool。您可以使用true或false。如果将其设置为false它将是readonly。

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. It works but there is lot of extra work to do since you can use the Stream in straight way.

任何方式如果你想使用你当前的类。您必须临时将Stream保存到磁盘并从中获取路径。它可以工作但是还有很多额外的工作要做,因为你可以直接使用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,...);

#2


0  

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”即可