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

时间:2021-07-07 14:02:06

Iam using visual studio 2012,

我使用visual studio 2012,

i would like to open "save dialog" to choose where to save my file instead of using fixed path, the following code is a sample of what i would like to use it in:

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

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

i would like to change this path "C:\output\book1.xlsx" to save dialog, so i can choose where to save it manually.

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

thanks alot..

非常感谢..

4 个解决方案

#1


3  

Like this, don't forget to dispose of com objects with the Marshal class like I added.

像这样,不要忘记像我添加的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

#2


1  

A little more comprehensive way to open the Save As Dialog than OneFineDay's answer (using the same method).
This opens the Save As dialog using a designated directory, filename, extension type, and window title; it also prompts before overwritting any existing files.

比OneFineDay的答案(使用相同的方法)打开另存为对话框更全面的方法。这将使用指定的目录,文件名,扩展名类型和窗口标题打开“另存为”对话框;它还会在覆盖任何现有文件之前提示。

Dim dir as String = "C:\output\"
Dim fName As String = "Book1"

Using sfd As New SaveFileDialog
    sfd.InitialDirectory = dir
    sfd.Title = "Save As"
    sfd.OverwritePrompt = True
    sfd.FileName = fName
    sfd.DefaultExt = ".xlsx"
    sfd.Filter = "Excel Workbook(*.xlsx)|"
    sfd.AddExtension = True
    If sfd.ShowDialog() = DialogResult.OK Then
        xlsWorkBook.SaveAs(sfd.FileName)
    End If
End Using

#3


0  

Add an openfiledialog to your form and then ...

在表单中添加一个openfiledialog然后...

  With OpenFileDialog1
        .Title = " whatever"
        .InitialDirectory = "c:\"
        .Multiselect = False
        If .ShowDialog() = DialogResult.OK Then
            xlsWorkBook.SaveAs(.FileName)
        End If

End With

结束

#4


0  

You can use this :

你可以用这个:

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")
    xls.Application.DisplayAlerts = False
    xlsWorkBook.Close()
    xls.Quit()


End Sub

#1


3  

Like this, don't forget to dispose of com objects with the Marshal class like I added.

像这样,不要忘记像我添加的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

#2


1  

A little more comprehensive way to open the Save As Dialog than OneFineDay's answer (using the same method).
This opens the Save As dialog using a designated directory, filename, extension type, and window title; it also prompts before overwritting any existing files.

比OneFineDay的答案(使用相同的方法)打开另存为对话框更全面的方法。这将使用指定的目录,文件名,扩展名类型和窗口标题打开“另存为”对话框;它还会在覆盖任何现有文件之前提示。

Dim dir as String = "C:\output\"
Dim fName As String = "Book1"

Using sfd As New SaveFileDialog
    sfd.InitialDirectory = dir
    sfd.Title = "Save As"
    sfd.OverwritePrompt = True
    sfd.FileName = fName
    sfd.DefaultExt = ".xlsx"
    sfd.Filter = "Excel Workbook(*.xlsx)|"
    sfd.AddExtension = True
    If sfd.ShowDialog() = DialogResult.OK Then
        xlsWorkBook.SaveAs(sfd.FileName)
    End If
End Using

#3


0  

Add an openfiledialog to your form and then ...

在表单中添加一个openfiledialog然后...

  With OpenFileDialog1
        .Title = " whatever"
        .InitialDirectory = "c:\"
        .Multiselect = False
        If .ShowDialog() = DialogResult.OK Then
            xlsWorkBook.SaveAs(.FileName)
        End If

End With

结束

#4


0  

You can use this :

你可以用这个:

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")
    xls.Application.DisplayAlerts = False
    xlsWorkBook.Close()
    xls.Quit()


End Sub