Attach an Excel file to a completed Visual Basic project

I created a Visual Basic project with many forms. In a project, I write information to an excel file, where I refer to the location on my local drive (C :), where the excel file is located. By reference, I mean, I open an excel instance that provides the source = local path on my hard drive. My question is how to attach the excel file to the project, so I do not need to reference the local location and run the program on another computer. In other words, how can I link an excel file to a program? And how can I change the call to the excel file as soon as I β€œlinked” it to the program?

+4
source share
3 answers

Add the excel file to the project. Go to the solution explorer and right-click the excel file and select properties. Change Copy to output directory to copy always. Now the file will be in the same directory as your exe. You can use Reflection.Assembly.GetExecutingAssemblyto get the catalog.

To add a file to the project:

Right click on the project> Add> Existing Item> Your Excel File.xls

To include a file in an assembly:

Right-click the file> Properties> Copy to Output Directory. Set this value to either Always Copy or Copy if it is newer.

Here is the code to get the path to the excel file:

Dim exeDir As New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName)
Dim xlPath = IO.Path.Combine(exeDir.DirectoryName, "Your Excel File.xls")

xlPath should now be the full path to the excel file.

+3
source

1) ( say MyExcelFile), excel.

Create setting

2) My.Settings.MyExcelFile excel.

3) excel , reset , .

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Not IO.File.Exists(My.Settings.MyExcelFile) Then
        Dim ofd As New OpenFileDialog
        ofd.Filter = "Excel Files (*.xls, *.xlsx)|*.xls; *.xlsx"
        ofd.Title = "Specify Excel File Location"
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            My.Settings.MyExcelFile = ofd.FileName
            My.Settings.Save()
        End If
    End If
End Sub
0

, iam , , excel , excel (, , , ),

  • excel
  • (*.* filter) excel
  • excel file: resource :
  • , .
  • use ( using system.reflectionand using system.ioand using system.resources)

    then write code like this

    string sPath = Path.GetTempFileName();
    File.WriteAllBytes(sPath, Properties.Resources.excel File name here);
    

then take this ( spath) and use it as your path so that you can then change the project location using the excel file inside it and it will still work without writing a permanent path for it

0
source

All Articles