简体   繁体   中英

prompt for opening a save dialog for excel file using vb.net

Iam using 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.

thanks alot..

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

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

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.

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

Add an openfiledialog to your form and then ...

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

End With

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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