public FileDataModel ReadTxtFile(string filePath)
{
string[] textData = System.IO.File.ReadAllLines(filePath);
string[] headers = textData[0].Split('\t');
DataTable dataTable1 = new DataTable();
foreach (string header in headers)
dataTable1.Columns.Add(header, typeof(string), null);
for (int i = 1; i < textData.Length; i++)
{
dataTable1.Rows.Add(textData[i].Split('\t'));
}
var fileDataModel = new FileDataModel();
// Convert From Table To Object
fileDataModel = ConvertFromTableToObject(dataTable1);
return fileDataModel;
}
private static FileDataModel ConvertFromTableToObject(DataTable dataTable1)
{
var fileDataModel = new FileDataModel();
var listDataResult = new List<List<FieldImports>>();
var columnList = new List<FieldImports>();
for (var columnIndex = 0; columnIndex < dataTable1.Columns.Count; columnIndex++)
{
var result = new FieldImports();
result.ColumnName = Convert.ToString(dataTable1.Columns[columnIndex]);
columnList.Add(result);
}
for (var rowIndex = 0; rowIndex < dataTable1.Rows.Count; rowIndex++)
{
var fileData = new List<FieldImports>();
for (var columnIndex = 0; columnIndex < dataTable1.Columns.Count; columnIndex++)
{
var result = new FieldImports();
result.ColumnValue = Convert.ToString(dataTable1.Rows[rowIndex][columnIndex]);
fileData.Add(result);
}
listDataResult.Add(fileData);
}
fileDataModel.fileData = listDataResult;
fileDataModel.columnList = columnList;
return fileDataModel;
}
{
string[] textData = System.IO.File.ReadAllLines(filePath);
string[] headers = textData[0].Split('\t');
DataTable dataTable1 = new DataTable();
foreach (string header in headers)
dataTable1.Columns.Add(header, typeof(string), null);
for (int i = 1; i < textData.Length; i++)
{
dataTable1.Rows.Add(textData[i].Split('\t'));
}
var fileDataModel = new FileDataModel();
// Convert From Table To Object
fileDataModel = ConvertFromTableToObject(dataTable1);
return fileDataModel;
}
private static FileDataModel ConvertFromTableToObject(DataTable dataTable1)
{
var fileDataModel = new FileDataModel();
var listDataResult = new List<List<FieldImports>>();
var columnList = new List<FieldImports>();
for (var columnIndex = 0; columnIndex < dataTable1.Columns.Count; columnIndex++)
{
var result = new FieldImports();
result.ColumnName = Convert.ToString(dataTable1.Columns[columnIndex]);
columnList.Add(result);
}
for (var rowIndex = 0; rowIndex < dataTable1.Rows.Count; rowIndex++)
{
var fileData = new List<FieldImports>();
for (var columnIndex = 0; columnIndex < dataTable1.Columns.Count; columnIndex++)
{
var result = new FieldImports();
result.ColumnValue = Convert.ToString(dataTable1.Rows[rowIndex][columnIndex]);
fileData.Add(result);
}
listDataResult.Add(fileData);
}
fileDataModel.fileData = listDataResult;
fileDataModel.columnList = columnList;
return fileDataModel;
}
No comments:
Post a Comment