ヘッダーとフッターの設定

ヘッダーとフッターの設定

Aspose.Cells を使用すると、実行時にワークシートにヘッダーとフッターを追加できますが、印刷用にあらかじめデザインされたファイルにヘッダーとフッターを手動で設定することをお勧めします。 Microsoft Excel を GUI ツールとして使用してヘッダーとフッターを設定し、労力と開発時間を節約できます。 Aspose.Cells ファイルをインポートして設定を保存できます。

実行時にヘッダーとフッターを追加するために、Aspose.Cells は特別な API 呼び出しとスクリプト コマンドを提供して、ヘッダーとフッターをフォーマットします。

スクリプト コマンド

スクリプト コマンドは、ヘッダーとフッターの書式を設定できる特別なコマンドです。

スクリプト コマンド 説明
&P 現在のページ番号
&G
&N 総ページ数
&D 現在の日付
&T 現在時刻
&A ワークシート名
&F パスなしのファイル名
&"<FontName>" フォント名を表します。例: &“Arial”
&"<FontName>, <FontStyle>" フォント名をスタイルで表します。例: &“Arial,Bold”
&<FontSize> フォントサイズを表します。例: 「&14abc」。ただし、このコマンドの後にヘッダーに印刷されるプレーンな数字が続く場合、これはフォント サイズからスペース文字で区切られる必要があります。例: 「&14 123」。

ヘッダーとフッターを設定する

ページ設定クラスは 2 つのメソッドを提供します。SetHeaderSetFooter、ワークシートにヘッダーとフッターを追加するために使用されます。これらのメソッドは、次の 2 つのパラメーターのみを受け取ります。

  • セクション – ヘッダーまたはフッターを配置するセクション。左、中央、右の 3 つのセクションがあり、それぞれ 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");

ヘッダーまたはフッターに画像を挿入する

ページ設定クラスには 2 つの追加メソッドがあり、SetHeaderPictureSetFooterPicture、ヘッダーとフッターに画像を追加するために使用されます。これらのメソッドはパラメータを取ります:

  • セクション– 画像が配置されるヘッダーまたはフッター セクション。左、中央、右の 3 つのセクションがあり、それぞれ値 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();