我正在尝试使用C#和EPPLUS返回Excel工作表,该工作表将向用户显示哪些行需要注意。包含sku的单元格的背景设置为红色,其他任何已检查的单元格都设置为黄色。
有效。
我在删除不会导致任何错误的行时遇到问题。我不确定删除一行是否会使下一行的行号与删除的行号相同,或者它是否保持不变。我目前正在获取对象引用未设置为对象错误的实例。
//Loops through the errors datatable pulling the sku's from there and entering them into an array. Then loops through each row on the excel sheet and checks to compare against sku array. If it is not in there, that row is deleted
int[,] errorList = new int[errors.Rows.Count, 3];
for (int i = 0; i < errors.Rows.Count; i++)
{
errorList[i, 0] = Convert.ToInt32(errors.Rows[i][0]); //SKU
errorList[i, 1] = Convert.ToInt32(errors.Rows[i][1]); //Brand
errorList[i, 2] = Convert.ToInt32(errors.Rows[i][2]); //Secondary Identifier
}
//Loop through the Excel sheet
for (int i = 2; i <= rowCnt; i++)
{
//Loop through the error array
for (int j = 0; j < errorList.Length/3; j++)
{
//Column 3 = SKU
if ((worksheet.Cells[i, 3].Value).ToString().Equals(errorList[j,0].ToString()))
{
worksheet.Cells[i, 3].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[i, 3].Style.Fill.BackgroundColor.SetColor(Color.Red);
//If brand caused an error
if (errorList[j,1] == 1)
{
worksheet.Cells[i, 5].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[i, 5].Style.Fill.BackgroundColor.SetColor(Color.Yellow);
}
//If secondary identifier(Destination) caused an error
if (errorList[j,2] == 1)
{
worksheet.Cells[i, 22].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[i, 22].Style.Fill.BackgroundColor.SetColor(Color.Yellow);
}
break;
}
}
//Check to see if the cell's background colour is red. If it isn't, delete the row
if(!worksheet.Cells[i,3].Style.Fill.BackgroundColor.Rgb.Equals(Color.Red.ToString()))
{
worksheet.DeleteRow(i);
i--;
}
}
如何使用EPPlus在Excel中删除行?
问题援助:
删除第5行
第6行保留为第6行 OR