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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21020359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:22:41  来源:igfitidea点击:

Export data from VB to Excel sheet

vb.netexcelvisual-studio

提问by user3151946

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 文件放在同一个每次我在任何其他机器上使用该应用程序时的路径)

采纳答案by Vland

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

P.S. this is just a sample to use with your current code, but I strongly suggest using EPPlus Libraryif you want to create/save/modify excel files.

PS 这只是与您当前代码一起使用的示例,但如果您想创建/保存/修改 excel 文件,我强烈建议使用EPPlus 库

回答by nehA

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