我们想从数据创建一个 CSV 文件。它显示 HTML 标记。同样适用于 xlx 文件。下面是代码。
Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
response.Clear()
response.ClearHeaders()
response.Write("<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8""/>")
'Chose what type of file needed ie.e CSV /XLS
If _exportType = ExportTypeEnum.CSV Then
response.ContentType = "application/vnd.xls"
Else
response.ContentType = "application/vnd.xls"
End If
response.AddHeader("Content-Disposition", "attachment;filename=" & FileNameToExport)
Dim sw As System.IO.StringWriter = New StringWriter()
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
Dim dg As DataGrid = New DataGrid()
Dim dv1 As DataView = data
dg.DataSource = dv1
dg.BorderStyle = BorderStyle.None
dg.DataBind()
dg.RenderControl(htw)
response.Write(sw.ToString())
response.End()
请提出为什么它不适用于 CSV 文件,尽管它适用于 xls 文件。
提前谢谢了