格式化行和列
处理行
如何调整行高
Aspose.Cells for Python via .NET 提供一个代表 Microsoft Excel 文件的类,Workbook 类包含一个 Workbook,该类允许访问 Excel 文件中的每个工作表。工作表由 WorksheetCollection 类表示。Worksheet 类提供了 Worksheet 集合,该集合表示工作表中的所有单元格。
Cells 集合提供了几种方法来管理工作表中的行或列。以下将更详细讨论其中的一些。
如何设置行的高度
可以通过调用 Cells 集合的 set_row_height 方法来设置单行的高度。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() |
使用列
如何设置列的宽度
通过调用Cells集合的set_column_width方法设置列的宽度。set_column_width方法接受以下参数:
- 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() |
如何以像素设置列宽
通过调用Cells集合的set_column_width_pixel方法设置列的宽度。set_column_width_pixel方法接受以下参数:
- column,要更改宽度的列的索引。
- pixels,以像素表示的所需列宽。
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") |
如何设置工作表中所有列的宽度
要为工作表中所有列设置相同的列宽,请使用Cells集合的standard_width属性。
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() |