...
Response.Clear()
Response.Buffer = True
Response.Charset = "UTF-8"
Response.AddHeader("Content-Disposition", "attachment;filename=test.xls")
Response.ContentEncoding = System.Text.Encoding.Default'這裏選System.Text.Encoding.Default,不然漢字會亂碼
Response.ContentType = "application/ms-excel"
Dim writer As New System.IO.StringWriter
Dim htmlwriter As New System.Web.UI.HtmlTextWriter(writer)
Response.Write(convertdttotdf(dt))
Response.Output.Write(writer)
Response.Flush()
Response.End()
convertdttotdf(ByVal dt As DataTable) As String
Dim dr As DataRow, ary() As Object, i As Integer
Dim icol As Integer
For icol = 0 To dt.Columns.Count - 1
Response.Write(dt.Columns(icol).ToString & vbTab)
Next
Response.Write(vbCrLf)
For Each dr In dt.Rows
ary = dr.ItemArray
For i = 0 To UBound(ary)
Response.Write(ary(i).ToString & vbTab)
Next
Response.Write(vbCrLf)
Next
End Function