1
I’m doing a function of ExcelToEntityList
, i.e., import Excel data into a list, but need to validate start and end dates.
I’m 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 date 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)
It would be nice if you said what was the problem you found when testing (If there was an error, if it just didn’t work, these things). It is not very reasonable that someone needs to take your code, try to analyze which part you talk about, test to see if you have error, and you can already simply say the difficulty, no? I did not deny your question, but I understand who has denied it, among other things by what I have just said. There are some very nice people here to help, but it’s good to already clarify the situation more objectively, then it makes it easier for everyone.
– Bacco
Test your code if it fails we can help you.
– Denis
He of the error in Try/catch, does not allow to compile, as it gives error in dateString, hence want to know if this is the correct way to validate the dates.
– Ricardo Gonçalves