У меня есть несколько строк, которые я закрасил белым и темно-серым. пример для альтернативных строк:
The code I used:
//end.Row is the last row of the excel file
for (var row = 1; row
and it works as intended.
But in my case here I have some identical names not counting the numbers at the end like (nameB_1 , nameB_2). I was able to find and group them with:
List BaseLines = new List();
for (int row = 1; row <= end.Row; row++)
{
//get the filename column
string fileName = BaseSheet.Cells[row, 1].Value.ToString();
BaseLines.Add(fileName.Substring(0, fileName.Length - 2));
}
var duplicateIndexes = BaseLines
.Select((t, i) => new { Index = i, Text = t })
.GroupBy(g => g.Text)
.Where(g => g.Count() > 1)
.SelectMany(g => g, (g, x) => x.Index);
It returns them as follows:
{Index = 3, Text = "nameB"}
{Index = 4, Text = "nameB"}
I want to do alternate rows coloring but making an exception in the case there is a two duplicate rows(the identical row will always be after the original one) so that the second row will be the same color like in this image:
I tried this:
for (var row = 1; row < end.Row; row++)
{
int pos = row % 2;
ExcelRow rowRange = BaseSheet.Row(row+1);
ExcelFill RowFill = rowRange.Style.Fill;
RowFill.PatternType = ExcelFillStyle.Solid;
//get the filename column
string fileName = BaseSheet.Cells[row, 1].Value.ToString();
//Get the filename without the last two characters to find continued lines
string NewfileName = fileName.Substring(0, fileName.Length - 2);
//To find the first instance to take the color from
var FirstDuplicate = duplicateIndexes .Where((x, i) => i % 2 == 0);
bool isduplicate= false;
foreach (var item in FirstDuplicate)
{
if (item == row)
{
isduplicate= true;
}
}
if (isduplicate== false)
{
if (pos == 0)
{
RowFill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(233, 233, 233));
}
else
{
RowFill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(255, 255, 255));
}
}
else
{
//Get the color of the first instance
var Hexcolor = BaseSheet.Cells[row, 1].Style.Fill.BackgroundColor.Rgb;
Color RGBColor = System.Drawing.ColorTranslator.FromHtml("#" + Hexcolor);
//Set the color to the current duplicate
RowFill.BackgroundColor.SetColor(RGBColor);
}
}
The problem with this approach that the next line that is not duplicated still has it's old color
введите описание изображения здесь
Итак, вкратце: я хочу сделать альтернативную раскраску строк, за исключением дублирования строк, чтобы они имели один и тот же цвет, но при этом оставшиеся строки сохраняли порядок окраски.