【VB】GridView內容輸出到Excel
條件: GridView繫結於SqlDataSource1
'==========匯出excel檔===============
'excel檔名
Dim file_name As String = Format(Now, "yyyyMMdd") & "採購單.xls"
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode(file_name))
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Dim sw As New System.IO.StringWriter
Dim htw As New System.Web.UI.HtmlTextWriter(sw)
Dim dg As New DataGrid
dg.DataSource = Me.SqlDataSource1.Select(DataSourceSelectArguments.Empty)
dg.DataBind()
dg.RenderControl(htw)
Response.Write(sw.ToString())
Response.End()
留言
張貼留言