将Gridview导出到Excel时,属性评估失败错误

时间:2012-06-29 05:48:03

标签: vb.net gridview export-to-excel

我正在尝试将gridview数据导出到Excel。

但我在Property Evaluation Failed

收到错误Response.End()

这是我的代码 - 任何想法?

Private Sub ExportGridToExcel()
Try
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=NPS.xls")
Response.ContentType = "application/vnd.ms-excel"
' Remove the charset from the Content-Type header.
Response.Charset = ""

' Turn off the view state.
Me.EnableViewState = False

Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)

' Get the HTML for the control.
gvNPSResults.RenderControl(hw)
' Write the HTML back to the browser.
Response.Write(tw.ToString())
' End the response.

HttpContext.Current.ApplicationInstance.CompleteRequest()
Response.End()
Catch ex As Exception
'Throw ex
End Try
End Sub

1 个答案:

答案 0 :(得分:0)

再次尝试替换您的代码

HttpContext.Current.ApplicationInstance.CompleteRequest();

Response.End();

通过

 Response.Flush();
 Response.Close();