Formato de Filas y Columnas

Trabajar con filas

Cómo ajustar la altura de fila

Aspose.Cells para Python via .NET proporciona una clase, Workbook, que representa un archivo de Microsoft Excel. La clase Workbook contiene un WorksheetCollection 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 una colección Cells que representa todas las celdas en la hoja de cálculo.

La colección Cells proporciona varios métodos para gestionar filas o columnas en una hoja de cálculo. Algunos de estos se discuten a continuación con más detalle.

Cómo establecer la altura de una fila

Es posible establecer la altura de una sola fila llamando al método set_row_height de la colección Cells. El método set_row_height toma los siguientes parámetros como sigue:

  • fila, el índice de la fila a la que se le cambia la altura.
  • altura, la altura de la fila que se aplica en la fila.
from aspose.cells import Workbook
# 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(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the height of the second row to 13
worksheet.cells.set_row_height(1, 13)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Cómo establecer la altura de todas las filas en una hoja de cálculo

Si los desarrolladores necesitan establecer la misma altura de fila para todas las filas en la hoja de cálculo, pueden hacerlo usando la propiedad standard_height de la colección Cells.

Ejemplo:

from aspose.cells import Workbook
# 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(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the height of all rows in the worksheet to 15
worksheet.cells.standard_height = 15.0
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Trabajar con columnas

Cómo establecer el ancho de una columna

Establezca el ancho de una columna llamando al método set_column_width de la colección Cells. El método set_column_width toma los siguientes parámetros:

  • columna, el índice de la columna cuyo ancho estás cambiando.
  • ancho, el ancho de la columna deseado.
from aspose.cells import Workbook
# 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(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the width of the second column to 17.5
worksheet.cells.set_column_width(1, 17.5)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Cómo establecer el ancho de columna en píxeles

Establezca el ancho de una columna llamando al método set_column_width_pixel de la colección Cells. El método set_column_width_pixel toma los siguientes parámetros:

  • columna, el índice de la columna cuyo ancho estás cambiando.
  • píxeles, el ancho de columna deseado en píxeles.
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
outDir = RunExamples.Get_OutputDirectory()
# Load source Excel file
workbook = Workbook(sourceDir + "Book1.xlsx")
# Access first worksheet
worksheet = workbook.worksheets[0]
# Set the width of the column in pixels
worksheet.cells.set_column_width_pixel(7, 200)
workbook.save(outDir + "SetColumnWidthInPixels_Out.xlsx")

Cómo establecer el ancho de todas las columnas en una hoja de cálculo

Para establecer el mismo ancho de columna para todas las columnas en la hoja de cálculo, use la propiedad standard_width de la colección Cells.

from aspose.cells import Workbook
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)
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting the width of all columns in the worksheet to 20.5
worksheet.cells.standard_width = 20.5
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

Temas avanzados