遍历列并找到空单元格时,为整行着色?
Loop through column and when empty cell is found, color entire row?
我想遍历一列,当我找到空单元格或包含特定数字的单元格时,整行都将被着色。
我试过的是这个(以及这个的一些变体),但它不起作用:
xl.Range end = MySheet.Cells.SpecialCells(xl.XlCellType.xlCellTypeLastCell, Type.Missing);
xl.Range start = MySheet.get_Range("Q2", end );
if (start.Value == null)
{
start.EntireRow.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
任何形式的帮助或任何好主意都将不胜感激。
更新:
找到解决方案:
xl.Range first_range = MySheet.Cells.SpecialCells(xl.XlCellType.xlCellTypeLastCell, Type.Missing);
xl.Range usedRange = MySheet.get_Range("Q2", first_range);
xl.Range rows = usedRange.Rows;
int count = 0;
foreach (xl.Range row in rows)
{
if (count > 0)
{
xl.Range firstcell = row.Cells[1];
string firstCellValue = firstcell.Value as string;
if (string.IsNullOrEmpty(firstCellValue))
{
row.EntireRow.Interior.Color = System.Drawing.Color.Red;
}
}
count++;
}
当对 10000 行这样的大数据使用 for 和 foreach 循环时,我得到
"OutOfMemory " exception
所以最好的解决方案是使用 Lambda 表达式
//best practice access cell by number not by string
yourWorksheet.RangeUsed().Rows(r => string.IsNullOrWhiteSpace(r.Cell(1).Value.ToString()))
.ForEach(r => r.Style.Fill.SetBackgroundColor(XLColor.Red));
我想遍历一列,当我找到空单元格或包含特定数字的单元格时,整行都将被着色。
我试过的是这个(以及这个的一些变体),但它不起作用:
xl.Range end = MySheet.Cells.SpecialCells(xl.XlCellType.xlCellTypeLastCell, Type.Missing);
xl.Range start = MySheet.get_Range("Q2", end );
if (start.Value == null)
{
start.EntireRow.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
任何形式的帮助或任何好主意都将不胜感激。
更新:
找到解决方案:
xl.Range first_range = MySheet.Cells.SpecialCells(xl.XlCellType.xlCellTypeLastCell, Type.Missing);
xl.Range usedRange = MySheet.get_Range("Q2", first_range);
xl.Range rows = usedRange.Rows;
int count = 0;
foreach (xl.Range row in rows)
{
if (count > 0)
{
xl.Range firstcell = row.Cells[1];
string firstCellValue = firstcell.Value as string;
if (string.IsNullOrEmpty(firstCellValue))
{
row.EntireRow.Interior.Color = System.Drawing.Color.Red;
}
}
count++;
}
当对 10000 行这样的大数据使用 for 和 foreach 循环时,我得到
"OutOfMemory " exception
所以最好的解决方案是使用 Lambda 表达式
//best practice access cell by number not by string
yourWorksheet.RangeUsed().Rows(r => string.IsNullOrWhiteSpace(r.Cell(1).Value.ToString()))
.ForEach(r => r.Style.Fill.SetBackgroundColor(XLColor.Red));