Can can anyone tell me how to convert text file into datatable?
Loading
Can can anyone tell me how to convert text file into datatable?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satya KarkiPosted Aug 14, 2021, 7:27 AM
Rijwan AnsariPosted Aug 14, 2021, 7:25 AM
Ganesan CPosted Aug 14, 2021, 6:27 AM
Meena BishtPosted Aug 13, 2021, 1:39 PM
Hello ,
You can try the below lines of code to read the txt file and convert it to a datatable.
var table = new DataTable();
var fileContents = File.ReadAllLines("yourFile");
var splitFileContents = (from f in fileContents select f.Split(':')).ToArray();
int maxLength = (from s in splitFileContents select s.Count()).Max();
for (int i = 0; i < maxLength; i++)
{
table.Columns.Add();
}
foreach (var line in splitFileContents)
{
DataRow row = table.NewRow();
row.ItemArray = (object[])line;
table.Rows.Add(row);
}
Thanks,
Jignesh KumarPosted Aug 6, 2021, 12:17 PM
Sachin SinghPosted Aug 6, 2021, 9:46 AM