【问题标题】:How to show window prompt for downloading excel file?如何显示下载excel文件的窗口提示?
【发布时间】:2014-09-04 05:56:30
【问题描述】:

我已经编写了将数据导出到 xlsx 文件的代码。但我不明白如何在客户端显示下载该 xlsx 文件的窗口提示。 这是我的代码:

Private Sub DataTableToExcel(ByVal tbl As DataTable)
    Dim Excel As Object = CreateObject("Excel.Application")
    Dim strFilename As String
    Dim intCol, intRow As Integer
    Dim strPath As String = "C:\"


    If Excel Is Nothing Then
        MsgBox("It appears that Excel is not installed on this machine. This operation requires MS Excel to be installed on this machine.", MsgBoxStyle.Critical)
        Return
    End If
    Try
        With Excel
            .SheetsInNewWorkbook = 1
            .Workbooks.Add()
            .Worksheets(1).Select()

            .cells(1, 1).value = "Complaint Detail Report" 'Heading of the excel file
            .cells(1, 1).EntireRow.Font.Bold = True


            Dim intI As Integer = 1
            For intCol = 0 To tbl.Columns.Count - 1
                .cells(2, intI).value = tbl.Columns(intCol).ColumnName
                .cells(2, intI).EntireRow.Font.Bold = True
                intI += 1
            Next
            intI = 3
            Dim intK As Integer = 1
            For intCol = 0 To tbl.Columns.Count - 1
                intI = 3
                For intRow = 0 To tbl.Rows.Count - 1
                    .Cells(intI, intK).Value = tbl.Rows(intRow).ItemArray(intCol)
                    intI += 1
                Next
                intK += 1
            Next
            If Mid$(strPath, strPath.Length, 1) <> "\" Then
                strPath = strPath & "\"
            End If
            strFilename = strPath & "ComplaintDetail.xlsx"
            .ActiveCell.Worksheet.SaveAs(strFilename)
        End With
        System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
        Excel = Nothing
        MsgBox("Data's are exported to Excel Succesfully: Location: '" & strFilename & "'", MsgBoxStyle.Information)
        ' Response.AddHeader("content-disposition", "attachment;filename=ComplaintDetail.xlsx")
        'Response.ContentType = "application/vnd.excel"
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
    For Each i As Process In pro
        i.Kill()
    Next

End Sub

在这里,我将 .XLSX 文件直接保存到“C 盘”。 为什么我选择C盘? : 因为 99% 的人都有 C: 在电脑里。 但是我遇到了一些情况,用户不允许访问他们的 C 驱动器,或者他们不允许在 C 驱动器中写入任何内容。 这就是为什么我试图添加这个窗口提示,用户将决定在哪里保存该文件。但是我在上面的代码中遇到了一些问题。 你能帮我在上面的代码中添加窗口提示吗?

【问题讨论】:

  • 你为什么放一个C#标签?
  • 它是 Windows 应用程序吗?如果是,那么你不应该用 asp.net 标记它。
  • 否 @Priyank.. 它不是 Windows 应用程序...它是一个 Web 应用程序
  • 如果您正确释放这些对象,则无需遍历查找 excel 和调用 Kill() 的进程,您永远不必这样做...
  • 感谢您的回复@MrCoDeXeR.. 我已经根据您的建议更新了我的代码...但是您能告诉我如何显示窗口提示或任何用户可以通过它保存文件的任何内容想要..

标签: asp.net vb.net excel


【解决方案1】:
  1. 保存在 App_Data 目录中。你可以找到绝对路径Server.MapPath("~/App_Data")这个路径是应用程序可写的
  2. 使用Response.TransmitFile 使文件被下载。

【讨论】:

    【解决方案2】:

    尝试使用保存文件对话框之类的东西(可以通过 ui 设计器添加)。 然后使用:

    If dialog.Show() = Windows.Forms.DialogResult.OK Then
         strPath = dialog.FileName
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多