这是代码。但是 Excel 文件显示的是整个 aspx 页面而不是 gridview。
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
Try
Response.Clear()
Response.AddHeader("content-disposition", ("attachment;filename=HotelList_" & DateTime.Now.Year.ToString().Trim()) + DateTime.Now.Month.ToString().Trim().PadLeft(2, Convert.ToChar("0")) + DateTime.Now.Day.ToString().Trim().PadLeft(2, Convert.ToChar("0")) & ".xls")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.ms-excel"
Dim stringWrite As New System.IO.StringWriter
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
Dim selctedCountry As String = ddlCountry.SelectedItem.Text.ToString()
Dim selCity As String = ddlCity.SelectedItem.Text.ToString()
htmlWrite.Write("<div style='PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px'><h3>HOTEL LIST</h3>")
htmlWrite.Write("<table><tr><td colspan='2'> Country : " & selctedCountry & " </td></tr>")
htmlWrite.Write("<table><tr><td colspan='2'> City : " & selCity & " </td></tr>")
gvHotelMarkup.RenderControl(htmlWrite)
htmlWrite.Write("</div>")
Response.Write(stringWrite.ToString())
Response.End()
Catch ex As Exception
lblErrMessage.Message = ex.Message()
End Try
End Sub
知道如何解决这个问题吗?提前致谢。