【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()

留言

這個網誌中的熱門文章

【ASP】日期轉換(西元<-->民國)

【VB】使用NPOI元件來匯出Excel--DataTableToExcel

【SQL】符號切割字串變成多欄