• 쇼핑몰
  • 커뮤니티
  • 북마크

vb.net


[기타] [vb.net] DataTable to CSV

하나를하더라도최선을
2021.06.04 05:45 464 0

본문



    Private Function CSVBuilder(dt As DataTable, headers As List(Of String), columns As List(Of String)) As String
        Dim sCSV = New StringBuilder(String.Join(",", headers))
        sCSV.Append(Environment.NewLine)
 
        Dim view As New DataView(dt)
        Dim tDt As DataTable = view.ToTable(True, columns.ToArray)
 
        For Each row As DataRow In tDt.Rows
            '-- Handle comma
            sCSV.Append(String.Join(",", (From rw In row.ItemArray Select If(rw.ToString.Trim.Contains(","), String.Format("""{0}""", rw.ToString.Trim), rw.ToString.Trim))))
            sCSV.Append(Environment.NewLine)
        Next
 
        Return sCSV.ToString
    End Function
 

Example:
        T = CSVBuilder(D,
                                     "상품코드,상품명,바코드,제조년월,제조원,원산지,주의사항,신고번호,제형,용량(매수)".Split(","c).ToList,
                                     "상품코드,상품명,바코드,제조년월,제조원,원산지,주의사항,신고번호,제형,용량(매수)".Split(","c).ToList)


댓글목록 0

등록된 댓글이 없습니다.