我想将 GridView 导出到 excel,这很容易。但在网格上方,在 Excel 中,我想要一些其他信息用于识别。我可以以某种方式导出除网格视图以外的东西,然后放入下面的网格视图吗?
编辑: 由于某种原因,当 GridView1 可见并且我尝试导出时,整个页面都会导出,而不仅仅是 gridview。不知道为什么!
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
'Create a StringWriter and HtmlTextWriter
Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
Dim htw As New System.Web.UI.HtmlTextWriter(sw)
'Clear the Response object's content and specify the header for the HTML response and type of application file to create
Response.ClearContent()
Response.AddHeader("content-disposition", "attachment; filename=SaveFile.xls")
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
EnableViewState = False
htw.WriteLine("Test, test, test")
Try
'Check for the number of GridView rows
If GridView1.Rows.Count < 65535 Then
'Turn sorting and paging off and rebind the GridView control
GridView1.AllowSorting = False
GridView1.AllowPaging = False
GridView1.PageSize = GridView1.Rows.Count
GridView1.AutoGenerateSelectButton() = False
GridView1.DataBind()
'Render the GridView1 as HTML - this will cause an error that will fire the VerifyRenderingInServerForm event -- this event is trapped by the Overriding sub procedure given at the end of the program listing
GridView1.RenderControl(htw)
'Write the response
Response.Write(sw.ToString())
Response.End()
'Turn sorting and paging on and rebind the GridView control
GridView1.AllowSorting = True
GridView1.AllowPaging = True
'.GridView1.PageSize = 10
GridView1.AutoGenerateSelectButton() = True
GridView1.DataBind()
End If
Catch ex As Exception
End Try
End Sub