Impostazione di intestazioni e piè di pagina

Impostazione di intestazioni e piè di pagina

Aspose.Cells consente di aggiungere intestazioni e piè di pagina ai fogli di lavoro in fase di esecuzione, ma si consiglia di impostare manualmente intestazioni e piè di pagina in un file predefinito per la stampa. È possibile utilizzare Microsoft Excel come strumento GUI per impostare intestazioni e piè di pagina per risparmiare fatica e tempo di sviluppo. Aspose.Cells può importare il file e salvare le impostazioni.

Per aggiungere intestazioni e piè di pagina in fase di esecuzione, Aspose.Cells fornisce speciali chiamate API e comandi di script per formattare intestazioni e piè di pagina.

Comandi di script

I comandi di script sono comandi speciali che consentono di impostare la formattazione di intestazioni e piè di pagina.

Comandi di script Descrizione
&P Il numero di pagina corrente
&G Una foto
&N Il numero totale di pagine
&D La data corrente
&T L’ora corrente
&UN Il nome del foglio di lavoro
&F Il nome del file senza il relativo percorso
&"<FontName>" Rappresenta un nome di carattere. Ad esempio: &“Arial”
&"<FontName>, <FontStyle>" Rappresenta il nome del carattere con lo stile. Ad esempio: &“Arial,Grassetto”
&<FontSize> Rappresenta la dimensione del carattere. Ad esempio: “&14abc”. Tuttavia, se questo comando è seguito da un numero in chiaro da stampare nell’intestazione, questo dovrebbe essere separato con un carattere di spazio dalla dimensione del carattere. Ad esempio: “&14 123”.

Imposta intestazioni e piè di pagina

IlImpostazione della pagina class fornisce due metodi,Impostaintestazione eImposta piè di pagina, utilizzato per aggiungere un’intestazione e un piè di pagina a un foglio di lavoro. Questi metodi accettano solo due parametri:

  • Sezione – la sezione in cui inserire l’intestazione o il piè di pagina. Ci sono tre sezioni: sinistra, centro e destra, rappresentate rispettivamente da 0, 1 e 2.
  • Sceneggiatura – lo script da utilizzare per l’intestazione o il piè di pagina. Questo script contiene comandi di script per formattare intestazioni o piè di pagina.
// 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");

Inserisci un’immagine in un’intestazione o in un piè di pagina

IlImpostazione della pagina class ha due metodi aggiuntivi,SetHeaderPicture eSetFooterPicture, utilizzato per aggiungere immagini nell’intestazione e nel piè di pagina. Questi metodi accettano i parametri:

  • Sezione– la sezione dell’intestazione o del piè di pagina in cui verrà posizionata l’immagine. Ci sono tre sezioni, sinistra, centro e destra, rappresentate rispettivamente dai valori 0, 1 e 2.
  • Matrice di byte – i dati grafici (i dati binari devono essere scritti nel buffer di un array di byte).

Dopo aver eseguito il codice seguente e aperto il file, controlla l’intestazione del foglio di lavoro:

  1. SulFile menù, selezionareImpostazione della pagina. Verrà visualizzata una finestra di dialogo.
  2. Seleziona ilIntestazione/piè di pagina scheda.
// 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();