Add excel workbook to VB.net application

2019-09-20 16:53发布

问题:

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!!

回答1:

  1. Right-click on the project in the Solution Explorer and select Add>Existing Item...
  2. Browse to the workbook, and click the Add button.
  3. The file should now appear in the list of files below the project in the Solution Explorer. Select the file.
  4. In the Properties window, change the Copy to Output Directory property to Copy always.

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)

This example assumes that the file is named, WorkbookName.xlsx, you'll need to change it to match the name of your file.