Скрытие и отображение строк и столбцов
Управление видимостью строк и столбцов
Aspose.Cells для Python via .NET предоставляет класс Workbook, который представляет файл Microsoft Excel. Класс Workbook содержит WorksheetCollection, позволяющий разработчикам получить доступ к каждому листу Excel-файла. Лист представлен классом Worksheet. Класс Worksheet предоставляет коллекцию cells, представляющую все ячейки на листе. Коллекция cells предоставляет несколько методов для управления строками или столбцами на листе. Ниже приведено несколько из них.
Как скрыть строки и столбцы
Разработчики могут скрыть строку или столбец, вызвав методы hide_row и hide_column соответственно из коллекции cells. Оба метода принимают индекс строки и столбца в качестве параметра для скрытия конкретной строки или столбца.
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() |
Как показать строки и столбцы
Разработчики могут показать любую скрытую строку или столбец, вызвав методы unhide_row и unhide_column из коллекции cells. Оба метода принимают два параметра:
- Индекс строки или столбца - индекс строки или столбца, который используется для отображения конкретной строки или столбца.
- Высота строки или ширина столбца - высота строки или ширина столбца, назначенные строке или столбцу после отображения.
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() |
Как скрыть несколько строк и столбцов
Разработчики могут скрыть сразу несколько строк или столбцов, вызвав методы hide_rows и hide_columns из коллекции cells. Оба метода принимают начальный индекс строки или столбца и количество строк или столбцов, которые должны быть скрыты.
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() |