我正在尝试允许用户使用Workbook / Worksheet对象下载在VB.net中创建的excel文件。 excel文件的创建是complet,但接下来是允许用户在单击按钮时下载它。我编写了我认为使用Response所需的方法然而当我点击按钮时,下载事件(在Chrome / IE上测试)没有发生。就像我从未按下按钮,但它在调试过程中贯穿代码。
Protected Sub btnMatrixSummary_Click(sender As Object, e As System.EventArgs) Handles btnMatrixSummary.Click
Dim refNum As Integer = employee_LB.SelectedValue
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlWorkSheet.Cells(1, 2) = "1. Displays High Integrity & Honesty"
'----------FILL REST OF EXCEL FILE HERE---------------------
'Prepare download file
Dim folder As String = Path.GetTempPath
xlWorkBook.SaveAs(folder & "Matrix.xlsx")
Dim filePath As System.IO.FileInfo = New System.IO.FileInfo(xlWorkBook.FullName)
xlWorkBook.Close(True, misValue, misValue)
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
Response.ClearHeaders()
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=Matrix6.xls")
Response.AddHeader("Content-Length", filePath.Length.ToString())
Response.ContentType = "application/vnd.xls"
Response.WriteFile(filePath.FullName)
HttpContext.Current.Response.Flush() 'Sends all currently buffered output To the client.
HttpContext.Current.Response.SuppressContent = True 'Gets Or sets a value indicating whether To send HTTP content To the client.
HttpContext.Current.ApplicationInstance.CompleteRequest()
'Delete local matrix off server
My.Computer.FileSystem.DeleteFile(filePath.FullName)