打开动态创建的 Excel 工作簿
Opening a dynamically created Excel workbook
我有一个 Windows 表单应用程序,其中包含一个 Excel 使用 SpreadsheetGear 创建的文件。我的应用程序使用 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
该代码工作正常,但用户不想担心保存文件的问题。他只想立即打开它,这样他就可以 copy/paste 他需要的信息,然后丢弃该文件,而不必导航到他的保存目的地。我似乎无法 SaveFileDialog()
为我做那件事。
有更好的方法吗?
这 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
我有一个 Windows 表单应用程序,其中包含一个 Excel 使用 SpreadsheetGear 创建的文件。我的应用程序使用 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
该代码工作正常,但用户不想担心保存文件的问题。他只想立即打开它,这样他就可以 copy/paste 他需要的信息,然后丢弃该文件,而不必导航到他的保存目的地。我似乎无法 SaveFileDialog()
为我做那件事。
有更好的方法吗?
这 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