使用 Epplus 在 Excel 中交替行着色
Alternate rows coloring in Excel using Epplus
我有多行,我用白色和深灰色着色。交替行的示例:
我使用的代码:
//end.Row is the last row of the excel file
for (var row = 1; row <= end.Row; row++)
{
int pos = row % 2;
ExcelRow rowRange = BaseSheet.Row(row);
ExcelFill RowFill = rowRange.Style.Fill;
RowFill.PatternType = ExcelFillStyle.Solid;
if (pos == 0)
{
RowFill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(233, 233, 233));
}
else
{
RowFill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(255, 255, 255));
}
}
并且它按预期工作。
但在我的例子中,我有一些相同的名字,不包括最后的数字,比如(nameB_1,nameB_2)。我能够找到它们并将它们分组:
List<string> BaseLines = new List<string>();
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);
它returns它们如下:
{Index = 3, Text = "nameB"}
{Index = 4, Text = "nameB"}
我想对行进行交替着色,但如果有两个重复行(相同的行将始终在原始行之后),那么第二行的颜色将与此相同图片:
我试过这个:
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);
}
}
这种方法的问题是未复制的下一行仍然是旧颜色
所以总而言之:我想做交替行着色,除了重复行具有相同的颜色,但仍然使其余行保持着色顺序。
我在 VBA 中找到了类似问题的答案:
我已经针对我目前的情况修改了它,用这个方法替换了整个 for 循环代码:
public void ColorAlternateRows(ExcelWorksheet sheet)
{
int row;
int lastrow;
Color color1 = System.Drawing.Color.FromArgb(233, 233, 233);
Color color2 = System.Drawing.Color.FromArgb(255, 255, 255);
// "Current" colour for highlighting
Color currentColor;
currentColor = color1;
lastrow = sheet.Dimension.End.Row;
for (row = 1; row < lastrow; row++)
{
//Define the pattern type.
//The range here is all rows within the excel sheet from A to G column
sheet.Cells["A" + row + ":G" + row].Style.Fill.PatternType = ExcelFillStyle.Solid;
//Set the color of the row
sheet.Cells["A" + row + ":G" + row].Style.Fill.BackgroundColor.SetColor(currentColor);
//get the filename in column A for the first line and second line
string firstLine = sheet.Cells[row, 1].Value.ToString();
string secondLine = sheet.Cells[row+1, 1].Value.ToString();
//get the original filename without the additional suffix numbering
string firstLineWithoutSuffix = firstLine.Substring(0, firstLine.Length - 2);
string SecondLineWithoutSuffix = secondLine.Substring(0, secondLine.Length - 2);
// If column A value in next row is different, change colour
if (SecondLineWithoutSuffix != firstLineWithoutSuffix)
{
// Set to whichever colour it is not
if (currentColor == color1)
currentColor = color2;
else
currentColor = color1;
}
}
}
我有多行,我用白色和深灰色着色。交替行的示例:
我使用的代码:
//end.Row is the last row of the excel file
for (var row = 1; row <= end.Row; row++)
{
int pos = row % 2;
ExcelRow rowRange = BaseSheet.Row(row);
ExcelFill RowFill = rowRange.Style.Fill;
RowFill.PatternType = ExcelFillStyle.Solid;
if (pos == 0)
{
RowFill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(233, 233, 233));
}
else
{
RowFill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(255, 255, 255));
}
}
并且它按预期工作。 但在我的例子中,我有一些相同的名字,不包括最后的数字,比如(nameB_1,nameB_2)。我能够找到它们并将它们分组:
List<string> BaseLines = new List<string>();
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);
它returns它们如下: {Index = 3, Text = "nameB"} {Index = 4, Text = "nameB"}
我想对行进行交替着色,但如果有两个重复行(相同的行将始终在原始行之后),那么第二行的颜色将与此相同图片:
我试过这个:
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);
}
}
这种方法的问题是未复制的下一行仍然是旧颜色
所以总而言之:我想做交替行着色,除了重复行具有相同的颜色,但仍然使其余行保持着色顺序。
我在 VBA 中找到了类似问题的答案:
我已经针对我目前的情况修改了它,用这个方法替换了整个 for 循环代码:
public void ColorAlternateRows(ExcelWorksheet sheet)
{
int row;
int lastrow;
Color color1 = System.Drawing.Color.FromArgb(233, 233, 233);
Color color2 = System.Drawing.Color.FromArgb(255, 255, 255);
// "Current" colour for highlighting
Color currentColor;
currentColor = color1;
lastrow = sheet.Dimension.End.Row;
for (row = 1; row < lastrow; row++)
{
//Define the pattern type.
//The range here is all rows within the excel sheet from A to G column
sheet.Cells["A" + row + ":G" + row].Style.Fill.PatternType = ExcelFillStyle.Solid;
//Set the color of the row
sheet.Cells["A" + row + ":G" + row].Style.Fill.BackgroundColor.SetColor(currentColor);
//get the filename in column A for the first line and second line
string firstLine = sheet.Cells[row, 1].Value.ToString();
string secondLine = sheet.Cells[row+1, 1].Value.ToString();
//get the original filename without the additional suffix numbering
string firstLineWithoutSuffix = firstLine.Substring(0, firstLine.Length - 2);
string SecondLineWithoutSuffix = secondLine.Substring(0, secondLine.Length - 2);
// If column A value in next row is different, change colour
if (SecondLineWithoutSuffix != firstLineWithoutSuffix)
{
// Set to whichever colour it is not
if (currentColor == color1)
currentColor = color2;
else
currentColor = color1;
}
}
}