Insertar y Eliminar Filas y Columnas de un archivo de Excel
Introducción
Ya sea creando una nueva hoja de cálculo desde cero o trabajando en una hoja de cálculo existente, puede ser necesario agregar filas o columnas adicionales para acomodar más datos. Inversamente, también puede ser necesario eliminar filas o columnas de posiciones específicas en la hoja de cálculo. Para cumplir con estos requisitos, Aspose.Cells para Python via .NET proporciona un conjunto muy simple de clases y métodos, discutidos a continuación.
Gestionar Filas y Columnas
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 un 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 y columnas en una hoja de cálculo. Algunos de estos se discuten a continuación.
Insertar Filas y Columnas
Cómo insertar una fila
Inserte una fila en la hoja de cálculo en cualquier ubicación llamando al método insert_row de la colección cells. El método insert_row toma el índice de la fila donde se insertará la nueva 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] | |
# Inserting a row into the worksheet at 3rd position | |
worksheet.cells.insert_row(2) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
Cómo insertar múltiples filas
Para insertar múltiples filas en una hoja de cálculo, llame al método insert_rows de la colección cells. El método insert_rows toma dos parámetros:
- Índice de la fila, el índice de la fila desde donde se insertarán las nuevas filas.
- Número de filas, el número total de filas que se deben insertar.
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] | |
# Inserting 10 rows into the worksheet starting from 3rd row | |
worksheet.cells.insert_rows(2, 10) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
Cómo insertar una fila con formato
Para insertar una fila con opciones de formato, use la sobrecarga insert_rows que toma InsertOptions como parámetro. Establezca la propiedad copy_format_type de la clase InsertOptions con la enumeración CopyFormatType. La enumeración CopyFormatType tiene tres miembros que se enumeran a continuación.
- MISMA_QUE_LA_ANTERIOR: Formatea la fila igual que la fila anterior.
- MISMA_QUE_LA_SIGUIENTE: Formatea la fila igual que la fila siguiente.
- CLARO: Borra el formato.
from aspose.cells import CopyFormatType, InsertOptions, 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 Formatting options | |
insertOptions = InsertOptions() | |
insertOptions.copy_format_type = CopyFormatType.SAME_AS_ABOVE | |
# Inserting a row into the worksheet at 3rd position | |
worksheet.cells.insert_rows(2, 1, insertOptions) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "InsertingARowWithFormatting.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
Cómo Insertar una Columna
Los desarrolladores también pueden insertar una columna en la hoja de cálculo en cualquier ubicación llamando al método insert_column de la colección cells. El método insert_column toma el índice de la columna donde se insertará la nueva columna.
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] | |
# Inserting a column into the worksheet at 2nd position | |
worksheet.cells.insert_column(1) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
Eliminar Filas y Columnas
Cómo borrar múltiples filas
Para eliminar múltiples filas de una hoja de cálculo, llama al método delete_rows de la colección cells. El método delete_rows toma dos parámetros:
- Índice de fila, el índice de la fila desde donde se eliminarán las filas.
- Número de filas, el número total de filas que deben ser eliminadas.
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.xlsx", "wb") | |
# 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] | |
# Deleting 10 rows from the worksheet starting from 3rd row | |
worksheet.cells.delete_rows(2, 10) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
Cómo eliminar una columna
Para eliminar una columna de la hoja de cálculo en cualquier ubicación, llama al método delete_column de la colección Cells. El método delete_column toma el índice de la columna a eliminar.
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.xlsx", "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] | |
# Deleting a column from the worksheet at 5th position | |
worksheet.cells.delete_column(4) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |