测试即将加载到表中的 Excel 文件的多个值
本文关键字:Excel 文件 加载 测试 | 更新日期: 2023-09-27 18:34:08
我正在读取 excel 文件的列。列不能为空。如何测试每一列,即col1value到col15value,而不对所有列使用&&语句
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.
{
try
{
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;
object col5Value = currentWorkSheet.Cells[rowNumber, 5].Value;
object col6Value = currentWorkSheet.Cells[rowNumber, 6].Value;
object col7Value = currentWorkSheet.Cells[rowNumber, 7].Value;
object col8Value = currentWorkSheet.Cells[rowNumber, 8].Value;
object col9Value = currentWorkSheet.Cells[rowNumber, 9].Value;
object col10Value = currentWorkSheet.Cells[rowNumber, 10].Value;
object col11Value = currentWorkSheet.Cells[rowNumber, 11].Value;
object col12Value = currentWorkSheet.Cells[rowNumber, 12].Value;
object col13Value = currentWorkSheet.Cells[rowNumber, 13].Value;
object col14Value = currentWorkSheet.Cells[rowNumber, 14].Value;
object col15Value = currentWorkSheet.Cells[rowNumber, 15].Value;
if ((col1Value != null && col2Value != null && col3Value != null))
{
excelFileDataList.Add(new priceAddDelete
{
businessunitcode = col1Value.ToString(),
customerNumber = Convert.ToInt32(col2Value),
productcode = col3Value.ToString(),
sizecode = col4Value.ToString(),
finishingmethodcode = col5Value.ToString(),
startdate = Convert.ToDateTime(col6Value),
typeofprice = col7Value.ToString(),
pricetype = Convert.ToInt32(col8Value),
typeofrent = Convert.ToInt32(col9Value),
changesperweek = Convert.ToInt32(col10Value),
adddelete = col11Value.ToString(),
price = Convert.ToInt32(col12Value),
processed = Convert.ToInt32(col13Value),
processed_date = Convert.ToDateTime(col14Value),
systemuser_id = Convert.ToInt32(col15Value)
});
}
}
catch (Exception ex)
{
}
}
您可以使用 for 循环。试试这个
bool _notempty = false;
for (int x = 1;x <= 15; x++)
{
if (currentWorkSheet.Cells[rowNumber, x].Value == null)
{
//MessageBox.Show("Error Message");
_notempty = false;
break;
}
else
{
_notempty = true;
}
}
然后将其放在循环外
if (_notempty)
{
excelFileDataList.Add(new priceAddDelete
{
businessunitcode = col1Value.ToString(),
customerNumber = Convert.ToInt32(col2Value),
productcode = col3Value.ToString(),
sizecode = col4Value.ToString(),
finishingmethodcode = col5Value.ToString(),
startdate = Convert.ToDateTime(col6Value),
typeofprice = col7Value.ToString(),
pricetype = Convert.ToInt32(col8Value),
typeofrent = Convert.ToInt32(col9Value),
changesperweek = Convert.ToInt32(col10Value),
adddelete = col11Value.ToString(),
price = Convert.ToInt32(col12Value),
processed = Convert.ToInt32(col13Value),
processed_date = Convert.ToDateTime(col14Value),
systemuser_id = Convert.ToInt32(col15Value)
});
}
您可以将列值放入对象数组或列表中,并测试其为 null。
// read each row from the start of the data (start row + 1 header row) to the end of the spreadsheet.
for (int rowNumber = startRow + 1; rowNumber <= currentWorkSheet.Dimension.End.Row; rowNumber++)
{
List<object> columns = List<object>();
columns.Add(currentWorkSheet.Cells[rowNumber, 1].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 2].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 3].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 4].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 5].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 6].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 7].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 8].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 9].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 10].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 11].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 12].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 13].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 14].Value);
columns.Add(currentWorkSheet.Cells[rowNumber, 15].Value);
if (!columns.Contains(null))
{
excelFileDataList.Add(new priceAddDelete
{
businessunitcode = columns[0].ToString(),
customerNumber = Convert.ToInt32(columns[1]),
productcode = columns[2].ToString(),
sizecode = columns[3].ToString(),
finishingmethodcode = columns[4].ToString(),
startdate = Convert.ToDateTime(columns[5]),
typeofprice = columns[6].ToString(),
pricetype = Convert.ToInt32(columns[7]),
typeofrent = Convert.ToInt32(columns[8]),
changesperweek = Convert.ToInt32(columns[9]),
adddelete = columns[10].ToString(),
price = Convert.ToInt32(columns[11]),
processed = Convert.ToInt32(columns[12]),
processed_date = Convert.ToDateTime(columns[13]),
systemuser_id = Convert.ToInt32(columns[14])
});
}
}