> For the complete documentation index, see [llms.txt](https://ghiscoding.gitbook.io/excel-builder-vanilla/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ghiscoding.gitbook.io/excel-builder-vanilla/cookbook/worksheet-headers-footers.md).

# Adding Headers/Footers to a Worksheet

### Adding headers and footers to a worksheet

Headers and footers are there mostly for when the user prints. A good example is the `"3 out of 12"` that you might get on the bottom of some pages, showing that you're looking at page three out of twelve. Giving print titles (such as `"CONFIDENTIAL"` or the name of the organization that this is being printed for) is pretty common practice. The problem with having this data in the worksheet is that you're potentially messing up your cells just in the name of slapping a header in so the person knows what they are looking at when it gets printed.

In office 2007 and up, go to the `View` and change to the `Page Layout` view to see the headers on a worksheet (or add/alter them).

Headers and footers are set with the `setHeader` and `setFooter` methods - each method expects an array with the length of three. Position one is for the leftmost block in the header/footer, the second is the for the center block and the third is for the right block. Each block takes a set of instructions:

A plain string with text An object with a `text` property and one or all of `bold`, `underline` and `size`. Size will be a number, the rest will be booleans. An array of items (either text or objects) Some special codes:

`&P` - The page number that is being printed/looked at (generally the `#` in `"# of 12"`) `&N` - The total number of pages that will be printed `&D` - The current date `&T` - The current time `&Z` - The path to the folder that the file is in `&F` - The filename `&A` - The name of the worksheet

```ts
import { createExcelFile, createWorkbook } from 'excel-builder-vanilla';

const originalData = [
  ['Artist', 'Album', 'Price'],
  ['Buckethead', 'Albino Slug', 8.99],
  ['Buckethead', 'Electric Tears', 13.99],
  ['Buckethead', 'Colma', 11.34],
  ['Crystal Method', 'Vegas', 10.54],
  ['Crystal Method', 'Tweekend', 10.64],
  ['Crystal Method', 'Divided By Night', 8.99],
];

const artistWorkbook = createWorkbook();
const albumList = artistWorkbook.createWorksheet({ name: 'Album List' });

albumList.setData(originalData); // <-- Here's the important part

albumList.setHeader([
  'This will be on the left',
  ['In the middle', { text: 'I shall be', bold: true }],
  { text: 'Right, underlined and size of 16', font: 16, underline: true },
]);

albumList.setFooter(['Date of print: &D &T', '&A', 'Page &P of &N']);
artistWorkbook.addWorksheet(albumList);

const data = createExcelFile(artistWorkbook);
downloader('Artist WB.xlsx', data);
```

***

### NodeJS Usage Example

Headers and footers work in NodeJS as well:

```js
import fs from 'node:fs';
import { createWorkbook, createExcelFile } from 'excel-builder-vanilla';

const workbook = createWorkbook();
const sheet = workbook.createWorksheet({ name: 'Demo' });
sheet.setData([
  ['Artist', 'Album', 'Price'],
  ['Buckethead', 'Albino Slug', 8.99],
]);
sheet.setHeader(['Left', 'Center', 'Right']);
sheet.setFooter(['Date: &D', '&A', 'Page &P of &N']);
workbook.addWorksheet(sheet);

const buffer = createExcelFile(workbook);
fs.writeFileSync('output.xlsx', buffer);
```

> **Note:** some NodeJS scripts can be found in the [packages/demo/node-examples/](https://github.com/ghiscoding/excel-builder-vanilla/tree/main/packages/demo/node-examples/) folder.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://ghiscoding.gitbook.io/excel-builder-vanilla/cookbook/worksheet-headers-footers.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
