设置范围边框

可能的使用场景

当您想要为范围设置边框时,您无需逐个设置每个单元格。 您可以设置范围的边框。 Aspose.Cells for Python via .NET 提供了这个功能。 本文提供了一个使用Aspose.Cells for Python via .NET来设置范围边框的样本代码。

如何在Excel中设置范围边框

要在Excel中设置范围的边框,可以按照以下步骤进行:

  1. 选择要应用边框的单元格范围。
  2. 在功能区“主页”选项卡中,找到“字体”组。
  3. 在“字体”组内,单击“边框”下拉按钮。
  4. 从下拉菜单中选择要应用的边框类型。您可以选择预设的边框样式或自定义您自己的边框。
  5. 选择所需的边框样式后,边框将应用于所选的单元格范围。

如何使用Aspose.Cells for Python Excel库设置范围边框

此示例演示如何:

  1. 创建一个工作簿。
  2. 在第一个工作表中的单元格中添加数据。
  3. 创建Range
  4. 设置范围内的边框。
  5. 设置范围外的边框。
from aspose.cells import BorderType, CellBorderType, Workbook
from aspose.pydrawing import Color
# Instantiating an Workbook object
workbook = Workbook()
# Obtaining the reference of the newly added worksheet
ws = workbook.worksheets[0]
cells = ws.cells
# Setting the value to the cells
cell = cells.get("A1")
cell.put_value("Fruit")
cell = cells.get("B1")
cell.put_value("Count")
cell = cells.get("C1")
cell.put_value("Price")
cell = cells.get("A2")
cell.put_value("Apple")
cell = cells.get("A3")
cell.put_value("Mango")
cell = cells.get("A4")
cell.put_value("Blackberry")
cell = cells.get("A5")
cell.put_value("Cherry")
cell = cells.get("B2")
cell.put_value(5)
cell = cells.get("B3")
cell.put_value(3)
cell = cells.get("B4")
cell.put_value(6)
cell = cells.get("B5")
cell.put_value(4)
cell = cells.get("C2")
cell.put_value(5)
cell = cells.get("C3")
cell.put_value(20)
cell = cells.get("C4")
cell.put_value(30)
cell = cells.get("C5")
cell.put_value(60)
# Create a range (A1:C5).
range = cells.create_range("A1", "C5")
# set inner borer of range
innerColor = workbook.create_cells_color()
innerColor.color = Color.red
range.set_inside_borders(BorderType.VERTICAL, CellBorderType.THIN, innerColor)
innerColor.color = Color.green
range.set_inside_borders(BorderType.HORIZONTAL, CellBorderType.THIN, innerColor)
# set outer borer of range
outerColor = workbook.create_cells_color()
outerColor.color = Color.blue
range.set_outline_borders(CellBorderType.THIN, outerColor)
workbook.save("out.xlsx")