将Excel工作簿添加到VB.net应用程序 [英] Add excel workbook to VB.net application
问题描述
我创建了一个仅显示1个表单和按钮的项目。我想做的是一旦单击按钮,它会打开一个预建的excel工作簿。问题是,我需要在应用程序捆绑包中添加excel文件。例如,我将应用程序光盘交给其他人加载到另一台计算机上,然后他们可以运行该应用程序以打开工作簿。我知道如何在我的个人计算机上引用文件,而不是如何在项目内或多或少地独立添加文件。该应用程序是出于库存目的,但需要在多台计算机上打开...这有点像使用VB作为excel工作簿的前端。我对Vb.net知之甚少,但愿意学习。请求逐步演练。
I have a project that I created that only shows 1 form and button. What I am trying to do is once the button is clicked, it opens a pre-built excel workbook. The problem is, I need to add the excel file within the application bundle. For example, I give the application disc to someone else to load on another computer, and then they can run the application to open the workbook. I know how to reference the file on my personal computer, just not how to add it "within" the project, to be, more or less, stand-alone. The application if for inventory purposes but it needs to be opened on numerous computers...this is kinda like using VB as a front end to excel workbook. I know very little about Vb.net but willing to learn. Requesting a walkthrough of steps. Please help!!
推荐答案
- 请在解决方案资源管理器中右键单击该项目,然后选择添加>现有项...
- 浏览到工作簿,然后单击添加按钮。
- 该文件现在应该出现在解决方案资源管理器中项目下方的文件列表中。选择文件。
- 在属性窗口中,将复制到输出目录属性更改为始终复制。 b $ b
- Right-click on the project in the Solution Explorer and select Add>Existing Item...
- Browse to the workbook, and click the Add button.
- The file should now appear in the list of files below the project in the Solution Explorer. Select the file.
- In the Properties window, change the Copy to Output Directory property to Copy always.
这应将工作簿作为可执行文件放在某个文件夹中。这还将复制工作簿并将其添加到您的项目文件夹中。如果您不希望发生这种情况,请使用添加按钮右侧的下拉菜单(请参见上面的第2步),然后选择添加为链接。然后构建将使用在当前位置找到的文件。
This should put the workbook in the some folder as the executable. This will also make a copy of the workbook and add it to you project folder. If you don't want that to happen, the use the drop-down on the right-hand side of the Add button (see Step 2 above), and select Add as Link. Then builds will use the file as it's found in its current location.
要以编程方式打开工作簿,请执行以下操作:
To open the workbook programmatically, do:
Dim filename as String = My.Application.Info.DirectoryPath & System.IO.Path.DirectorySeparatorChar & "WorkbookName.xlsx"
Process.Start(filename)
此示例假定文件名为 WorkbookName.xlsx ,则需要对其进行更改以匹配文件名。
This example assumes that the file is named, WorkbookName.xlsx, you'll need to change it to match the name of your file.
这篇关于将Excel工作簿添加到VB.net应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!