我有这段代码。我想在以Sat
或Sun
开头的行中为背景添加绿色。我还想用红色字体为小于特定值的单元格着色。
// Color the row when the day is Saturday or Sunday
var rang2 = ws.Range["$A$5:$M35"];
Excel.FormatCondition condition2 = (Excel.FormatCondition)rang2.FormatConditions.Add(
Excel.XlFormatConditionType.xlExpression,
Type.Missing, "=OR(TEXT($A1,\"ddd\")=\"Sun\",TEXT($A1,\"ddd\")=\"Sat\")", Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing);
// Color with red
var rang = ws.Range["$K$5:$L35"];
Excel.FormatCondition condition = (Excel.FormatCondition)rang.FormatConditions.Add(
Excel.XlFormatConditionType.xlCellValue,
Excel.XlFormatConditionOperator.xlLess, "=$L$5", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
condition2.Interior.ColorIndex = 50; condition2.Font.ColorIndex = 1; // Green Background and Black font
condition.Interior.ColorIndex = 0; condition.Font.ColorIndex = 3; // White Background and Red font
问题在于两种条件格式相互重叠。我尝试改变两种条件格式的顺序,但我仍然无法使其工作。
使用此设置,第一个规则也将包含红色字体,第二个规则仅包含白色背景和黑色文本(而不是红色文本)。
答案 0 :(得分:0)
如果您更改订单,它应该对我有效。你需要首先使用xlCellValue(s),稍后再使用xlExpression。即:这段代码对我有用:
void Main()
{
var tbl = new System.Data.DataTable();
new SqlDataAdapter(@"
WITH tally ( OrderNo, UniqueId, RandNumber )
AS (
SELECT TOP 50000
ROW_NUMBER() OVER ( ORDER BY t1.object_id ),
NEWID(),
CAST(CAST(CAST(NEWID() AS VARBINARY(4)) AS INT) AS DECIMAL) / 1000
FROM master.sys.all_columns t1
CROSS JOIN master.sys.all_columns t2
)
SELECT OrderNo,
DATEADD(DAY, -OrderNo, GETDATE()) as OrnekDate,
UniqueId, RandNumber,
abs(RandNumber)%100 / 100 as pct
FROM [tally];", @"server=.\SQLExpress;Database=master;Trusted_Connection=yes;").Fill(tbl);
object[,] arr = new object[tbl.Rows.Count + 1, tbl.Columns.Count];
for (int i = 0; i < tbl.Columns.Count; i++)
{
arr[0, i] = tbl.Columns[i].Caption;
}
for (int i = 0; i < tbl.Rows.Count; i++)
{
for (int j = 0; j < tbl.Columns.Count; j++)
{
arr[i + 1, j] = tbl.Rows[i][j].ToString();
}
}
// Excel dosya yarat ve arrayi koy
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
var workbook = xl.Workbooks.Add();
xl.Visible = true;
Worksheet sht = ((Worksheet)workbook.ActiveSheet);
Range target = (Range)sht.Range[ (Range)sht.Cells[1,1], (Range)sht.Cells[arr.GetUpperBound(0)+1,arr.GetUpperBound(1)+1] ];
target.Value = arr;
((Range)sht.Range["D:D"]).NumberFormat = "$#,##0.0000";
((Range)sht.Range["E:E"]).NumberFormat = "0.00%";
// Color with red
var rang = sht.Range["$D$5:$D35"];
FormatCondition condition = (FormatCondition)rang
.FormatConditions.Add(XlFormatConditionType.xlCellValue,
XlFormatConditionOperator.xlLess, "0");
// Color the row when the day is Saturday or Sunday
var rang2 = sht.Range["$A$5:$M35"];
FormatCondition condition2 = (FormatCondition)rang2
.FormatConditions.Add(XlFormatConditionType.xlExpression, Type.Missing,
@"=OR(TEXT($A1,""ddd"")=""Sun"",TEXT($A1,""ddd"")=""Sat"")");
condition2.Interior.ColorIndex = 50;
condition2.Font.ColorIndex = 1; // Green Background and Black font
condition.Interior.ColorIndex = 0;
condition.Font.ColorIndex = 3; // White Background and Red font
}