将网格数据导出为ex​​cel。保存之前打开excel文件

时间:2016-05-11 15:02:02

标签: c# excel infragistics ultrawingrid

我使用infragistics excel导出器将gridview数据导出到excel。一切正常,数据导出,文件保存在本地磁盘上。

但是我想在导出发生之前或之后查看/打开文件。我怎样才能做到这一点。因此,我可以将文件保存到适当的位置,无论我需要保存为

我的代码如下

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

1 个答案:

答案 0 :(得分:4)

在运行Export方法之前,打开SaveFileDialog并询问用户他们想要保存传入文件的位置。然后使用用户选择替换硬编码文件名

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