GridView を Excel にエクスポートしたいと思いますが、これは簡単です。しかし、グリッドの上では、Excel で、識別のために他の情報が必要です。下のグリッドビューに入れながら、グリッドビュー以外のものをエクスポートできますか?
編集: 何らかの理由で GridView1 が表示されているときにエクスポートしようとすると、グリッドビューだけでなくページ全体がエクスポートされます。理由がわからない!
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