Форматирование строк и столбцов
Работа со строками
Как настроить высоту строки
Aspose.Cells для Python via .NET предоставляет класс Workbook, который представляет собой файл Microsoft Excel. Класс Workbook содержит WorksheetCollection, который позволяет получить доступ к каждому листу в файле Excel. Лист представлен классом Worksheet. Класс Worksheet предоставляет коллекцию Cells, которая представляет все ячейки в листе.
Коллекция Cells предоставляет несколько методов для управления строками или столбцами на листе. Некоторые из них обсуждаются ниже более подробно.
Как установить высоту строки
Можно установить высоту отдельной строки, вызвав метод set_row_height коллекции Cells. Метод set_row_height принимает следующие параметры:
- row, индекс строки, высоту которой вы хотите изменить.
- height, высота строки для применения на строку.
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] | |
# Setting the height of the second row to 13 | |
worksheet.cells.set_row_height(1, 13) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
Как установить высоту всех строк на листе
Если разработчикам необходимо установить одинаковую высоту всех строк на листе, они могут сделать это, используя свойство standard_height коллекции 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] | |
# Setting the height of all rows in the worksheet to 15 | |
worksheet.cells.standard_height = 15.0 | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
Работа с колонками
Как установить ширину столбца
Установите ширину столбца, вызвав метод коллекции set_column_width Cells. Метод set_column_width принимает следующие параметры:
- столбец, индекс столбца, ширину которого вы изменяете.
- ширина, желаемая ширина столбца.
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] | |
# Setting the width of the second column to 17.5 | |
worksheet.cells.set_column_width(1, 17.5) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
Как установить ширину столбца в пикселях
Установите ширину столбца, вызвав метод коллекции set_column_width_pixel Cells. Метод set_column_width_pixel принимает следующие параметры:
- столбец, индекс столбца, ширину которого вы изменяете.
- пиксели, желаемая ширина столбца в пикселях.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Source directory | |
sourceDir = RunExamples.Get_SourceDirectory() | |
outDir = RunExamples.Get_OutputDirectory() | |
# Load source Excel file | |
workbook = Workbook(sourceDir + "Book1.xlsx") | |
# Access first worksheet | |
worksheet = workbook.worksheets[0] | |
# Set the width of the column in pixels | |
worksheet.cells.set_column_width_pixel(7, 200) | |
workbook.save(outDir + "SetColumnWidthInPixels_Out.xlsx") |
Как установить ширину всех столбцов в листе Excel
Чтобы установить одинаковую ширину столбца для всех столбцов в листе, используйте свойство standard_width коллекции Cells.
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) | |
# 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 the width of all columns in the worksheet to 20.5 | |
worksheet.cells.standard_width = 20.5 | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.out.xls") | |
# Closing the file stream to free all resources | |
fstream.close() |
Продвинутые темы
- Автоподгонка строк и столбцов
- Преобразование текста в столбцы с использованием Aspose.Cells
- Копирование строк и столбцов
- Удаление пустых строк и столбцов в листе Excel
- Группировка и разгруппировка строк и столбцов
- Скрытие и отображение строк и столбцов
- Вставка или удаление строк в листе Excel
- Вставка и удаление строк и столбцов в файле Excel
- Удалить дублирующиеся строки в рабочем листе
- Обновление ссылок в других листах при удалении пустых столбцов и строк на листе