Export data to csv file

AMER SAID 396 Reputation points
2021-04-27T12:42:13.22+00:00

hi

I want to export data from datatable to csv file.

when you group student names STU_NAMES, the number of columns that appear in the csv file will be as numbers from 0 to the last values.
When group STU_TEST, each row is titled in a csv file
The data appears horizontally for each student according to a value and number in the column
91705-tacsv1.png

91751-stud3.png

result

91732-tacsv2.png

The method I use to export data for a text file.
The data is as it is, not vertically.

Blockquote

Public Sub csvexportd(ByVal dT As DataTable, ByVal filpath As String)
Dim thecsvfile As String = String.Empty

     Dim nameHeader0 = dT.Columns(1).ColumnName.ToString   
    Dim nameHeader1 = dT.Columns(2).ColumnName.ToString   
    Dim nameHeader2 = dT.Columns(3).ColumnName.ToString   
    

    thecsvfile &= String.Join(",", {nameHeader0, nameHeader1, nameHeader2}) & vbNewLine  



    Dim I As Single = 0  
    For I = 0 To dT.Rows.Count - 1  
        If dT Is Nothing Then  
            Exit For : Exit Sub  

        End If  

        '//  
        Dim nameValue0 = ""  
        Dim nameValue1 = ""  
        Dim nameValue2 = ""  
        
        
        '//  

         
            nameValue0 = dT.Rows(I).Item(0).ToString()  
            nameValue1 = dT.Rows(I).Item(1).ToString()  
            nameValue2 = dT.Rows(I).Item(2).ToString()  
           

            'thecsvfile &= String.Join(",", {nameValue0, nameValue1, nameValue2}) & vbNewLine  

        End If  

    Next  
    thecsvfile = thecsvfile.Trim  

    My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Excel\" & filpath & now & ".csv", thecsvfile, False)  
     
End Sub  
Developer technologies | VB
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-04-28T05:56:10.99+00:00

    Hi @AMER SAID ,
    You can refer to the following method to generate the table:

        Public Function GenerateTable(ByVal sourceTable As DataTable) As DataTable  
            Dim newTable As DataTable = New DataTable()  
            newTable.Columns.Add("STU_TEST")  
            Dim TArr As List(Of String) = New List(Of String)()  
      
            For Each row As DataRow In sourceTable.Rows  
                Dim columnName As String = row.ItemArray(0).ToString()  
                Dim rowName As String = row.ItemArray(2).ToString()  
      
                If Not newTable.Columns.Contains(columnName) Then  
                    newTable.Columns.Add(columnName)  
                End If  
                If Not TArr.Contains(rowName) Then  
                    TArr.Add(rowName)  
                End If  
            Next  
            For Each TValue As String In TArr  
                Dim row As DataRow = newTable.NewRow()  
                row(0) = TValue  
      
                For i As Integer = 1 To newTable.Columns.Count - 1  
                    row(i) = sourceTable.AsEnumerable().Where(Function(x) (x.Field(Of String)("STU_NAME") = newTable.Columns(i).ColumnName) AndAlso (x.Field(Of String)("STU_TEST") = TValue)).Select(Function(x) x.Field(Of String)("STU_RATE")).FirstOrDefault()  
                Next  
                newTable.Rows.Add(row)  
            Next  
            Return newTable  
        End Function  
    

    Code in my test:

            Dim dt As DataTable = New DataTable()  
            dt.Columns.Add("STU_NAME")  
            dt.Columns.Add("STU_RATE")  
            dt.Columns.Add("STU_TEST")  
            dt.Rows.Add("ALIE", "0", "T1")  
            dt.Rows.Add("SAIED", "", "T1")  
            dt.Rows.Add("EBRAHIM", "#", "T1")  
            dt.Rows.Add("ALIE", "0", "T2")  
            dt.Rows.Add("SAIED", "NOT", "T2")  
            dt.Rows.Add("EBRAHIM", "1", "T2")  
            dt.Rows.Add("SALIM", "NOT", "T2")  
            dt.Rows.Add("MAHER", "2", "T2")  
            Dim newTable As DataTable = GenerateTable(dt)  
    

    Result:
    91909-2.png
    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.