Search code examples
c#excelinfragisticsultrawingrid

Exporting grid data to excel. Open excel file before before saving


I am exporting gridview data to excel using infragistics excel exporter. Everything is working fine and data is exported and file is saved on local disk.

But i want to view/open the file before or after export happens. How can I do that. So that I can save my file to a proper location whereever I need to save as

My code is as below

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.UltraGridExcelExporter1.Export(Me.grdiView1, "C:\GridData.xls")
End Sub

Solution

  • Before running the Export method open a SaveFileDialog and ask your users where they want to save the incoming file. Then use the user selection replacing your hardcoded filename

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using sfd = New SaveFileDialog()
            sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            sfd.AddExtension = True
            sfd.Filter = "Excel file (*.xls, *.xlsx)|*.xls;*.xlsx"
            If DialogResult.OK = sfd.ShowDialog() Then
                Me.UltraGridExcelExporter1.Export(Me.grdiView1, sfd.Filename)
                if DialogResult.Yes = MessageBox.Show("Do you want to open the file", "Excel", MessageBoxButtons.YesNo Then
                    System.Diagnostics.Process.Start(sfd.FileName)
                End If 
            End If
        End Using 
    End Sub