罫線設定

セルにボーダーを追加する

Microsoft Excelでは、ユーザーが罫線を追加することでセルのフォーマットを指定できます。追加する罫線の種類は、追加される位置に依存します。たとえば、上部の罫線はセルの上部に追加される罫線です。Aspose.Cellsでは、開発者はMicrosoft Excelと同じ柔軟な方法で罫線を追加し、外見をカスタマイズできます。

Aspose.Cells for Python via .NETを使用すると、開発者はExcelと同じ柔軟な方法で枠線を追加およびカスタマイズできます。

セルにボーダーを追加する

Aspose.Cells for Python via .NETは、Workbookクラスを提供します。このクラスはMicrosoft Excelファイルを表します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるworksheetsコレクションが含まれています。ワークシートはWorksheetクラスで表されます。WorksheetクラスはCellsコレクションを提供します。Cellsコレクション内の各アイテムはCellクラスのオブジェクトを表します。

Aspose.Cells for Python via .NETは、get_styleメソッドをCellクラスに提供します。このset_styleメソッドはセルの書式スタイルを設定するために使用されます。Styleクラスはセルに枠線を追加するためのプロパティを提供します。

セルに罫線を追加

開発者は、Styleオブジェクトのbordersコレクションを使用してセルに罫線を追加できます。罫線のタイプは、borders列挙型で事前に定義されています。

境界の列挙

境界タイプ 説明
BOTTOM_BORDER 下境界線
DIAGONAL_DOWN 左上から右下へ向かう斜め線
DIAGONAL_UP 左下から右上へ向かう斜め線
LEFT_BORDER 左側の境界線
RIGHT_BORDER 右側の境界線
TOP_BORDER 上の境界線

The borders collection stores all borders. Each border in the Borders collection is represented by a Border object which provides two properties, color and line_style to set a border’s line color and style respectively.

境界線の線の色を設定するには、.NET Frameworkの一部であるColor列挙型を使用して色を選択し、それをBorderオブジェクトのColorプロパティに割り当てます。

境界線の線スタイルは、CellBorderType列挙体から線スタイルを選択して設定されます。

CellBorderType列挙体

線のスタイル 説明
DASH_DOT 細い点線の線
DASH_DOT_DOT 細い点線の二点線
DASHED 破線
DOTTED 点線
DOUBLE 二重線
HAIR 細線
MEDIUM_DASH_DOT 中間の破線点線
MEDIUM_DASH_DOT_DOT 中間の破点線
MEDIUM_DASHED 中くらいの破線
NONE 線無し
MEDIUM 中程度の線
SLANTED_DASH_DOT 斜め中点破線付き線
THICK 太い線
THIN 細い線
線のスタイルを選択してから、Borderオブジェクトの line_style プロパティにそれを割り当てます。
from aspose.cells import BorderType, CellBorderType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first (default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Visit Aspose!")
# Create a style object
style = cell.get_style()
# Setting the line style of the top border
style.borders.get(BorderType.TOP_BORDER).line_style = CellBorderType.THICK
# Setting the color of the top border
style.borders.get(BorderType.TOP_BORDER).color = Color.black
# Setting the line style of the bottom border
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.THICK
# Setting the color of the bottom border
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.black
# Setting the line style of the left border
style.borders.get(BorderType.LEFT_BORDER).line_style = CellBorderType.THICK
# Setting the color of the left border
style.borders.get(BorderType.LEFT_BORDER).color = Color.black
# Setting the line style of the right border
style.borders.get(BorderType.RIGHT_BORDER).line_style = CellBorderType.THICK
# Setting the color of the right border
style.borders.get(BorderType.RIGHT_BORDER).color = Color.black
# Apply the border styles to the cell
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

セルの範囲に境界線を追加する

1つのセルだけでなく、セルの範囲にも境界線を追加することができます。そのためには、まずCellsコレクションのcreate_rangeメソッドを呼び出して、セルの範囲を作成します。 このメソッドには、次のパラメータを渡します:

  • 最初の行、範囲の最初の行。
  • 最初の列、範囲の最初の列を表す。
  • 行数、範囲内の行数。
  • 列数、範囲内の列数。

create_range メソッドは、指定されたセルの範囲を含む Range オブジェクトを返し、Range オブジェクトは、次のパラメータを取る set_outline_border メソッドを提供する。

  • 境界線の種類BorderType 列挙型から選択した境界線の種類。
  • 線のスタイルCellBorderType 列挙型から選択した境界線のスタイル。
  • 、Color 列挙型から選択した線の色。
from aspose.cells import BorderType, CellBorderType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first (default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello World From Aspose")
# Creating a range of cells starting from "A1" cell to 3rd column in a row
range = worksheet.cells.create_range(0, 0, 1, 3)
# Adding a thick top border with blue line
range.set_outline_border(BorderType.TOP_BORDER, CellBorderType.THICK, Color.blue)
# Adding a thick bottom border with blue line
range.set_outline_border(BorderType.BOTTOM_BORDER, CellBorderType.THICK, Color.blue)
# Adding a thick left border with blue line
range.set_outline_border(BorderType.LEFT_BORDER, CellBorderType.THICK, Color.blue)
# Adding a thick right border with blue line
range.set_outline_border(BorderType.RIGHT_BORDER, CellBorderType.THICK, Color.blue)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")