zoukankan      html  css  js  c++  java
  • C# 导出到CSV

    ''' <summary>
    ''' 数据导出
    ''' </summary>
    ''' <param name="dt"></param>
    ''' <param name="fileName"></param>
    ''' <remarks></remarks>
    Public Sub SaveCSV(ByVal dt As DataTable, ByVal fileName As String)

    Dim fs As New FileStream(fileName, FileMode.Create, FileAccess.Write)
    Dim sw As New StreamWriter(fs, Encoding.Default)
    Dim strs() As String

    'title
    Dim ar(dt.Columns.Count - 1) As DataColumn
    dt.Columns.CopyTo(ar,
    0)
    strs
    = Array.ConvertAll(ar, New Converter(Of DataColumn, String)(AddressOf DataColumnToString))
    sw.WriteLine(
    String.Join(",", strs))

    'data
    For i = 0 To dt.Rows.Count - 1
    strs
    = Array.ConvertAll(dt.Rows(i).ItemArray, New Converter(Of Object, String)(AddressOf ObjectToString))
    sw.WriteLine(
    String.Join(",", strs))
    Next i

    sw.Flush()
    sw.Close()
    fs.Dispose()
    End Sub

    Public Function ObjectToString(ByVal obj As Object) As String
    Return obj.ToString()
    End Function
    Public Function DataColumnToString(ByVal dc As DataColumn) As String
    Return dc.ColumnName
    End Function
  • 相关阅读:
    win10 uwp 依赖属性
    win10 uwp 依赖属性
    Java实现蓝桥杯VIP 算法训练 阶乘末尾
    PHP fileperms() 函数
    PHP fileowner() 函数
    PHP filemtime() 函数
    PHP fileinode() 函数
    PHP filegroup() 函数
    查看MySQL已经运行的时长
    [HNOI2016]矿区
  • 原文地址:https://www.cnblogs.com/cnbwang/p/1981454.html
Copyright © 2011-2022 走看看