I am using ExcelDateReader nuget package for converting excel file to csv but while conversion getting datetime instead of date. i want the date in dd/MM/yyyy format or yyyy-MM-dd format. but getting it in yyyy-MM-dd hh:mm:ss t format. Is there any way i can do that or after conversion i using csvhelper to parse the csv file to a list of object there i am getting error while parsing the date value. So any of the solution will work either while converting it from .xls to csv change the date format or while parsing the csv file with csv helper change the date format.
Here is my code where i have tried to check if any column value have AM then replace the value with empty string but not the ideal way to solve the issue
private static bool SaveAsCsv(string excelFilePath, string destinationCsvFilePath, string password)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
using (var stream = new FileStream(excelFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
IExcelDataReader reader = null;
//var conf = new ExcelReaderConfiguration { Password = password};
if (excelFilePath.EndsWith(".xls"))
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (excelFilePath.EndsWith(".xlsx"))
{
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
if (reader == null)
return false;
var ds = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
{
UseHeaderRow = false,
}
});
var csvContent = string.Empty;
int row_no = 0;
while (row_no < ds.Tables[0].Rows.Count)
{
var arr = new List<string>();
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
if (ds.Tables[0].Rows[row_no][i].ToString().Contains("AM"))
{
ds.Tables[0].Rows[row_no][i].ToString().Replace("12:00:00 AM","");
}
arr.Add(ds.Tables[0].Rows[row_no][i].ToString()!);
}
row_no++;
csvContent += string.Join(",", arr) + "\n";
}
StreamWriter csv = new StreamWriter(destinationCsvFilePath, false);
csv.Write(csvContent);
csv.Close();
return true;
}
}
Thanks for the help in advance