Protected Sub bt_excel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bt_excel.Click
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=Export.xls")
Response.ContentType = "application/vnd.ms-excel"
Dim sw As New System.IO.StringWriter()
Dim htw As New HtmlTextWriter(sw)
GridView1.HeaderStyle.BackColor = Color.White
GridView1.HeaderStyle.ForeColor = Color.Black
GridView1.AlternatingRowStyle.BackColor = Color.White
GridView1.RowStyle.BackColor = Color.White
'關閉換頁跟排序
'GridView1.AllowSorting = false;
'GridView1.AllowPaging = false;
'//移去不要的欄位
'GridView1.Columns.RemoveAt(GridView1.Columns.Count - 1);
'GridView1.DataBind();
'//建立假HtmlForm避免以下錯誤
'//Control 'GridView1' of type 'GridView' must be placed inside
'//a form tag with runat=server.
'//另一種做法是override VerifyRenderingInServerForm後不做任何事
'//這樣就可以直接GridView1.RenderControl(htw);
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>")
GridView1.RenderControl(htw)
'Dim hf As New HtmlForm()
'Controls.Add(hf)
'hf.Controls.Add(GridView1)
'hf.RenderControl(htw)
Response.Write(sw.ToString())
Response.End()
End Sub
6月 13, 2010
GridView 輸出成Excel
標籤:
Asp.Net
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言