【问题标题】:Opening a dynamically created Excel workbook打开动态创建的 Excel 工作簿
【发布时间】:2018-03-28 02:14:09
【问题描述】:

我有一个 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() 为我做这件事。

有没有更好的方法来做到这一点?

【问题讨论】:

  • 哪个部分不起作用?
  • @rlb.usa - 上面的代码有效,只是无法打开文件。我需要能够在保存后自动打开文件,或者完全跳过SaveFileDialog()并打开它而不保存。
  • 这个问题对你有帮助吗? stackoverflow.com/questions/18246053/… 我知道您希望浏览器打开本地文件,但我非常确定这是不可能的(想想恶意应用程序吧!)而且您能做的最好的事情就是在屏幕说“单击此处”,然后打开 file:///
  • @rlb.usa - 这是一个 Windows 窗体应用程序,而不是网站,所以这个问题没有多大帮助。
  • 抱歉! 12 怎么样

标签: excel vb.net winforms savefiledialog


【解决方案1】:

这个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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多