Analytics

2010年5月28日 星期五

[ASP.NET]DataGrid轉成Excel檔(DataGrid converted to Excel files)


問題

DataGrid轉成Excel檔



解決方法

'Create DataSet ds first
myDataGrid.DataSource = ds.Tables("mydataset")
myDataGrid.DataBind()
Response.Clear()
' Set the content type to Excel(設定資料格式內容為EXCEL)
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment;filename=my.xls")
EnableViewState = False
Dim myTextWriter As New System.IO.StringWriter
Dim myHtmlTextWriter As New HtmlTextWriter(myTextWriter)
myHtmlTextWriter.SingleQuoteChar.ToString()
Response.Buffer = True
Response.Charset = "utf-8"
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8")
myDataGrid.RenderControl(myHtmlTextWriter)
'Write the HTML to the browser
Response.Write(myTextWriter.ToString())
Response.End()

沒有留言:

熱門文章