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