for TextFieldParser
we need to add dll
Microsoft.VisualBasic
/// <summary>
/// GetDataTabletFromCSVFile
/// </summary>
/// <param name="fileImportCriteria"></param>
/// <returns></returns>
private static DataTable GetDataTabletFromCsvFile(FileImportCriteriaModel fileImportCriteria)
{
DataTable csvData = new DataTable();
try
{
using (TextFieldParser csvReader = new TextFieldParser(fileImportCriteria.FilePath))
{
csvReader.SetDelimiters(new string[] { fileImportCriteria.ManualDelimiter });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
csvData.Rows.Add(fieldData);
}
}
}
catch (System.Exception ex)
{
}
return csvData;
}
we need to add dll
Microsoft.VisualBasic
/// <summary>
/// GetDataTabletFromCSVFile
/// </summary>
/// <param name="fileImportCriteria"></param>
/// <returns></returns>
private static DataTable GetDataTabletFromCsvFile(FileImportCriteriaModel fileImportCriteria)
{
DataTable csvData = new DataTable();
try
{
using (TextFieldParser csvReader = new TextFieldParser(fileImportCriteria.FilePath))
{
csvReader.SetDelimiters(new string[] { fileImportCriteria.ManualDelimiter });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
csvData.Rows.Add(fieldData);
}
}
}
catch (System.Exception ex)
{
}
return csvData;
}
No comments:
Post a Comment