Excel および ODS ファイルの条件付き書式を設定する。
紹介
条件付き書式は、Microsoft Excel の高度な機能で、セルやセル範囲に書式を適用し、その値や数式に応じて書式を変更することができます。たとえば、セルの値が 500 より大きいときにのみ太字にすることができます。セルの値が条件を満たした場合、指定された書式が適用されます。セルの値が書式条件を満たさない場合は、セルのデフォルトの書式が使用されます。Microsoft Excel では、書式、条件付き書式 を選択して、条件付き書式ダイアログを開きます。
Aspose.Cells はセルに条件付き書式を実行時に適用することをサポートしています。この記事ではその方法を説明します。また、Excel が色スケールの条件付き書式に使用する色の計算方法も説明します。
条件付き書式の適用
Aspose.Cells はいくつかの方法で条件付き書式をサポートしています。
- デザイナー スプレッドシートを使用
- コピー メソッドを使用
- 実行時に条件付き書式を作成
デザイナー スプレッドシートを使用
開発者は、Microsoft Excel で条件付き書式を含むデザイナー スプレッドシートを作成し、そのスプレッドシートを Aspose.Cells で開くことができます。 Aspose.Cells は、デザイナー スプレッドシートを読み込み、保持し、条件付き書式の設定を保持します。
コピー メソッドを使用
Aspose.Cells は、ワークシート内のセルから別のセルへ条件付き書式設定をコピーすることができます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Copying conditional format settings from cell "A1" to cell "B1" | |
//worksheet.CopyConditionalFormatting(0, 0, 0, 1); | |
int TotalRowCount = 0; | |
for (int i = 0; i < workbook.Worksheets.Count; i++) | |
{ | |
Worksheet sourceSheet = workbook.Worksheets[i]; | |
Range sourceRange = sourceSheet.Cells.MaxDisplayRange; | |
Range destRange = worksheet.Cells.CreateRange(sourceRange.FirstRow + TotalRowCount, sourceRange.FirstColumn, | |
sourceRange.RowCount, sourceRange.ColumnCount); | |
destRange.Copy(sourceRange); | |
TotalRowCount = sourceRange.RowCount + TotalRowCount; | |
} | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
ランタイムで条件付き書式を適用
Aspose.Cells では、条件付き書式をランタイムで追加および削除することができます。以下のコードサンプルでは、条件付き書式の設定方法を示しています。
- ワークブックをインスタンス化してください。
- 空の条件付き書式を追加してください。
- 書式を適用する範囲を設定してください。
- 書式の条件を定義してください。
- ファイルを保存します。
この後に、フォント設定や罫線設定、パターン設定などの他の小さな例が続きます。
Microsoft Excel 2007 では、より高度な条件付き書式が追加され、それを Aspose.Cells もサポートしています。ここにある例では単純な書式設定を示していますが、Microsoft Excel 2007 の例ではより高度な条件付き書式の適用方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir + "Book1.xlsx"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Adds an empty conditional formatting | |
int index = sheet.ConditionalFormattings.Add(); | |
FormatConditionCollection fcs = sheet.ConditionalFormattings[index]; | |
// Sets the conditional format range. | |
CellArea ca = new CellArea(); | |
ca.StartRow = 0; | |
ca.EndRow = 0; | |
ca.StartColumn = 0; | |
ca.EndColumn = 0; | |
fcs.AddArea(ca); | |
ca = new CellArea(); | |
ca.StartRow = 1; | |
ca.EndRow = 1; | |
ca.StartColumn = 1; | |
ca.EndColumn = 1; | |
fcs.AddArea(ca); | |
// Adds condition. | |
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "=A2", "100"); | |
// Adds condition. | |
int conditionIndex2 = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100"); | |
// Sets the background color. | |
FormatCondition fc = fcs[conditionIndex]; | |
fc.Style.BackgroundColor = Color.Red; | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); |
フォントの設定
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int i = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[i]; | |
// Accessing the "A1" cell from the worksheet | |
Aspose.Cells.Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
Style style = cell.GetStyle(); | |
// Setting the font weight to bold | |
style.Font.IsBold = true; | |
// Applying the style to the cell | |
cell.SetStyle(style); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003); |
境界線の設定
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
public static void Run() | |
{ | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Adds an empty conditional formatting | |
int index = sheet.ConditionalFormattings.Add(); | |
FormatConditionCollection fcs = sheet.ConditionalFormattings[index]; | |
// Sets the conditional format range. | |
CellArea ca = new CellArea(); | |
ca.StartRow = 0; | |
ca.EndRow = 5; | |
ca.StartColumn = 0; | |
ca.EndColumn = 3; | |
fcs.AddArea(ca); | |
// Adds condition. | |
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100"); | |
// Sets the background color. | |
FormatCondition fc = fcs[conditionIndex]; | |
fc.Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Dashed; | |
fc.Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Dashed; | |
fc.Style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Dashed; | |
fc.Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Dashed; | |
fc.Style.Borders[BorderType.LeftBorder].Color = Color.FromArgb(0, 255, 255); | |
fc.Style.Borders[BorderType.RightBorder].Color = Color.FromArgb(0, 255, 255); | |
fc.Style.Borders[BorderType.TopBorder].Color = Color.FromArgb(0, 255, 255); | |
fc.Style.Borders[BorderType.BottomBorder].Color = Color.FromArgb(255, 255, 0); | |
workbook.Save(dataDir + "output.xlsx"); | |
} |
パターンの設定
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Adds an empty conditional formatting | |
int index = sheet.ConditionalFormattings.Add(); | |
FormatConditionCollection fcs = sheet.ConditionalFormattings[index]; | |
// Sets the conditional format range. | |
CellArea ca = new CellArea(); | |
ca.StartRow = 0; | |
ca.EndRow = 5; | |
ca.StartColumn = 0; | |
ca.EndColumn = 3; | |
fcs.AddArea(ca); | |
// Adds condition. | |
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100"); | |
FormatCondition fc = fcs[conditionIndex]; | |
fc.Style.Pattern = BackgroundType.ReverseDiagonalStripe; | |
fc.Style.ForegroundColor = Color.FromArgb(255, 255, 0); | |
fc.Style.BackgroundColor = Color.FromArgb(0, 255, 255); | |
workbook.Save(dataDir + "output.xlsx"); |