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