设置边距

如何设置边距

Aspose.Cells for Python via .NET 提供一个类,Workbook,表示一个Excel文件。Workbook 类包含一个 worksheets 集合,可以访问Excel文件中的每个工作表。一个工作表由 Worksheet 类表示。

Worksheet类提供了page_setup属性,用于设置工作表的页面设置选项。page_setup属性是PageSetup类的一个对象,使开发人员能够为打印的工作表设置不同的页面布局选项。PageSetup类提供了用于设置页面设置选项的各种属性和方法。

如何设置页面边距

使用PageSetup类的成员设置页面边距(左、右、上、下)。下面列出了一些用于指定页面边距的方法:

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(".")
# Create a workbook object
workbook = Workbook()
# Get the worksheets in the workbook
worksheets = workbook.worksheets
# Get the first (default) worksheet
worksheet = worksheets[0]
# Get the pagesetup object
pageSetup = worksheet.page_setup
# Set bottom,left,right and top page margins
pageSetup.bottom_margin = 2.0
pageSetup.left_margin = 1.0
pageSetup.right_margin = 1.0
pageSetup.top_margin = 3.0
# Save the Workbook.
workbook.save(dataDir + "SetMargins_out.xls")

如何页面居中

可以在页面上水平和垂直居中某物。为此,PageSetup类的一些有用的成员是center_horizontallycenter_vertically

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(".")
# Create a workbook object
workbook = Workbook()
# Get the worksheets in the workbook
worksheets = workbook.worksheets
# Get the first (default) worksheet
worksheet = worksheets[0]
# Get the pagesetup object
pageSetup = worksheet.page_setup
# Specify Center on page Horizontally and Vertically
pageSetup.center_horizontally = True
pageSetup.center_vertically = True
# Save the Workbook.
workbook.save(dataDir + "CenterOnPage_out.xls")

如何设置页眉和页脚边距

使用PageSetup类的成员(如header_marginfooter_margin)设置页眉和页脚边距。

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(".")
# Create a workbook object
workbook = Workbook()
# Get the worksheets in the workbook
worksheets = workbook.worksheets
# Get the first (default) worksheet
worksheet = worksheets[0]
# Get the pagesetup object
pageSetup = worksheet.page_setup
# Specify Header / Footer margins
pageSetup.header_margin = 2.0
pageSetup.footer_margin = 2.0
# Save the Workbook.
workbook.save(dataDir + "CenterOnPage_out.xls")