セルの書式設定
序章
GetStyle および SetStyle メソッドを使用して Cells をフォーマットします。
セルにさまざまな種類の書式設定スタイルを適用して、背景色または前景色、境界線、フォント、水平および垂直方向の配置、インデント レベル、テキストの方向、回転角度などを設定します。
GetStyle および SetStyle メソッドの使用
開発者が異なるセルに異なるフォーマット スタイルを適用する必要がある場合は、スタイルセルの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"); |
スタイル オブジェクトを使用して異なる Cells をフォーマットする
開発者が同じ書式設定スタイルを異なるセルに適用する必要がある場合は、次を使用できますスタイル物体。以下の手順に従ってご利用ください。スタイル物体:
- を追加スタイルを呼び出してオブジェクトをCreateStyleの方法ワークブッククラス
- 新しく追加されたスタイル物体
- の必要なプロパティ/属性を設定しますスタイル目的の書式設定を適用するオブジェクト
- 構成されたスタイル目的の細胞に異議を唱える
このアプローチにより、アプリケーションの効率が大幅に向上し、メモリも節約できます。
// 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"); |
Cell で選択した文字をフォーマットする
フォント設定の取り扱いでは、セル内のテキストを書式設定する方法について説明していますが、すべてのセル コンテンツを書式設定する方法についてのみ説明しています。選択した文字だけをフォーマットしたい場合はどうしますか?
Aspose.Cells もこの機能をサポートしています。このトピックでは、この機能を効果的に使用する方法について説明します。
選択した文字の書式設定
Aspose.Cells はクラスを提供し、ワークブックMicrosoft Excel ファイルを表します。のワークブッククラスにはワークシートExcel ファイル内の各ワークシートにアクセスできるコレクション。ワークシートは、ワークシートクラス。のワークシートクラスはCellsコレクション。の各項目Cellsコレクションはのオブジェクトを表しますCellクラス。
のCellクラスが提供するキャラクター次のパラメーターを使用してセル内の文字の範囲を選択するメソッド:
- 開始インデックス、選択が始まる文字のインデックス。
- 文字数、選択する文字数。
のキャラクターメソッドはのインスタンスを返しますフォント設定以下のコード例に示すように、開発者がセルと同じ方法で文字をフォーマットできるようにするクラス。出力ファイルの 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"); |
行と列のフォーマット
場合によっては、開発者が行または列に同じ書式を適用する必要があります。セルに 1 つずつ書式設定を適用すると時間がかかることが多く、適切な解決策ではありません。 この問題に対処するために、Aspose.Cells は、この記事で詳しく説明されている簡単で迅速な方法を提供します。
行と列のフォーマット
Aspose.Cells はクラスを提供します。ワークブックMicrosoft Excel ファイルを表します。のワークブッククラスにはワークシートExcel ファイル内の各ワークシートにアクセスできるコレクション。ワークシートは、ワークシートクラス。のワークシートクラスはCellsコレクション。のCellsコレクションは、行コレクション。
行のフォーマット
の各項目行コレクションは行物体。の行オブジェクトは、スタイルを適用行のフォーマット設定に使用されるメソッド。行に同じフォーマットを適用するには、スタイル物体。以下の手順は、その使用方法を示しています。
- を追加スタイルに異議を唱えるワークブッククラスを呼び出してCreateStyle方法。
- をセットするスタイルオブジェクトのプロパティを使用して書式設定を適用します。
- 関連する属性を ON にします。スタイルフラグ物体。
- 構成されたスタイルに異議を唱える行物体。
// 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コレクションも提供しますコラムコレクション。の各項目コラムコレクションは桁物体。に似ている行オブジェクト、桁オブジェクトも提供しますスタイルを適用列をフォーマットするメソッド。
// 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"); |