将datatable下载为csv文件

时间:2013-04-11 07:07:30

标签: asp.net vb.net datatable

大家好我试图允许用户从数据库下载数据,这样他们就可以在不访问数据库的情况下对其进行操作。到目前为止,我有这个文件,但我需要它下载到客户端电脑。有什么提示吗?

Protected Sub ExportDataTableToCSV(cmdText As String)
    Try
        Dim sqlcon_Local As New SqlClient.SqlConnection
        sqlcon_Local.ConnectionString = ConfigurationManager.ConnectionStrings("Portal").ConnectionString
        Dim objCmd As New SqlCommand(cmdText, sqlcon_Local)
        Dim da As SqlDataAdapter = New SqlDataAdapter(objCmd)
        Dim dt As DataTable = New DataTable
        da.Fill(dt)
        Dim filename As String = Server.MapPath("~/download.csv")
        Dim sw As New StreamWriter(filename, False)

        Dim iColCount As Integer = dt.Columns.Count
        For i As Integer = 0 To iColCount - 1
            sw.Write(dt.Columns(i))
            If i < iColCount - 1 Then
                sw.Write(",")
            End If
        Next
        sw.Write(sw.NewLine)

        For Each dr As DataRow In dt.Rows
            For i As Integer = 0 To iColCount - 1
                If Not Convert.IsDBNull(dr(i)) Then
                    sw.Write(dr(i).ToString())
                End If
                If i < iColCount - 1 Then
                    sw.Write(",")
                End If
            Next
            sw.Write(sw.NewLine)
        Next
        sw.Close()

        Response.Clear()
        Response.ContentType = "application/csv"
        Response.AddHeader("Content-Disposition", "attachment; filename=download.csv")
        Response.WriteFile(filename)
        Response.Flush()
        Response.End()

    Catch ex As Exception

    End Try
End Sub
我做错了什么? 任何意见都欢迎。

谢谢你的时间!

2 个答案:

答案 0 :(得分:0)

请将Response.ContentType更改为

Response.ContentType =“text / csv”;

希望这会对你有所帮助

答案 1 :(得分:0)

   Public Shared Function Export_CSV(ByVal cmdText As String) As String

        Dim message As String = Nothing

        Try

            Dim sqlcon_Local As New SqlClient.SqlConnection
            sqlcon_Local.ConnectionString = ConfigurationManager.ConnectionStrings("Portal").ConnectionString
            Dim objCmd As New SqlCommand(cmdText, sqlcon_Local)
            Dim da As SqlDataAdapter = New SqlDataAdapter(objCmd)
            Dim dt As DataTable = New DataTable
            da.Fill(dt)

            Dim context As HttpContext = HttpContext.Current
            context.Response.Clear()

            For Each column As DataColumn In dt.Columns
                context.Response.Write(column.ColumnName + ",")
            Next

            context.Response.Write(Environment.NewLine)

            For Each row As DataRow In dt.Rows

                For i As Integer = 0 To dt.Columns.Count - 1
                    context.Response.Write(row(i).ToString.Replace(",", String.Empty) + ",")
                Next

                context.Response.Write(Environment.NewLine)

            Next

            context.Response.ContentType = "txt/csv"
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + download + ".csv")

            HttpContext.Current.ApplicationInstance.CompleteRequest()

            message = "Export Successful"

        Catch ex As Exception

            message = ex.Message.ToString

        End Try

        Return message

    End Function

参考:http://www.thewebbureau.com/tech_blog/post/Export-DataTable-To-CSV.aspx

如果IE浏览器有问题

更改

 context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + download + ".csv")

 context.Response.AppendHeader("Content-Disposition", "attachment; inline=" + download + ".csv")