Export data from SQL Server to Text and CSV file using C# and VB.Net in Windows Application

Last Reply 3 months ago By pandeyism

Posted 3 months ago

Hi! How I can export data to csv and txt file from datatable? 

using System.Windows.Forms;

namespace ExportToDifFile
{
    public partial class Form1 : Form
    {
        public DataTable dt = new DataTable();
        public Form1()
        {
            InitializeComponent();
            dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Country", typeof(string)) });
            dt.Rows.Add(1, "Rustam Pulodov", "Tajikistan");
            dt.Rows.Add(2, "Sadriddin Sharipov", "USA");
            dt.Rows.Add(3, "Firuzjon Soliev", "France");
            dt.Rows.Add(4, "Asrori Yatim", "Russia");
        }

        private void btncsv_Click(object sender, EventArgs e)
        {
            int i;
            string csv = string.Empty;            

            foreach (DataRow row in dt.Rows)
            {

                csv += row.ToString().Replace(",", ";") + ',';

                //Add new line.
                csv += "\r\n";
            }

            //Exporting to CSV.
            string folderPath = "C:\\CSV\\";
            File.WriteAllText(folderPath + "Export.csv", csv);
        }

        private void btntxt_Click(object sender, EventArgs e)
        {
            int i;
            string txt = string.Empty;  

            foreach (DataRow row in dt.Rows)
            {
                
                txt += row.ToString().Replace(",", ";") + ',';

                //Add new line.
                txt += "\r\n";
            }

            //Exporting to CSV.
            string folderPath = "C:\\CSV\\";
            File.WriteAllText(folderPath + "Result.txt", txt);
        }
    }
}

output csv:

Id

Name

Country

1

Rustam Pulodov

Tajikistan

2

Sadriddin Sharipov

USA

3

Firuzjon Soliev

France

4

Asrori Yatim

Russia

output txt file:

Id    Name           Country

1     Rustam Pulodov; Tajikistan

2     Sadriddin Sharipov; USA

3     Firuzjon Soliev; France

4     Asrori Yatim; Russia

You are viewing reply posted by: pandeyism 3 months ago.
Posted 3 months ago

Hey RPA,

Please refer below smaple.

Namespaces

C#

using System.IO;

VB.Net

Imports System.IO

Code

C#

public DataTable dt = new DataTable();
public Form1()
{
    InitializeComponent();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Country", typeof(string)) });
    dt.Rows.Add(1, "Rustam Pulodov", "Tajikistan");
    dt.Rows.Add(2, "Sadriddin Sharipov", "USA");
    dt.Rows.Add(3, "Firuzjon Soliev", "France");
    dt.Rows.Add(4, "Asrori Yatim", "Russia");
}

private void btncsv_Click(object sender, EventArgs e)
{
    string csv = string.Empty;
    foreach (DataColumn column in dt.Columns)
    {
        csv += column.ColumnName + ',';
    }
    csv += "\r\n";
    foreach (DataRow row in dt.Rows)
    {
        foreach (DataColumn column in dt.Columns)
        {
            csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
        }
        csv += "\r\n";
    }
    string folderPath = @"D:\File\";
    File.WriteAllText(folderPath + "Export.csv", csv);
}

private void btntxt_Click(object sender, EventArgs e)
{
    string txt = string.Empty;
    foreach (DataColumn column in dt.Columns)
    {
        txt += column.ColumnName + ' ';
    }
    txt += "\r\n";
    foreach (DataRow row in dt.Rows)
    {
        int i = 0;
        foreach (DataColumn column in dt.Columns)
        {
            if (i == 0)
            {
                txt += row[column.ColumnName].ToString().Replace(",", ";") + ' ';
            }
            else
            {
                txt += row[column.ColumnName].ToString().Replace(",", ";") + ';';
            }
            i++;
        }
        txt = txt.Remove(txt.Length - 1, 1);
        txt += "\r\n";
    }

    string folderPath = @"D:\File\";
    File.WriteAllText(folderPath + "Result.txt", txt);
}

VB.Net

Public dt As DataTable = New DataTable()
Public Sub New()
    InitializeComponent()
    dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, "Rustam Pulodov", "Tajikistan")
    dt.Rows.Add(2, "Sadriddin Sharipov", "USA")
    dt.Rows.Add(3, "Firuzjon Soliev", "France")
    dt.Rows.Add(4, "Asrori Yatim", "Russia")
End Sub

Private Sub btncsv_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
    Dim i As Integer
    Dim csv As String = String.Empty
    For Each column As DataColumn In dt.Columns
        csv += column.ColumnName + ","c
    Next
    csv += vbCrLf
    For Each row As DataRow In dt.Rows
        For Each column As DataColumn In dt.Columns
            csv += row(column.ColumnName).ToString().Replace(",", ";") + ","c
        Next
        csv += vbCrLf
    Next
    Dim folderPath As String = "D:\File\"
    File.WriteAllText(folderPath & "Export.csv", csv)
End Sub

Private Sub btntxt_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim txt As String = String.Empty
    For Each column As DataColumn In dt.Columns
        txt += column.ColumnName + " "c
    Next
    txt += vbCrLf
    For Each row As DataRow In dt.Rows
        Dim i As Integer = 0
        For Each column As DataColumn In dt.Columns
            If i = 0 Then
                txt += row(column.ColumnName).ToString().Replace(",", ";") + " "c
            Else
                txt += row(column.ColumnName).ToString().Replace(",", ";") + ";"c
            End If
            i += 1
        Next
        txt = txt.Remove(txt.Length - 1, 1)
        txt += vbCrLf
    Next
    Dim folderPath As String = "D:\File\"
    File.WriteAllText(folderPath & "Result.txt", txt)
End Sub

Screenshot

TextFile

CSV File