I'm doing a ExcelToEntityList
function, that is, importing Excel data into a list, but I need to validate the start and end dates.
Am I doing it right?
The function:
public List<ProdutosConfiguracaoTaxas> ExcelToEntityList(ExcelPackage package, out bool hadErrors)
{
hadErrors = false;
List<ProdutosConfiguracaoTaxas> taxes = new List<ProdutosConfiguracaoTaxas>();
ExcelWorksheet workSheet = package.Workbook.Worksheets.First();
DataTable table = new DataTable();
foreach (ExcelRangeBase firstRowCell in workSheet.Cells[1, 1, 1, workSheet.Dimension.End.Column])
{
table.Columns.Add(firstRowCell.Text);
}
for (int rowNumber = 2; rowNumber <= workSheet.Dimension.End.Row; rowNumber++)
{
ProdutosConfiguracaoTaxas tax = new ProdutosConfiguracaoTaxas();
ExcelRangeBase row = workSheet.Cells[rowNumber, 1, rowNumber, workSheet.Dimension.End.Column];
DateTime DtInicio, DtFim;
DataRow newRow = table.NewRow();
bool value = false;
foreach (ExcelRangeBase cell in row)
{
newRow[cell.Start.Column - 1] = cell.Text;
if (!string.IsNullOrEmpty(cell.Text))
{
value = true;
}
}
if (!value) { return taxes; }
try
{
string dateString;
var formats = new[] { "dd/MM/yyyy", "yyyy-MM-dd" };
if (DateTime.TryParseExact(dateString, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out DtInicio))
{
tax.Data_Incio = ITCore.ExtensionsMethods.ToDateTime((string)newRow[2]);
}
else
{
}
tax.NumeroPrestacoes = (int)newRow[0];
tax.TAN = (int)newRow[1];
tax.Data_Fim = ITCore.ExtensionsMethods.ToDateTime((string)newRow[3]);
}
catch (Exception)
{
hadErrors = true;
}
taxes.Add(tax);
}
return taxes;
}
Part I'm doing the dates validation:
try
{
string dateString;
var formats = new[] { "dd/MM/yyyy", "yyyy-MM-dd" };
if (DateTime.TryParseExact(dateString, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out DtInicio))
{
tax.Data_Incio = ITCore.ExtensionsMethods.ToDateTime((string)newRow[2]);
}
else
{
}
tax.NumeroPrestacoes = (int)newRow[0];
tax.TAN = (int)newRow[1];
tax.Data_Fim = ITCore.ExtensionsMethods.ToDateTime((string)newRow[3]);
}
catch (Exception)