C# Microsoft.Office.Interop.Excel.FormatConditions能否包含3种以上的条件格式?
中的文档说明: FormatConditions集合最多可以包含三种条件格式 这在中得到了进一步的加强,它说: 不能为一个范围定义三种以上的条件格式 但是,我可以为一系列单元格定义3种以上的条件格式。我是在Microsoft Visual Studio 2008中开发的一个C#应用程序中这样做的,该应用程序使用.NET framework 3.5的目标框架,并引用了Microsoft.Office.Interop.Excel.dll版本12.0.0.0C# Microsoft.Office.Interop.Excel.FormatConditions能否包含3种以上的条件格式?,c#,excel,C#,Excel,中的文档说明: FormatConditions集合最多可以包含三种条件格式 这在中得到了进一步的加强,它说: 不能为一个范围定义三种以上的条件格式 但是,我可以为一系列单元格定义3种以上的条件格式。我是在Microsoft Visual Studio 2008中开发的一个C#应用程序中这样做的,该应用程序使用.NET framework 3.5的目标框架,并引用了Microsoft.Office.Interop.Excel.dll版本12.0.0.0 using System; using S
using System;
using System.IO;
using Microsoft.Office.Interop.Excel;
namespace test
{
class Program
{
static int Main( string[] args )
{
//------------------------------
// Parse command line for output path.
string path = null;
if ( args.Length > 0 )
{
path = args[0];
}
Console.WriteLine( "Output path: '" + path + "'" );
//------------------------------
// The Excel application.
Application app = new Application();
// Disable appearance of Excel window.
app.Visible = false;
// Disable screen updates, so that changing cells is faster.
app.ScreenUpdating = false;
// Disable prompts, like whether to overwrite an existing file.
app.DisplayAlerts = false;
//------------------------------
// The workbook.
Workbook book = app.Workbooks.Add( Missing.Value );
//------------------------------
// The worksheet.
Worksheet sheet = (Worksheet)book.ActiveSheet;
//------------------------------
// The first row.
int r = 1;
int c = 1;
sheet.Cells[r, c++] = "R";
sheet.Cells[r, c++] = "Y";
sheet.Cells[r, c++] = "G";
sheet.Cells[r, c++] = "B";
sheet.Cells[r, c++] = "X";
sheet.Cells[r, c++] = "R";
sheet.Cells[r, c++] = "Y";
sheet.Cells[r, c++] = "G";
//------------------------------
// The cells to format.
Range range = sheet.get_Range( sheet.Cells[1, 1], sheet.Cells[1, 8] );
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
range.VerticalAlignment = XlVAlign.xlVAlignCenter;
range.Borders.LineStyle = XlLineStyle.xlContinuous;
//------------------------------
// *** Add more than 3 conditional format rules as a test. ***
//------------------------------
//------------------------------
// Red status.
FormatCondition cond = (FormatCondition)range.FormatConditions.Add(
XlFormatConditionType.xlCellValue, // type
XlFormatConditionOperator.xlEqual, // operator
"R", // formula1: The value or expression associated with the conditional format.
Type.Missing, // formula2
Type.Missing, // object string
Type.Missing, // textOperator
Type.Missing, // dateOperator
Type.Missing // scopeType
);
cond.Font.Bold = true;
cond.Font.Color = XlColor.WHITE;
cond.Interior.Color = XlColor.STATUS_RED;
//------------------------------
// Yellow status.
cond = (FormatCondition)range.FormatConditions.Add(
XlFormatConditionType.xlCellValue,
XlFormatConditionOperator.xlEqual,
"Y",
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing );
cond.Font.Bold = true;
cond.Interior.Color = XlColor.STATUS_YELLOW;
//------------------------------
// Green status.
cond = (FormatCondition)range.FormatConditions.Add(
XlFormatConditionType.xlCellValue,
XlFormatConditionOperator.xlEqual,
"G",
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing );
cond.Font.Bold = true;
cond.Font.Color = XlColor.WHITE;
cond.Interior.Color = XlColor.STATUS_GREEN;
//------------------------------
// Blue status.
// *** The 4th condition I supposedly should not have. ***
cond = (FormatCondition)range.FormatConditions.Add(
XlFormatConditionType.xlCellValue,
XlFormatConditionOperator.xlEqual,
"B",
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing );
cond.Font.Bold = true;
cond.Font.Color = XlColor.WHITE;
cond.Interior.Color = XlColor.STATUS_BLUE;
//------------------------------
// Another status for good measure.
// *** The 5th condition I supposedly should not have. ***
cond = (FormatCondition)range.FormatConditions.Add(
XlFormatConditionType.xlCellValue,
XlFormatConditionOperator.xlEqual,
"X",
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing );
cond.Font.Bold = true;
cond.Font.Color = XlColor.WHITE;
cond.Interior.Color = XlColor.BLACK;
//------------------------------
// Save workbook to output path.
book.SaveAs( path, // fileName
XlFileFormat.xlWorkbookDefault, // fileFormat
Type.Missing, // password
Type.Missing, // writeResPassword
false, // readOnlyRecommended
false, // createBackup
XlSaveAsAccessMode.xlNoChange, // accessMode
XlSaveConflictResolution.xlLocalSessionChanges, // conflictResolution: Should not matter since the workbook I create is not shared, but probably benign to specify it.
Type.Missing, // addToMru
Type.Missing, // textCodepage
Type.Missing, // textVisualLayout
Type.Missing // local
);
Console.WriteLine( "Saved workbook: '" + book.FullName + "'" );
book.Close( true, // saveChanges: If true, save changes to the workbook. If there is not yet a file name associated with the workbook, then given fileName is used.
path, // fileName: If saveChanges is true and fileName is omitted, the user is asked to supply a file name.
false // routeWorkbook
);
return 0;
}
}
/// <summary>
/// B8G8R8 color values that can be used with Microsoft.Office.Interop.Excel API.
/// </summary>
public enum XlColor
{
BLACK = 0x000000,
WHITE = 0xffffff,
RED = 0x0000ff,
GREEN = 0x00ff00,
BLUE = 0xff0000,
YELLOW = 0x00ffff,
MAGENTA = 0xff00ff,
CYAN = 0xffff00,
STATUS_RED = 0x0707be,
STATUS_YELLOW = 0x00ffff,
STATUS_GREEN = 0x50b000,
STATUS_BLUE = 0x784e1f,
};
}
使用系统;
使用System.IO;
使用Microsoft.Office.Interop.Excel;
名称空间测试
{
班级计划
{
静态int Main(字符串[]args)
{
//------------------------------
//解析输出路径的命令行。
字符串路径=null;
如果(参数长度>0)
{
path=args[0];
}
WriteLine(“输出路径:“+”路径+“””);
//------------------------------
//Excel应用程序。
应用程序app=新应用程序();
//禁用Excel窗口的外观。
app.Visible=false;
//禁用屏幕更新,以便更快地更改单元格。
app.screenUpdate=false;
//禁用提示,如是否覆盖现有文件。
app.DisplayAlerts=false;
//------------------------------
//工作簿。
工作簿=app.Workbooks.Add(缺少.Value);
//------------------------------
//工作表。
工作表工作表=(工作表)book.ActiveSheet;
//------------------------------
//第一排。
int r=1;
int c=1;
单元格[r,c++]=“r”;
单元格[r,c++]=“Y”;
单元格[r,c++]=“G”;
单元格[r,c++]=“B”;
单元格[r,c++]=“X”;
单元格[r,c++]=“r”;
单元格[r,c++]=“Y”;
单元格[r,c++]=“G”;
//------------------------------
//要格式化的单元格。
范围范围=sheet.get_范围(sheet.Cells[1,1],sheet.Cells[1,8]);
range.HorizontalAlignment=XlHAlign.xlHAlignCenter;
range.VerticalAlignment=XlVAlign.xlVAlignCenter;
range.Borders.LineStyle=XlLineStyle.xlContinuous;
//------------------------------
//***添加3个以上的条件格式规则作为测试***
//------------------------------
//------------------------------
//红色状态。
FormatCondition cond=(FormatCondition)range.FormatConditions.Add(
XlFormatConditionType.xlCellValue,//类型
XlFormatConditionOperator.xlEqual,//运算符
“R”,//公式1:与条件格式关联的值或表达式。
Type.Missing,//公式2
Type.Missing,//对象字符串
Type.Missing,//textOperator
类型。缺少,//dateOperator
类型。缺少//范围类型
);
cond.Font.Bold=true;
cond.Font.Color=XlColor.WHITE;
cond.Interior.Color=XlColor.STATUS_RED;
//------------------------------
//黄色状态。
cond=(FormatCondition)range.FormatConditions.Add(
XlFormatConditionType.xlCellValue,
XlFormatConditionOperator.xlEqual,
“Y”,
类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失);
cond.Font.Bold=true;
cond.Interior.Color=XlColor.STATUS_黄色;
//------------------------------
//绿色状态。
cond=(FormatCondition)range.FormatConditions.Add(
XlFormatConditionType.xlCellValue,
XlFormatConditionOperator.xlEqual,
“G”,
类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失);
cond.Font.Bold=true;
cond.Font.Color=XlColor.WHITE;
cond.Interior.Color=XlColor.STATUS_绿色;
//------------------------------
//蓝色状态。
//***第四个条件我本不应该有***
cond=(FormatCondition)range.FormatConditions.Add(
XlFormatConditionType.xlCellValue,
XlFormatConditionOperator.xlEqual,
“B”,
类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失);
cond.Font.Bold=true;
cond.Font.Color=XlColor.WHITE;
cond.Interior.Color=XlColor.STATUS_蓝色;
//------------------------------
//另一个良好的衡量地位。
//***第五个条件我不应该有***
cond=(FormatCondition)range.FormatConditions.Add(
XlFormatConditionType.xlCellValue,
XlFormatConditionOperator.xlEqual,
“X”,
类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失,类型。缺失);
cond.Font.Bold=true;
cond.Font.Color=XlColor.WHITE;
cond.Interior.Color=XlColor.BLACK;
//------------------------------
//将工作簿保存到输出路径。
book.SaveAs(路径,//文件名
XlFileFormat.xlWorkbookDefault,//fileFormat
类型。缺少,//密码
Type.Missing,//writeResPassword
false,//只读建议
false,//createBackup
XlSaveAsAccessMode.xlNoChange,//accessMode
XlSaveConflictResolution.xlLocalSessionChanges,//conflictResolution:应该无关紧要,因为我创建的工作簿不是共享的,但可能需要指定它。
Type.Missing,//addToMru
Type.Missing,//textCodepage
Type.Missing,//textVisualLayout
类型。缺少//本地
);
Console.WriteLine(“保存的工作簿:”+book.FullName