设置页眉和页脚

设置页眉和页脚

Aspose.Cells 允许您在运行时向工作表添加页眉和页脚,但我们建议在预先设计的打印文件中手动设置页眉和页脚。您可以使用 Microsoft Excel 作为 GUI 工具来设置页眉和页脚,以节省工作量和开发时间。 Aspose.Cells 可以导入文件并保存设置。

要在运行时添加页眉和页脚,Aspose.Cells 提供特殊的 API 调用和脚本命令来格式化页眉和页脚。

脚本命令

脚本命令是允许您设置页眉和页脚格式的特殊命令。

脚本命令 描述
&P 当前页码
&G 照片
&N 总页数
&D 当前日期
&T 当前时间
&一种 工作表名称
&F 没有路径的文件名
&"<FontName>" 表示字体名称。例如:&“宋体”
&"<FontName>, <FontStyle>" 表示带有样式的字体名称。例如:&“Arial,Bold”
&<FontSize> 表示字体大小。例如:“&14abc”。但是,如果此命令后跟要在标题中打印的普通数字,则应使用空格字符将其与字体大小分隔开。例如:“&14 123”。

设置页眉和页脚

页面设置类提供了两个方法,设置标题设置页脚用于向工作表添加页眉和页脚。这些方法只有两个参数:

  • 部分 – 应该放置页眉或页脚的部分。分为左、中、右三部分,分别用0、1、2表示。
  • 脚本– 用于页眉或页脚的脚本。此脚本包含用于格式化页眉或页脚的脚本命令。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook excel = new Workbook();
// Obtaining the reference of the PageSetup of the worksheet
PageSetup pageSetup = excel.Worksheets[0].PageSetup;
// Setting worksheet name at the left section of the header
pageSetup.SetHeader(0, "&A");
// Setting current date and current time at the centeral section of the header
// and changing the font of the header
pageSetup.SetHeader(1, "&\"Times New Roman,Bold\"&D-&T");
// Setting current file name at the right section of the header and changing the
// font of the header
pageSetup.SetHeader(2, "&\"Times New Roman,Bold\"&12&F");
// Setting a string at the left section of the footer and changing the font
// of a part of this string ("123")
pageSetup.SetFooter(0, "Hello World! &\"Courier New\"&14 123");
// Setting the current page number at the central section of the footer
pageSetup.SetFooter(1, "&P");
// Setting page count at the right section of footer
pageSetup.SetFooter(2, "&N");
// Save the Workbook.
excel.Save("SetHeadersAndFooters_out.xls");

将图像插入页眉或页脚

页面设置类有两个额外的方法,设置页眉图片设置页脚图片用于将图片添加到页眉和页脚中。这些方法采用参数:

  • 部分– 放置图片的页眉或页脚部分。共有左、中、右三个部分,分别用值 0、1 和 2 表示。
  • 字节数组– 图形数据(二进制数据应写入字节数组的缓冲区)。

执行下面的代码并打开文件后,通过以下方式检查工作表的标题:

  1. 文件菜单,选择页面设置.将显示一个对话框。
  2. 选择页眉页脚标签。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a Workbook object
Workbook workbook = new Workbook();
// Creating a string variable to store the url of the logo/picture
string logo_url = dataDir + "aspose-logo.jpg";
// Declaring a FileStream object
FileStream inFile;
// Declaring a byte array
byte[] binaryData;
// Creating the instance of the FileStream object to open the logo/picture in the stream
inFile = new System.IO.FileStream(logo_url, System.IO.FileMode.Open, System.IO.FileAccess.Read);
// Instantiating the byte array of FileStream object's size
binaryData = new Byte[inFile.Length];
// Reads a block of bytes from the stream and writes data in a given buffer of byte array.
long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
// Creating a PageSetup object to get the page settings of the first worksheet of the workbook
PageSetup pageSetup = workbook.Worksheets[0].PageSetup;
// Setting the logo/picture in the central section of the page header
pageSetup.SetHeaderPicture(1, binaryData);
// Setting the script for the logo/picture
pageSetup.SetHeader(1, "&G");
// Setting the Sheet's name in the right section of the page header with the script
pageSetup.SetHeader(2, "&A");
// Saving the workbook
workbook.Save(dataDir + "InsertImageInHeaderFooter_out.xls");
//Closing the FileStream object
inFile.Close();