セルの範囲の書式設定
Contents
[
Hide
]
このトピックも、セルにフォント設定やその他の書式設定を適用するトピックシリーズに属しています。最後のトピックでは、このような機能について詳しく説明しています。たとえば、セルのフォントと色の変更 および セルにスタイルを適用する トピックを参照して、同じ機能について学ぶことができます。 すでにこれらの概念をカバーしているなら、このトピックではどのような新しいものがあるのでしょうか。 このトピックが前のものと異なる唯一の点は、単一のセルではなく、セルの範囲に書式設定(フォントやその他のスタイルに関するもの)を適用することです。 このトピックがあなたにとって有用であることを願っています。
セルのフォントとスタイルの設定
書式設定について話す前に(以前のトピックですでにたくさん話している)、セルの範囲を作成する方法について知っておく必要があります。 さて、CellRange クラスを使用してセルの範囲を作成できます。このクラスのコンストラクタは、セルの範囲を指定するためのいくつかのパラメータを取ります。 開始セルおよび終了セルの名前または行および列のインデックスを使用してセルの範囲を指定できます。
CellRange オブジェクトを作成したら、Worksheet のSetStyle、SetFont、SetFontColor メソッドのオーバーロードされたバージョンを使用して、指定したセルの範囲に書式設定を適用できます。
この基本的な概念を理解するための例を見てみましょう。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Setting sample values | |
GridCell cell = sheet.Cells["b7"]; | |
cell.SetCellValue("1"); | |
cell = sheet.Cells["c7"]; | |
cell.SetCellValue("2"); | |
cell = sheet.Cells["d7"]; | |
cell.SetCellValue("3"); | |
cell = sheet.Cells["e7"]; | |
cell.SetCellValue("4"); | |
// Creating a CellRange object starting from "B7" to "E7" | |
CellRange range = new CellRange(6, 1, 6, 4); | |
// Accessing and setting Style attributes | |
Style style = new Style(this.gridDesktop1); | |
style.Color = Color.Yellow; | |
// Applying Style object on the range of cells | |
sheet.SetStyle(range, style); | |
// Creating a customized Font object | |
Font font = new Font("Courier New", 12f); | |
// Setting the font of range of cells to the customized Font object | |
sheet.SetFont(range, font); | |
// Setting the font color of range of cells to Red | |
sheet.SetFontColor(range, Color.Red); |