罫線設定
セルにボーダーを追加する
Microsoft Excelでは、ユーザーが罫線を追加することでセルのフォーマットを指定できます。追加する罫線の種類は、追加される位置に依存します。たとえば、上部の罫線はセルの上部に追加される罫線です。Aspose.Cellsでは、開発者はMicrosoft Excelと同じ柔軟な方法で罫線を追加し、外見をカスタマイズできます。
Aspose.Cellsを使用すると、開発者はMicrosoft Excelと同様に柔軟に罫線を追加し、見た目をカスタマイズできます。
セルにボーダーを追加する
Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供しています。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetsコレクションが含まれています。ワークシートはWorksheetクラスで表されます。Worksheetクラスは、Cellsコレクションを提供しています。Cellsコレクション内の各アイテムは、Cellクラスのオブジェクトを表します。
Aspose.Cellsは、GetStyleクラスでCellメソッドを提供しています。SetStyleメソッドは、セルの書式設定スタイルを設定するために使用されます。Styleクラスは、セルに罫線を追加するプロパティを提供しています。
セルに罫線を追加
開発者は、StyleオブジェクトのBordersコレクションを使用してセルに罫線を追加できます。罫線のタイプは、Borders列挙型で事前に定義されています。
境界の列挙
境界タイプ | 説明 |
---|---|
BottomBorder | 下部の境界線 |
DiagonalDown | 左上から右下への対角線 |
DiagonalUp | 左下から右上への対角線 |
LeftBorder | 左側の境界線 |
RightBorder | 右側の境界線 |
TopBorder | 上部の境界線 |
The Borders collection stores all borders. Each border in the Borders collection is represented by a Border object which provides two properties, Color and LineStyle to set a border’s line color and style respectively.
境界線の線の色を設定するには、.NET Frameworkの一部であるColor列挙型を使用して色を選択し、それをBorderオブジェクトのColorプロパティに割り当てます。
境界線の線スタイルは、CellBorderType列挙体から線スタイルを選択して設定されます。
CellBorderType列挙体
線のスタイル | 説明 |
---|---|
DashDot | 細い点線のような線 |
DashDotDot | 細い破線点線のような線 |
Dashed | 破線のような線 |
Dotted | 点線のような線 |
Double | 二重線 |
Hair | 細い線 |
MediumDashDot | 中くらいの点線のような線 |
MediumDashDotDot | 中くらいの破線点線のような線 |
MediumDashed | 中くらいの破線のような線 |
None | 線なし |
Medium | 中くらいの線 |
SlantedDashDot | 対角の中くらいの点線のような線 |
Thick | 太い線 |
Thin | 細い線 |
線のスタイルを選択してから、Borderオブジェクトの LineStyle プロパティにそれを割り当てます。 |
// 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 | |
Aspose.Cells.Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Visit Aspose!"); | |
// Create a style object | |
Style style = cell.GetStyle(); | |
// Setting the line style of the top border | |
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the top border | |
style.Borders[BorderType.TopBorder].Color = Color.Black; | |
// Setting the line style of the bottom border | |
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the bottom border | |
style.Borders[BorderType.BottomBorder].Color = Color.Black; | |
// Setting the line style of the left border | |
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the left border | |
style.Borders[BorderType.LeftBorder].Color = Color.Black; | |
// Setting the line style of the right border | |
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thick; | |
// Setting the color of the right border | |
style.Borders[BorderType.RightBorder].Color = Color.Black; | |
// Apply the border styles to the cell | |
cell.SetStyle(style); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
セルの範囲に境界線を追加する
1つのセルだけでなく、セルの範囲にも境界線を追加することができます。そのためには、まずCellsコレクションのCreateRangeメソッドを呼び出して、セルの範囲を作成します。 このメソッドには、次のパラメータを渡します:
- 最初の行、範囲の最初の行。
- 最初の列、範囲の最初の列を表す。
- 行数、範囲内の行数。
- 列数、範囲内の列数。
CreateRange メソッドは、指定されたセルの範囲を含む Range オブジェクトを返し、Range オブジェクトは、次のパラメータを取る SetOutlineBorder メソッドを提供する。
- 境界線の種類、BorderType 列挙型から選択した境界線の種類。
- 線のスタイル、CellBorderType 列挙型から選択した境界線のスタイル。
- 色、Color 列挙型から選択した線の色。
// 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("Hello World From Aspose"); | |
// Creating a range of cells starting from "A1" cell to 3rd column in a row | |
Range range = worksheet.Cells.CreateRange(0, 0, 1, 3); | |
// Adding a thick top border with blue line | |
range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Thick, Color.Blue); | |
// Adding a thick bottom border with blue line | |
range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Thick, Color.Blue); | |
// Adding a thick left border with blue line | |
range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Blue); | |
// Adding a thick right border with blue line | |
range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Thick, Color.Blue); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |