Inserimento ed eliminazione di righe e colonne del file Excel

Introduzione

Che si stia creando un nuovo foglio di lavoro da zero o si stia lavorando su un foglio di lavoro esistente, potremmo dover aggiungere righe o colonne aggiuntive per ospitare più dati. Al contrario, potremmo anche dover eliminare righe o colonne da posizioni specifiche nel foglio di lavoro. Per soddisfare queste esigenze, Aspose.Cells per Python via .NET fornisce un insieme molto semplice di classi e metodi, discussi di seguito.

Gestire righe e colonne

Aspose.Cells per Python via .NET fornisce una classe Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene una raccolta worksheets che consente l’accesso a ciascun foglio di lavoro in un file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta cells che rappresenta tutte le celle nel foglio di lavoro.

La raccolta cells fornisce diversi metodi per gestire righe e colonne in un foglio di lavoro. Alcuni di questi sono discussi di seguito.

Inserire righe e colonne

Come inserire una riga

Inserire una riga nel foglio di lavoro in qualsiasi posizione chiamando il metodo insert_row della raccolta cells. Il metodo insert_row richiede l’indice della riga in cui verrà inserita la nuova riga.

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()

Come inserire più righe

Per inserire più righe in un foglio di lavoro, chiamare il metodo insert_rows della collezione cells. Il metodo insert_rows richiede due parametri:

  • Indice di riga, l’indice della riga da cui saranno inserite le nuove righe.
  • Numero di righe, il numero totale di righe da inserire.
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()

Come inserire una riga con formattazione

Per inserire una riga con opzioni di formattazione, utilizzare il sovraccarico insert_rows che richiede InsertOptions come parametro. Impostare la proprietà copy_format_type della classe InsertOptions con l’enumerazione CopyFormatType. L’enumerazione CopyFormatType ha tre membri come indicato di seguito.

  • UGUALE_A_QUOTA_SOPRA: Formatta la riga come la riga sopra.
  • UGUALE_A_QUOTA_SOTTO: Formatta la riga come la riga sotto.
  • CANCELLA: Cancella la formattazione.
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()

Come inserire una colonna

Gli sviluppatori possono anche inserire una colonna nel foglio di lavoro in qualsiasi posizione chiamando il metodo insert_column della collezione cells. Il metodo insert_column richiede l’indice della colonna in cui verrà inserita la nuova colonna.

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()

Eliminare righe e colonne

Come eliminare più righe

Per eliminare più righe da un foglio di lavoro, chiamare il metodo delete_rows della collezione cells. Il metodo delete_rows richiede due parametri:

  • Indice riga, l’indice della riga da cui partiranno le eliminazioni.
  • Numero di righe, il numero totale di righe da eliminare.
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()

Come eliminare una colonna

Per eliminare una colonna dal foglio di lavoro in qualsiasi posizione, chiamare il metodo delete_column della collezione Cells. Il metodo delete_column richiede l’indice della colonna da eliminare.

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()