用C++设置边距
设置页边距
Aspose.Cells 提供一个Workbook类,它代表一个Excel文件。 Workbook类包含Worksheets集合,可以访问Excel文件中的每个工作表。一个工作表由Worksheet类表示。
Worksheet类提供用于设置工作表页面设置选项的PageSetup属性。PageSetup属性是PageSetup类的对象,允许开发者为打印的工作表设置不同的页面布局选项。PageSetup类提供各种属性和方法,用于设置页面布局。
页面边距
使用PageSetup类成员设置页面边距(左、右、上、下)。以下列出一些用于指定页面边距的方法:
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a workbook object
Workbook workbook;
// Get the worksheets in the workbook
WorksheetCollection worksheets = workbook.GetWorksheets();
// Get the first (default) worksheet
Worksheet worksheet = worksheets.Get(0);
// Get the pagesetup object
PageSetup pageSetup = worksheet.GetPageSetup();
// Set bottom, left, right, and top page margins
pageSetup.SetBottomMargin(2);
pageSetup.SetLeftMargin(1);
pageSetup.SetRightMargin(1);
pageSetup.SetTopMargin(3);
// Save the Workbook
workbook.Save(outDir + u"SetMargins_out.xls");
std::cout << "Margins set successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
页面居中
可以在页面上水平和垂直居中某个内容。为此,有一些有用的 PageSetup 类成员,如 GetCenterHorizontally() 和 GetCenterVertically()。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a workbook object
Workbook workbook;
// Get the worksheets in the workbook
WorksheetCollection worksheets = workbook.GetWorksheets();
// Get the first (default) worksheet
Worksheet worksheet = worksheets.Get(0);
// Get the pagesetup object
PageSetup pageSetup = worksheet.GetPageSetup();
// Specify Center on page Horizontally and Vertically
pageSetup.SetCenterHorizontally(true);
pageSetup.SetCenterVertically(true);
// Save the Workbook
workbook.Save(outDir + u"CenterOnPage_out.xls");
std::cout << "Workbook saved successfully with centered page setup!" << std::endl;
Aspose::Cells::Cleanup();
}
页眉和页脚边距
使用 PageSetup 类成员(如 GetHeaderMargin() 和 GetFooterMargin())设置页眉和页脚边距。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a workbook object
Workbook workbook;
// Get the worksheets in the workbook
WorksheetCollection worksheets = workbook.GetWorksheets();
// Get the first (default) worksheet
Worksheet worksheet = worksheets.Get(0);
// Get the pagesetup object
PageSetup pageSetup = worksheet.GetPageSetup();
// Specify Header / Footer margins
pageSetup.SetHeaderMargin(2);
pageSetup.SetFooterMargin(2);
// Save the Workbook
workbook.Save(outDir + u"CenterOnPage_out.xls");
std::cout << "Workbook saved successfully with centered header and footer margins!" << std::endl;
Aspose::Cells::Cleanup();
}