我正在上载5列,约10行的Excel文件。我想验证每一行/每一列,并确保有数据。列不能是null或为空。我正在使用Epplus。
如果某些行未通过验证,我想跟踪行/列并发送此消息回到客户。我只有一个工作表。我认为最好有一个错误模型
我该如何实现以及此代码将运往何处?
//check if there is actually a file being uploaded
if (FileUpload1.HasFile)
{
//load the uploaded file into the memorystream
using (MemoryStream stream = new MemoryStream(FileUpload1.FileBytes))
using (ExcelPackage excelPackage = new ExcelPackage(stream))
{
//loop all worksheets
foreach (ExcelWorksheet worksheet in excelPackage.Workbook.Worksheets)
{
//loop all rows
for (int i = worksheet.Dimension.Start.Row; i <= worksheet.Dimension.End.Row; i++)
{
//loop all columns in a row
for (int j = worksheet.Dimension.Start.Column; j <= worksheet.Dimension.End.Column; j++)
{
//add the cell data to the List
if (worksheet.Cells[i, j].Value != null)
{
excelData.Add(worksheet.Cells[i, j].Value.ToString());
}
}
}
}
}
}
我建议使用简单的List<string>
来跟踪您的错误。
类似:
List<string> errors = new List<string>();
然后在您的验证中:
if (String.IsNullOrEmpty(worksheet.Cells[i, j].Value))
errors.Add($"Row {i} Column {j} does not contain data.");