Excel C# 中的 EPPLUS 多列配置和条件格式
EPPLUS multiple columns configuration and conditional formatting in Excel C#
在我的工具中,用户可以选择一种配置(通过组合框->多个数据table),相应的table将反映在excel sheet中每个 below.Columns(行中的数据会有所不同)对于所有配置都将保持不变的是产品名称、序列名称和长度 1 以及总长度。不同的配置会添加长度 2、长度 3、长度 4 等列(用户将在这些行中添加数据)等
我想在总长度列中添加条件格式公式,如果在范围内(最小值到最大值)背景单元格将变为绿色,超出范围时背景单元格将变为红色。当用户在 excel 中添加数据时,我坚持使用没有 solution.It 的代码没有改变任何颜色。帮助。谢谢!
Table
private void ManualFormatExcelandAddRules(ExcelWorksheet WS, DataTable DTtoFormat, int ColStartAddOfDT, int RowStartAddOfDT)
{
int colCountofDT = DTtoFormat.Columns.Count;
int rowCountofDT = DTtoFormat.Rows.Count;
double minval = 0;
double maxval = 0;
int flag = 0;
for (int Colno = ColStartAddOfDT; Colno < ColStartAddOfDT + colCountofDT; Colno++)
{
WS.Cells[RowStartAddOfDT, Colno].Style.Border.BorderAround(ExcelBorderStyle.Thin);
for (int RowNo = RowStartAddOfDT + 1; RowNo <= RowStartAddOfDT + rowCountofDT; RowNo++)
{ if (WS.Cells[RowNo, Colno].Text.Contains("to") && WS.Cells[RowNo, ColStartAddOfDT].Text.Contains("DRAM"))
{
string[] GuidelineVal = WS.Cells[RowNo, Colno].Text.Split("to".ToArray(), StringSplitOptions.RemoveEmptyEntries).ToArray();
if (GuidelineVal[0].Trim() != "NA" && GuidelineVal[1].Trim() != "NA")
{
minval = Convert.ToDouble(GuidelineVal[0].Trim());
maxval = Convert.ToDouble(GuidelineVal[1].Trim());
flag = 0;
}
else
flag = 1;
}
else if (WS.Cells[RowNo, Colno].Text == "" && WS.Cells[RowStartAddOfDT + 1, Colno].Text.Contains("to"))
{
if (flag == 0)
{
string _statement = "AND(Convert.ToDouble(WS.Cells[RowNo, Colno].Text) >= minval,Convert.ToDouble(WS.Cells[RowNo, Colno].Text) <= maxval)";
var _cond = WS.ConditionalFormatting.AddExpression(WS.Cells[RowNo, Colno]);
_cond.Formula = _statement;
_cond.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond.Style.Fill.BackgroundColor.Color = Color.Green;
}
else
WS.Cells[RowNo, Colno].Style.Fill.BackgroundColor.SetColor(Color.Red);
WS.Cells[RowNo, Colno].Style.Border.BorderAround(ExcelBorderStyle.Thin);
}
}
}
您使用的条件格式表达式是 wrong/contains 语法 errors/uses 不存在的函数,这使得 Excel 将忽略它,因为它不理解它需要什么去做。
查看您的代码,您有 4 个变量组成该表达式:
RowNo
和 ColNo
指示将条件格式应用于 的单元格
minval
和 maxval
是条件的下限和上限
以下代码使用这些变量构建正确的表达式:
string _statement = string.Format(
CultureInfo.InvariantCulture,
"AND({0}>={1},{0}<={2})",
new OfficeOpenXml.ExcelCellAddress(RowNo, ColNo).Address,
minval,
maxval );
var _cond = WS.ConditionalFormatting.AddExpression(WS.Cells[RowNo, ColNo]);
_cond.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond.Style.Fill.BackgroundColor.Color = Color.Green;
_cond.Formula = _statement;
请注意,表达式仅使用有效的 Excel 函数。你不能混入像 Convert.ToDouble
这样的 .Net 语句。使用 InvariantCulture 进行数字转换也很重要,否则分隔符可能会被解释为函数中的额外参数。
当您调试时,此 _statement 将包含以下内容:AND(A2>=40.2,A2<=44.5)
并且当应用于 A2 单元格时,它会像宣传的那样工作。
在我的工具中,用户可以选择一种配置(通过组合框->多个数据table),相应的table将反映在excel sheet中每个 below.Columns(行中的数据会有所不同)对于所有配置都将保持不变的是产品名称、序列名称和长度 1 以及总长度。不同的配置会添加长度 2、长度 3、长度 4 等列(用户将在这些行中添加数据)等
我想在总长度列中添加条件格式公式,如果在范围内(最小值到最大值)背景单元格将变为绿色,超出范围时背景单元格将变为红色。当用户在 excel 中添加数据时,我坚持使用没有 solution.It 的代码没有改变任何颜色。帮助。谢谢!
Table
private void ManualFormatExcelandAddRules(ExcelWorksheet WS, DataTable DTtoFormat, int ColStartAddOfDT, int RowStartAddOfDT)
{
int colCountofDT = DTtoFormat.Columns.Count;
int rowCountofDT = DTtoFormat.Rows.Count;
double minval = 0;
double maxval = 0;
int flag = 0;
for (int Colno = ColStartAddOfDT; Colno < ColStartAddOfDT + colCountofDT; Colno++)
{
WS.Cells[RowStartAddOfDT, Colno].Style.Border.BorderAround(ExcelBorderStyle.Thin);
for (int RowNo = RowStartAddOfDT + 1; RowNo <= RowStartAddOfDT + rowCountofDT; RowNo++)
{ if (WS.Cells[RowNo, Colno].Text.Contains("to") && WS.Cells[RowNo, ColStartAddOfDT].Text.Contains("DRAM"))
{
string[] GuidelineVal = WS.Cells[RowNo, Colno].Text.Split("to".ToArray(), StringSplitOptions.RemoveEmptyEntries).ToArray();
if (GuidelineVal[0].Trim() != "NA" && GuidelineVal[1].Trim() != "NA")
{
minval = Convert.ToDouble(GuidelineVal[0].Trim());
maxval = Convert.ToDouble(GuidelineVal[1].Trim());
flag = 0;
}
else
flag = 1;
}
else if (WS.Cells[RowNo, Colno].Text == "" && WS.Cells[RowStartAddOfDT + 1, Colno].Text.Contains("to"))
{
if (flag == 0)
{
string _statement = "AND(Convert.ToDouble(WS.Cells[RowNo, Colno].Text) >= minval,Convert.ToDouble(WS.Cells[RowNo, Colno].Text) <= maxval)";
var _cond = WS.ConditionalFormatting.AddExpression(WS.Cells[RowNo, Colno]);
_cond.Formula = _statement;
_cond.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond.Style.Fill.BackgroundColor.Color = Color.Green;
}
else
WS.Cells[RowNo, Colno].Style.Fill.BackgroundColor.SetColor(Color.Red);
WS.Cells[RowNo, Colno].Style.Border.BorderAround(ExcelBorderStyle.Thin);
}
}
}
您使用的条件格式表达式是 wrong/contains 语法 errors/uses 不存在的函数,这使得 Excel 将忽略它,因为它不理解它需要什么去做。
查看您的代码,您有 4 个变量组成该表达式:
RowNo
和ColNo
指示将条件格式应用于 的单元格
minval
和maxval
是条件的下限和上限
以下代码使用这些变量构建正确的表达式:
string _statement = string.Format(
CultureInfo.InvariantCulture,
"AND({0}>={1},{0}<={2})",
new OfficeOpenXml.ExcelCellAddress(RowNo, ColNo).Address,
minval,
maxval );
var _cond = WS.ConditionalFormatting.AddExpression(WS.Cells[RowNo, ColNo]);
_cond.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond.Style.Fill.BackgroundColor.Color = Color.Green;
_cond.Formula = _statement;
请注意,表达式仅使用有效的 Excel 函数。你不能混入像 Convert.ToDouble
这样的 .Net 语句。使用 InvariantCulture 进行数字转换也很重要,否则分隔符可能会被解释为函数中的额外参数。
当您调试时,此 _statement 将包含以下内容:AND(A2>=40.2,A2<=44.5)
并且当应用于 A2 单元格时,它会像宣传的那样工作。