翻譯|使用教程|編輯:李顯亮|2020-08-13 10:25:39.900|閱讀 1526 次
概述:頁眉和頁腳分別是顯示在頂部邊緣下方或底部邊緣上方的文本行。也可以在工作表中添加頁眉和頁腳。Aspose.Cells允許您在運行時向工作表添加頁眉和頁腳,但是建議在預先設計的文件中手動設置頁眉和頁腳以進行打印。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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,用于將頁眉和頁腳添加到工作表。這些方法僅采用兩個參數:
// 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,用于將圖片添加到頁眉和頁腳。這些方法采用以下參數:
執行以下代碼并打開文件后,通過以下方法檢查工作表的標題:
// 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();
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn