我正在读取一个 excel 文件,每当我读取一个具有错误值的字段时,都会停止执行并转到 catch 部分。(例如在 dateTime 列中有一个字符串)我希望能够在不停止执行的情况下完成循环并将我遇到的所有错误消息存储在 stringbuilder 对象中。我不希望用户在遇到错误时更正输入文件。字符串生成器对象应显示文件中的所有错误。
请问我怎样才能做到这一点?我试过继续没有运气。
public void testThis()
{
try
{
for (int rowNumber = startRow + 1; rowNumber <= currentWorkSheet.Dimension.End.Row; rowNumber++)
// read each row from the start of the data (start row + 1 header row) to the end of the spreadsheet.
{
object col1Value = currentWorkSheet.Cells[rowNumber, 1].Value;
object col2Value = currentWorkSheet.Cells[rowNumber, 2].Value;
object col3Value = currentWorkSheet.Cells[rowNumber, 3].Value;
object col4Value = currentWorkSheet.Cells[rowNumber, 4].Value;
if ((col1Value != null && col2Value != null))
{
exampleDataList.Add(new PersonalData
{
firstname = col1Value.ToString(),
lastname = col2Value.ToString(),
currentDate = col3Value == null ? DateTime.MinValue : Convert.ToDateTime(col3Value),
mySalary = col4Value == null ? 0 : Convert.ToInt32(col4Value)
});
}
}
}
catch(Exception Exception)
{
//continue.. do not stop
}
}