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

时间:2014-01-18 00:11:42

标签: vb.net excel visual-studio

我使用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"保存对话框,我可以选择手动保存它的位置。

非常感谢..

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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

结束

答案 3 :(得分:0)

您可以使用:

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