セルの書式設定
紹介
GetStyleおよびSetStyleメソッドを使用してセルの書式設定する方法
セルに異なる種類の書式設定スタイルを適用して、背景色や前景色、枠線、フォント、水平および垂直の配置、インデントレベル、テキストの方向、回転角などを設定する。
GetStyle および SetStyle メソッドの使用方法
開発者が異なるセルに異なる書式スタイルを適用する必要がある場合、セルの Style を取得し、Cell.GetStyle メソッドを使用してセルのスタイル属性を指定し、その後 Cell.SetStyle メソッドを使用して書式を適用することが好ましいです。以下に、セルに異なる書式を適用するこのアプローチをデモンストレーションするための例が示されています。
// 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(); | |
// Obtaining the reference of the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Hello Aspose!"); | |
// Defining a Style object | |
Aspose.Cells.Style style; | |
// Get the style from A1 cell | |
style = cell.GetStyle(); | |
// Setting the vertical alignment | |
style.VerticalAlignment = TextAlignmentType.Center; | |
// Setting the horizontal alignment | |
style.HorizontalAlignment = TextAlignmentType.Center; | |
// Setting the font color of the text | |
style.Font.Color = Color.Green; | |
// Setting to shrink according to the text contained in it | |
style.ShrinkToFit = true; | |
// Setting the bottom border color to red | |
style.Borders[BorderType.BottomBorder].Color = Color.Red; | |
// Setting the bottom border type to medium | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium; | |
// Applying the style to A1 cell | |
cell.SetStyle(style); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
異なるセルのフォーマットにスタイルオブジェクトを使用する方法
開発者が異なるセルに同じ書式スタイルを適用する必要がある場合、Style オブジェクトを使用できます。以下に、Style オブジェクトを使用する手順について説明します。
1.Workbook クラスの CreateStyle メソッドを呼び出して Style オブジェクトを追加します
このアプローチは、アプリケーションの効率を大幅に改善し、メモリも節約できます。
// 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 first worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[i]; | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Hello Aspose!"); | |
// Adding a new Style | |
Style style = workbook.CreateStyle(); | |
// Setting the vertical alignment of the text in the "A1" cell | |
style.VerticalAlignment = TextAlignmentType.Center; | |
// Setting the horizontal alignment of the text in the "A1" cell | |
style.HorizontalAlignment = TextAlignmentType.Center; | |
// Setting the font color of the text in the "A1" cell | |
style.Font.Color = Color.Green; | |
// Shrinking the text to fit in the cell | |
style.ShrinkToFit = true; | |
// Setting the bottom border color of the cell to red | |
style.Borders[BorderType.BottomBorder].Color = Color.Red; | |
// Setting the bottom border type of the cell to medium | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium; | |
// Assigning the Style object to the "A1" cell | |
cell.SetStyle(style); | |
// Apply the same style to some other cells | |
worksheet.Cells["B1"].SetStyle(style); | |
worksheet.Cells["C1"].SetStyle(style); | |
worksheet.Cells["D1"].SetStyle(style); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
Microsoft Excel 2007 の事前定義されたスタイルの使用方法
Microsoft Excel 2007 に異なる書式スタイルを適用する必要がある場合、Aspose.Cells API を使用してスタイルを適用します。以下に、セルに事前定義されたスタイルを適用するこのアプローチをデモンストレーションするための例が示されています。
// 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); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Create a style object . | |
Style style = workbook.CreateStyle(); | |
// Input a value to A1 cell. | |
workbook.Worksheets[0].Cells["A1"].PutValue("Test"); | |
// Apply the style to the cell. | |
workbook.Worksheets[0].Cells["A1"].SetStyle(style); | |
// Save the Excel 2007 file. | |
workbook.Save(dataDir + "book1.out.xlsx"); |
セル内の選択された文字の書式設定方法
フォント設定の取り扱いは、セル内のテキストの書式設定方法について説明していますが、セルの内容全体の書式設定方法のみを説明しています。しかし、選択した文字のみを書式設定する場合はどうすればよいでしょうか?
Aspose.Cells はこの機能もサポートしています。このトピックでは、この機能を効果的に使用する方法について説明します。
選択された文字の書式設定方法
Aspose.Cells は、Microsoft Excel ファイルを表す Workbook クラスを提供します。Workbook クラスには、Excel ファイル内の各ワークシートにアクセス可能にする Worksheets コレクションが含まれています。ワークシートは Worksheet クラスで表されます。Worksheet クラスは Cells コレクションを提供します。Cells コレクションの各アイテムは Cell クラスのオブジェクトを表します。
Cell クラスは、セル内の文字の範囲を選択するための以下のパラメータを取る Characters メソッドを提供します
- 開始インデックス、選択を開始する文字のインデックス。
- 文字数、選択する文字数。
Characters メソッドは、FontSetting クラスのインスタンスを返します。このインスタンスを使用して、セルのように文字を書式設定することができます。出力ファイルでは、A1 セルにおいて、‘Visit’ という単語はデフォルトフォントで、‘Aspose!’ は太字および青色で書式設定されます。
// 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(); | |
// Obtaining the reference of the first(default) worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Visit Aspose!"); | |
// Setting the font of selected characters to bold | |
cell.Characters(6, 7).Font.IsBold = true; | |
// Setting the font color of selected characters to blue | |
cell.Characters(6, 7).Font.Color = Color.Blue; | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
行および列の書式設定方法
時には、開発者は行または列に同じ書式を適用する必要があります。セルごとに書式を適用することは時間がかかるため、良い解決策ではありません。 この問題に対処するために、Aspose.Cells がこの記事で詳しく説明しているシンプルで高速な方法を提供します。
行および列の書式設定
Aspose.Cells は、Microsoft Excel ファイルを表す Workbook クラスを提供します。Workbook クラスには、Excel ファイル内の各ワークシートにアクセス可能にする Worksheets コレクションが含まれています。ワークシートは Worksheet クラスで表されます。Worksheet クラスは Cells コレクションを提供します。Cells コレクションは Rows コレクションを提供します。
行の書式設定方法
Rows コレクションの各アイテムは Row オブジェクトを表します。Row オブジェクトには行の書式を設定するために使用される ApplyStyle メソッドが提供されています。行に同じ書式を適用するには、Style オブジェクトを使用します。以下の手順はその使用方法を示しています。
- Workbook クラスに CreateStyle メソッドを呼び出して Style オブジェクトを追加します
- 書式設定の設定を適用するために Style オブジェクトのプロパティを設定します。
- StyleFlagオブジェクトの関連する属性をONにします。
- 設定されたStyleオブジェクトをRowオブジェクトに割り当てます。
// 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(); | |
// Obtaining the reference of the first (default) worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding a new Style to the styles | |
Style style = workbook.CreateStyle(); | |
// Setting the vertical alignment of the text in the "A1" cell | |
style.VerticalAlignment = TextAlignmentType.Center; | |
// Setting the horizontal alignment of the text in the "A1" cell | |
style.HorizontalAlignment = TextAlignmentType.Center; | |
// Setting the font color of the text in the "A1" cell | |
style.Font.Color = Color.Green; | |
// Shrinking the text to fit in the cell | |
style.ShrinkToFit = true; | |
// Setting the bottom border color of the cell to red | |
style.Borders[BorderType.BottomBorder].Color = Color.Red; | |
// Setting the bottom border type of the cell to medium | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium; | |
// Creating StyleFlag | |
StyleFlag styleFlag = new StyleFlag(); | |
styleFlag.HorizontalAlignment = true; | |
styleFlag.VerticalAlignment = true; | |
styleFlag.ShrinkToFit = true; | |
styleFlag.Borders = true; | |
styleFlag.FontColor = true; | |
// Accessing a row from the Rows collection | |
Row row = worksheet.Cells.Rows[0]; | |
// Assigning the Style object to the Style property of the row | |
row.ApplyStyle(style, styleFlag); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
列のフォーマット方法
CellsコレクションはColumnsコレクションも提供します。Columnsコレクション内の各アイテムはColumnオブジェクトを表します。Rowオブジェクトと同様に、Columnオブジェクトも列のフォーマットに関するApplyStyleメソッドを提供します。
// 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(); | |
// Obtaining the reference of the first (default) worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding a new Style to the styles | |
Style style = workbook.CreateStyle(); | |
// Setting the vertical alignment of the text in the "A1" cell | |
style.VerticalAlignment = TextAlignmentType.Center; | |
// Setting the horizontal alignment of the text in the "A1" cell | |
style.HorizontalAlignment = TextAlignmentType.Center; | |
// Setting the font color of the text in the "A1" cell | |
style.Font.Color = Color.Green; | |
// Shrinking the text to fit in the cell | |
style.ShrinkToFit = true; | |
// Setting the bottom border color of the cell to red | |
style.Borders[BorderType.BottomBorder].Color = Color.Red; | |
// Setting the bottom border type of the cell to medium | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Medium; | |
// Creating StyleFlag | |
StyleFlag styleFlag = new StyleFlag(); | |
styleFlag.HorizontalAlignment = true; | |
styleFlag.VerticalAlignment = true; | |
styleFlag.ShrinkToFit = true; | |
styleFlag.Borders = true; | |
styleFlag.FontColor = true; | |
// Accessing a column from the Columns collection | |
Column column = worksheet.Cells.Columns[0]; | |
// Applying the style to the column | |
column.ApplyStyle(style, styleFlag); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |