Excel管理控件Aspose.Cells開發者指南(三十六):設置頁眉和頁腳
Aspose.Cells for .NET是Excel電子表格編程API,可加快電子表格管理和處理任務,支持構建具有生成,修改,轉換,呈現和打印電子表格功能的跨平臺應用程序。
在接下來的系列教程中,將為開發者帶來Aspose.Cells for .NET的一系列使用教程,例如關于加載保存轉換、字體、渲染、繪圖、智能標記等等。本文重點介紹如何設置頁眉和頁腳。
>>Aspose.Cells for .NET已經更新至v20.7,添加FilterString()條件支持,支持對所有PivotField進行循環,提升Worksheet.Cells.RemoveDuplicates工作性能,發現4處異常情況,點擊下載體驗
第八章:關于頁面功能設置
▲第二節:設置頁眉頁腳
Aspose.Cells允許在運行時向工作表添加頁眉和頁腳,但是建議在預先設計的文件中手動設置頁眉和頁腳以進行打印??梢允褂肕icrosoft Excel作為GUI工具來設置頁眉和頁腳,以節省工作量和開發時間。Aspose.Cells可以導入文件并保存設置。
腳本命令
為了在運行時添加頁眉和頁腳,Aspose.Cells提供了特殊的API調用和腳本命令來格式化頁眉和頁腳。
腳本命令 |
描述 |
&P | 當前頁碼 |
&G | 照片 |
&N | 總頁數 |
&D | 當前日期 |
&T | 當前時間 |
&A |
|
&F | 沒有路徑的文件名 |
&“” | 代表字體名稱。例如:&“ Arial” |
&“, ” | 用樣式表示字體名稱。例如:&“ Arial,Bold” |
& | 代表字體大小。例如:“&14abc”。但是,如果此命令后跟要在頁眉中打印的純數字,則應在字體大小中用空格字符分隔。例如:“&14 123”。 |
設置頁眉和頁腳
所述PAGESETUP 類提供兩種方法,SetHeader可以 和SetFooter,用于將頁眉和頁腳添加到工作表。這些方法僅采用兩個參數:
- 節——應該放置頁眉或頁腳的節。共有三部分:左,中和右,分別由0、1和2表示。
- 腳本——用于頁眉或頁腳的腳本。該腳本包含用于格式化頁眉或頁腳的腳本命令。
// 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");
將圖像插入頁眉或頁腳
該PAGESETUP 類有兩個方法,SetHeaderPicture 和SetFooterPicture,用于將圖片添加到頁眉和頁腳。這些方法采用以下參數:
- 節——將放置圖片的頁眉或頁腳節。共有三個部分,左,中和右,分別由值0、1和2表示。
- 字節數組——圖形數據(二進制數據應寫入字節數組的緩沖區中)。
執行以下代碼并打開文件后,通過以下方法檢查工作表的標題:
- 在文件菜單上,選擇頁面設置。將顯示一個對話框。
- 選擇“ 頁眉/頁腳”選項卡。
// 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();
還想要更多嗎?您可以點擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。