Configuración de bordes

Añadiendo Bordes a las Celdas

Microsoft Excel permite a los usuarios formatear celdas agregando bordes. El tipo de borde depende de dónde se agrega. Por ejemplo, un borde superior es aquel agregado a la posición superior de una celda. Los usuarios también pueden modificar el estilo de línea y el color de los bordes.

Con Aspose.Cells para Python via .NET, los desarrolladores pueden agregar bordes y personalizarlos de la misma manera flexible que en Microsoft Excel.

Añadiendo Bordes a las Celdas

Aspose.Cells para Python via .NET proporciona una clase, Workbook que representa un archivo de Microsoft Excel. La clase Workbook contiene una colección worksheets que permite acceder a cada hoja de cálculo en el archivo de Excel. Una hoja de cálculo está representada por la clase Worksheet. La clase Worksheet proporciona la colección Cells. Cada elemento en la colección Cells representa un objeto de la clase Cell.

Aspose.Cells para Python via .NET proporciona el método get_style en la clase Cell. El método set_style se usa para establecer el estilo de formato de una celda. La clase Style proporciona propiedades para agregar bordes a las celdas.

Añadir bordes a una celda

Los desarrolladores pueden añadir bordes a una celda utilizando la colección borders del objeto Style. El tipo de borde se pasa como un índice a la colección borders. Todos los tipos de bordes están predefinidos en la enumeración BorderType.

Enumeración de Bordes

Tipos de Bordes Descripción
BORDER_INFERIOR Una línea de borde inferior
DIAGONAL_ABAJO Una línea diagonal de arriba a la izquierda a la derecha abajo
DIAGONAL_ARRIBA Una línea diagonal de abajo a la izquierda a la parte superior derecha
BORDER_IZQUIERDO Una línea de borde izquierda
BORDER_DERECHO Una línea de borde derecha
BORDER_SUPERIOR Una línea de borde superior

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.

Para establecer el color de la línea de un borde, seleccione un color usando la enumeración Color (parte del Framework .NET) y asígnele la propiedad Color del objeto Border.

El estilo de línea del borde se establece seleccionando un estilo de línea de la enumeración CellBorderType

Enumeración de Tipo de Bordes de Celda

Estilos de Línea Descripción
DASH_DOT Línea delgada con puntos y guiones
DASH_DOT_DOT Línea delgada con puntos, guiones y puntos
ESTAMPADO Línea a trazos
PUNTOS Línea punteada
DOBLE Línea doble
PELO Línea delgada
MEDIO_DASH_DOT Línea medio guion-dot
MEDIO_DASH_DOT_DOT Línea medio guion-dot-dotted
MEDIO_DASHED Línea medio guion
NINGUNO Sin línea
MEDIO Línea media
DASH_DOT_INCLINADO Línea medio guion-dot inclinada
GRUESO Línea gruesa
DELGADO Línea delgada
Selecciona uno de los estilos de línea y luego asígnalo a la propiedad line_style del objeto Border.
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")

Agregar bordes a un rango de celdas

También es posible agregar bordes a un rango de celdas en lugar de solo a una celda. Para hacerlo, primero crea un rango de celdas llamando al método create_range de la colección Cells. Toma los siguientes parámetros:

  • Primera fila, la primera fila del rango.
  • Primera columna, representa la primera columna del rango.
  • Número de filas, el número de filas en el rango.
  • Número de columnas, el número de columnas en el rango.

El método Range devuelve un objeto Range, que contiene el rango de celdas especificado. El objeto Range proporciona un método set_outline_border que toma los siguientes parámetros para agregar un borde al rango de celdas:

  • Tipo de borde, el tipo de borde, seleccionado de la enumeración BorderType .
  • Estilo de línea, el estilo de línea de borde, seleccionado de la enumeración CellBorderType .
  • Color, el color de línea, seleccionado de la enumeración 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")