打开动态创建的Excel工作簿

时间:2018-03-27 16:02:44

标签: excel vb.net winforms savefiledialog

我有一个Windows窗体应用程序,其中包含我使用SpreadsheetGear创建的Excel文件。我的应用程序使用SaveFileDialog()提示用户使用以下代码将文件保存到他们的计算机:

'Bring up the save dialog to save the newly created Excel file
Using saveFileDialog1 As New SaveFileDialog()
    saveFileDialog1.FileName = "ExportFile.xlsx"
    If DialogResult.OK <> saveFileDialog1.ShowDialog() Then
        Return
    End If
    Try
        e.Result.SaveAs(saveFileDialog1.FileName, FileFormat.OpenXMLWorkbook)
    Catch ex As Exception
        MessageBox.Show("Error: " & ex.Message, "Save Error", MessageBoxButtons.OK)
    End Try
End Using

该代码工作正常,但用户并不担心保存文件。他只是希望它立即打开,这样他就可以复制/粘贴他需要的信息,然后丢弃文件而无需导航到他的保存目的地。我似乎无法让SaveFileDialog()为我做这件事。

有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

closed question让我发现了答案。来自Process.Start的文件名为SaveFileDialog

更新代码:

'Bring up the save dialog to save the newly created Excel file
Using saveFileDialog1 As New SaveFileDialog()
    saveFileDialog1.FileName = "ExportFile.xlsx"
    If DialogResult.OK <> saveFileDialog1.ShowDialog() Then
        Return
    End If
    Try
        e.Result.SaveAs(saveFileDialog1.FileName, FileFormat.OpenXMLWorkbook)

        If File.Exists(saveFileDialog1.FileName) Then
                Process.Start(saveFileDialog1.FileName) 'Open the newly created Excel file
        Else
            Throw New Exception("Could not find file to open. Please try again.")
        End If
    Catch ex As Exception
        MessageBox.Show("Error: " & ex.Message, "Save Error", MessageBoxButtons.OK)
    End Try
End Using