0
這是代碼。但Excel文件顯示的是整個aspx頁面而不是gridview。將Gridview導出到Excel,但整個aspx頁面導出代替
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
任何想法如何解決這個問題? 在此先感謝。
您好,感謝回答。我添加了這個,但它仍然導出整個頁面。 – user2621831