Gestire i fogli di lavoro dei file di Microsoft Excel.

Aspose.Cells fornisce una classe, Workbook, che rappresenta un file Excel. La classe Workbook contiene una raccolta worksheets che consente di accedere a ciascun foglio di lavoro nel file Excel.

Un foglio di calcolo è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una vasta gamma di proprietà e metodi per gestire i fogli di lavoro.

Come aggiungere fogli di lavoro a un nuovo file Excel

Per creare un nuovo file Excel in modo programmatico:

  1. Creare un oggetto della classe Workbook.
  2. Chiamare il metodo add della classe WorksheetCollection. Viene automaticamente aggiunto un foglio di lavoro vuoto al file Excel. Può essere referenziato passando l’indice del foglio del nuovo foglio alla raccolta worksheets.
  3. Ottenere un riferimento al foglio di lavoro.
  4. Lavorare sui fogli di lavoro.
  5. Salvare il nuovo file Excel con nuovi fogli di lavoro chiamando il metodo save della classe Workbook.
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)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Setting the name of the newly added worksheet
worksheet.name = "My Worksheet"
# Saving the Excel file
workbook.save(dataDir + "output.out.xls")

Come aggiungere fogli di lavoro a un foglio di calcolo progettato

Il processo di aggiunta di fogli di lavoro a un foglio di calcolo del designer è lo stesso di quello dell’aggiunta di un nuovo foglio di lavoro, tranne che il file Excel esiste già quindi dovrebbe essere aperto prima che i fogli di lavoro vengano aggiunti. Un foglio di calcolo del designer può essere aperto dalla classe Workbook.

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(".")
InputPath = dataDir + "book1.xlsx"
# Creating a file stream containing the Excel file to be opened
fstream = open(InputPath, "rb")
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Adding a new worksheet to the Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Setting the name of the newly added worksheet
worksheet.name = "My Worksheet"
# Saving the Excel file
workbook.save(dataDir + "output.xlsx")

Come accedere ai fogli di lavoro usando il nome del foglio

Accedi a qualsiasi foglio di lavoro specificando il suo nome o indice.

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(".")
InputPath = dataDir + "book1.xlsx"
# Creating a file stream containing the Excel file to be opened
fstream = open(InputPath, "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing a worksheet using its sheet name
worksheet = workbook.worksheets.get("Sheet1")
cell = worksheet.cells.get("A1")
print(cell.value)

Come rimuovere i fogli di lavoro usando il nome del foglio

Per rimuovere fogli di lavoro da un file, chiama il metodo remove_by_name della classe WorksheetCollection. Passa il nome del foglio al metodo remove_by_name per rimuovere un foglio di lavoro specifico.

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)
# Removing a worksheet using its sheet name
workbook.worksheets.remove_by_name("Sheet1")
# Save workbook
workbook.save(dataDir + "output.out.xls")

Come rimuovere i fogli di lavoro usando l’indice del foglio

La rimozione dei fogli di lavoro per nome funziona bene quando si conosce il nome del foglio di lavoro. Se non si conosce il nome del foglio di lavoro, utilizzare il metodo remove_by_index che prende l’indice del foglio di lavoro invece del suo nome.

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)
# Removing a worksheet using its sheet index
workbook.worksheets.remove_by_index(0)
# Save workbook
workbook.save(dataDir + "output.out.xls")

Come attivare i fogli e rendere attiva una cella nel foglio di lavoro

A volte è necessario che un foglio di lavoro specifico sia attivo e visualizzato quando un utente apre un file Microsoft Excel in Excel. Allo stesso modo, potresti voler attivare una cella specifica e impostare le barre di scorrimento per mostrare la cella attiva. Aspose.Cells è in grado di eseguire tutti questi compiti.

Un foglio attivo è un foglio su cui stai lavorando: il nome del foglio attivo sulla scheda è in grassetto per impostazione predefinita.

Una cella attiva è una cella selezionata, la cella in cui i dati vengono inseriti quando si inizia a digitare. Solo una cella è attiva alla volta. La cella attiva è evidenziata da un bordo spesso.

Come attivare i fogli e rendere attiva una cella

Aspose.Cells fornisce chiamate API specifiche per attivare un foglio e una cella. Ad esempio, la proprietà Aspose.Cells.WorksheetCollection.active_sheet_index è utile per impostare il foglio attivo in un foglio di lavoro. Analogamente, la proprietà Aspose.Cells.Worksheet.active_cell è utilizzata per impostare e ottenere una cella attiva nel foglio di lavoro.

Per assicurarti che le barre di scorrimento orizzontali o verticali siano nella posizione dell’indice di riga e colonna che vuoi mostrare dati specifici, utilizza le proprietà Aspose.Cells.Worksheet.first_visible_row e Aspose.Cells.Worksheet.first_visible_column.

L’esempio seguente mostra come attivare un foglio di lavoro e rendere una cella attiva in esso. Nell’output generato, le barre di scorrimento verranno scorrere per fare in modo che la seconda riga e la seconda colonna siano la loro prima riga e colonna visibile.

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(".")
# Instantiate a new Workbook.
workbook = Workbook()
# Get the first worksheet in the workbook.
worksheet1 = workbook.worksheets[0]
# Get the cells in the worksheet.
cells = worksheet1.cells
# Input data into B2 cell.
cells.get(1, 1).put_value("Hello World!")
# Set the first sheet as an active sheet.
workbook.worksheets.active_sheet_index = 0
# Set B2 cell as an active cell in the worksheet.
worksheet1.active_cell = "B2"
# Set the B column as the first visible column in the worksheet.
worksheet1.first_visible_column = 1
# Set the 2nd row as the first visible row in the worksheet.
worksheet1.first_visible_row = 1
# Save the excel file.
workbook.save(dataDir + "output.xls")