Nascondere e mostrare righe e colonne
Controllo della visibilità di righe e colonne
Aspose.Cells per Python via .NET fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene un WorksheetCollection che consente ai developer di accedere a ciascun foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una collezione cells che rappresenta tutte le celle nel foglio di lavoro. La collezione cells fornisce diversi metodi per gestire righe o colonne in un foglio di lavoro. Alcuni di questi vengono discussi di seguito.
Come nascondere righe e colonne
Gli sviluppatori possono nascondere una riga o colonna chiamando rispettivamente i metodi hide_row e hide_column della collezione cells. Entrambi i metodi prendono l’indice della riga e della colonna come parametro per nascondere la riga o colonna specifica.
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] | |
# Hiding the 3rd row of the worksheet | |
worksheet.cells.hide_row(2) | |
# Hiding the 2nd column of the worksheet | |
worksheet.cells.hide_column(1) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
Come mostrare righe e colonne
Gli sviluppatori possono mostrare qualsiasi riga o colonna nascosta chiamando rispettivamente i metodi unhide_row e unhide_column della collezione cells. Entrambi i metodi prendono due parametri:
- Indice della riga o colonna - l’indice di una riga o colonna che viene utilizzato per mostrare la riga o colonna specifica.
- Altezza della riga o larghezza della colonna - l’altezza della riga o la larghezza della colonna assegnata alla riga o colonna dopo l’annullamento della visualizzazione.
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] | |
# Unhiding the 3rd row and setting its height to 13.5 | |
worksheet.cells.unhide_row(2, 13.5) | |
# Unhiding the 2nd column and setting its width to 8.5 | |
worksheet.cells.unhide_column(1, 8.5) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
Come nascondere righe e colonne multiple
Gli sviluppatori possono nascondere più righe o colonne contemporaneamente chiamando rispettivamente i metodi hide_rows e hide_columns della collezione cells. Entrambi i metodi prendono l’indice di partenza della riga o colonna e il numero di righe o colonne che devono essere nascoste come parametri.
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] | |
# Hiding 3,4 and 5 rows in the worksheet | |
worksheet.cells.hide_rows(2, 3) | |
# Hiding 2 and 3 columns in the worksheet | |
worksheet.cells.hide_columns(1, 2) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "outputxls") | |
# Closing the file stream to free all resources | |
fstream.close() |