File open or save dialog box in .net windows application

gladstonesheeba
gladstonesheeba used Ask the Experts™
on
Hi ,
  am developing a vb.net windows application to show the data in the datagridview. The user wants to be able to export the data to excel sheet. I have no issues in exporting to an excel file. The only issue  i have is , how to give the user an option to open the excel file or save the excel file to whatever folder they choose. Right now am saving to a C drive by default. But they want to have an option to show the modal dialog box to open or save , like we have it in the web applications (ex :Response. writeline.....)

Do we have this ability in the windows application.

Please help me with this issue. I searched for so many articles , but i couldn't find it and i have no idea how to give them the option.


I would appreciate your help.

thanks
Dim xlApp As New Application
        Dim xlWorkBook As Workbook
        Dim xlWorkSheet As Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        Dim cols As Integer
        cols = DataGridView1.Columns.Count
        Dim iRow As Integer, iCol As Integer
        For iRow = 0 To DataGridView1.Rows.Count - 1
            For iCol = 0 To DataGridView1.Columns.Count - 1
                xlWorkSheet.Cells(iRow + 1, iCol + 1).Value = DataGridView1.Columns(iCol).Name.ToString().ToUpper()
            Next
        Next
        For iRow = 0 To DataGridView1.Rows.Count - 1
            For iCol = 0 To DataGridView1.Columns.Count - 1
                xlWorkSheet.Cells(iRow + 2, iCol + 1).Value = DataGridView1.Rows(iRow).Cells(iCol).Value
            Next
        Next
       
        xlWorkSheet.SaveAs("C:\Test.xls")
        xlWorkBook.Close()
        xlApp.Quit()
        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi, Savefiledialog is pretty easy to work with. try this.
Dim xlApp As New Application
        Dim xlWorkBook As Workbook
        Dim xlWorkSheet As Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        Dim cols As Integer
        cols = DataGridView1.Columns.Count
        Dim iRow As Integer, iCol As Integer
        For iRow = 0 To DataGridView1.Rows.Count - 1
            For iCol = 0 To DataGridView1.Columns.Count - 1
                xlWorkSheet.Cells(iRow + 1, iCol + 1).Value = DataGridView1.Columns(iCol).Name.ToString().ToUpper()
            Next
        Next
        For iRow = 0 To DataGridView1.Rows.Count - 1
            For iCol = 0 To DataGridView1.Columns.Count - 1
                xlWorkSheet.Cells(iRow + 2, iCol + 1).Value = DataGridView1.Rows(iRow).Cells(iCol).Value
            Next
        Next
        Dim Save As New SaveFileDialog
        If Save.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
        Else
            xlWorkSheet.SaveAs(Save.FileName)
            xlWorkBook.Close()
        End If


        xlApp.Quit()
        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

Open in new window

Author

Commented:
Thanks for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial