简体   繁体   English

将数据从VB导出到Excel工作表

[英]Export data from VB to Excel sheet

On VS 2012 I have created a VB.NET calculation application (outputs based on variable inputs), i need to save these input & output data to certain cells in excel sheet, here is a sample of the code i use: 在VS 2012上我创建了一个VB.NET计算应用程序(基于变量输入的输出),我需要将这些输入和输出数据保存到excel表中的某些单元格,这里是我使用的代码示例:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim xls As Microsoft.Office.Interop.Excel.Application
        Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        xls = New Microsoft.Office.Interop.Excel.Application
        xlsWorkBook = xls.Workbooks.Open("D:\bookl.xlsx")
        xlsWorkSheet = xlsWorkBook.Sheets("sheet1")

        xlsWorkSheet.Cells(1, 1) = TextBox1.Text

        xlsWorkBook.Close()
        xls.Quit()

End Sub

my problem here is that in every time i click the save button it saves the data to a excel sheet file which i have to specify its path previously. 我的问题在于,每次我单击保存按钮时,它都会将数据保存到excel表文件中,我必须先指定其路径。

What i wish to do is if there is any way to load from VB it self then choose where to save it (because iam going to use it in a lot of machines, so i don't want to put the excel file in the same path each time i use the application in any other machine) 我想做的是,如果有任何方法从VB加载它自己然后选择保存它的位置(因为我将在很多机器中使用它,所以我不想把excel文件放在同一个每次我在任何其他机器上使用该应用程序的路径)

Open Excel file from My.Resources location 从My.Resources位置打开Excel文件

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim xls As New Microsoft.Office.Interop.Excel.Application

    Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\..\..\Resources\")
    Dim fileName = "book1.xlsx"

    xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
    xlsWorkSheet = xlsWorkBook.Sheets("Sheet1")

    xlsWorkSheet.Cells(1, 1) = TextBox1.Text

    xlsWorkBook.Close()
    xls.Quit()

    MsgBox("file saved to " & resourcesFolder)
End Sub

The resource template xlsx file must be copied to the output directory, so edit its properties and choose (actually I'm not super sure you need this...) 必须将资源模板xlsx文件复制到输出目录,因此请编辑其属性并选择(实际上我不确定您是否需要此...)

Build Action = Content
Copy To Output Directory = Copy Always

PS this is just a sample to use with your current code, but I strongly suggest using EPPlus Library if you want to create/save/modify excel files. PS这只是一个与当前代码一起使用的示例,但如果您想创建/保存/修改Excel文件,我强烈建议使用EPPlus库

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim xls As New Microsoft.Office.Interop.Excel.Application

    Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\..\..\Resources\")
    Dim fileName = "book1.xlsx"

    xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
    xlsWorkSheet = xlsWorkBook.Sheets("Sheet1")

    xlsWorkSheet.Cells(1, 1) = TextBox1.Text

    xlsWorkBook.Close()
    xls.Quit()

    MsgBox("file saved to " & resourcesFolder)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM