关闭

VB.NET DATAGRID或DATASET 输出CSV文件

2482人阅读 评论(0) 收藏 举报

Dim lstSelect As New LstConstrol
        Dim DstTemp As DataSet
        Dim strTemp As String
        Dim strData As String
        'DATASET循环用变量
        Dim TempRow As DataRow
        Dim TempColumn As DataColumn
        'DATAGRID循环用变量
        Dim TempItem As DataGridItem
        Dim TempGridCol As DataGridColumn
        Dim i As Int32

        DstTemp = lstSelect.SelectAll()
        drgShow.DataSource = DstTemp
        drgShow.DataBind()
        'dataset循环读取
        'For Each TempRow In DstTemp.Tables(0).Rows
        '    For Each TempColumn In DstTemp.Tables(0).Columns
        '        strData += TempRow(TempColumn).ToString() + ","
        '    Next
        '    strData += ControlChars.CrLf
        'Next

        'datagrid循环读取
        '表头循环读取
        For Each TempGridCol In drgShow.Columns
            strData += TempGridCol.HeaderText + ","
        Next
        strData += ControlChars.CrLf
        '内容循环读取
        For Each TempItem In drgShow.Items
            For i = 0 To drgShow.Columns.Count - 1
                strData += TempItem.Cells(i).Text + ","
            Next
            strData += ControlChars.CrLf
        Next

        '生成输出流.输出csv
        strTemp = String.Format("attachment;filename={0}", "ExportData.csv")
        Response.ContentEncoding = Encoding.GetEncoding("Gb2312")
        Response.ClearHeaders()
        Response.AppendHeader("Content-disposition", strTemp)
        Response.Write(strData)
        Response.End()

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:249330次
    • 积分:3705
    • 等级:
    • 排名:第9053名
    • 原创:123篇
    • 转载:46篇
    • 译文:0篇
    • 评论:23条
    最新评论