gpt4 book ai didi

vb.net - 使用 vb.net 打开 Excel 文件的保存对话框的提示

转载 作者:行者123 更新时间:2023-12-02 17:01:16 26 4
gpt4 key购买 nike

我正在使用 Visual Studio 2012,

我想打开“保存对话框”来选择保存文件的位置而不是使用固定路径,以下代码是我想使用它的示例:

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("a")

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

xlsWorkBook.SaveAs("C:\output\book1.xlsx")

xlsWorkBook.Close()
xls.Quit()


End Sub

我想更改此路径“C:\output\book1.xlsx”来保存对话框,以便我可以选择手动保存它的位置。

非常感谢..

最佳答案

像这样,不要忘记像我添加的那样使用 Marshal 类处理 com 对象。

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim xls As New Microsoft.Office.Interop.Excel.Application
Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim resourcesFolder = IO.Path.GetFullPath(Application.StartupPath & "\Resources\")
Dim fileName = "book1.xlsx"
xlsWorkBook = xls.Workbooks.Open(resourcesFolder & fileName)
xlsWorkSheet = xlsWorkBook.Sheets("a")
xlsWorkSheet.Cells(1, 1) = TextBox1.Text
Using sfd As New SaveFileDialog
If sfd.ShowDialog() = DialogResult.OK Then
xlsWorkBook.SaveAs(sfd.FileName)
MessageBox.Show(sfd.Filename)
End If
End Using
xlsWorkBook.Close()
xls.Quit()
Marshal.FinalReleaseComObject(xlsWorkSheet)
Marshal.FinalReleaseComObject(xlsWorkBook)
Marshal.FinalReleaseComObject(xls)
End Sub

关于vb.net - 使用 vb.net 打开 Excel 文件的保存对话框的提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21198296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com