Getting Headers or Footers with JavaScript via C++
Headers and footers are displayed only in Page Layout view, Print Preview, and on printed pages.
You can also use the Page Setup dialog box if you want to view headers or footers for more than one worksheet at a time.
For other sheet types, such as chart sheets, or charts, you can insert headers and footers only by using the Page Setup dialog box.
Getting Headers and Footers in MS Excel
- Click the worksheet where you want to view or change headers or footers.
- On the View tab, in the Workbook Views group, click Page Layout. Excel displays the worksheet in Page Layout view.
- To view or edit a header or footer, click the left, center, or right header or footer text box at the top or the bottom of the worksheet page (under Header, or above Footer).
Getting Headers and Footers using Aspose.Cells for JavaScript via C++
With PageSetup.header(number) and PageSetup.footer(number) methods, JavaScript developers can simply get headers or footers from the file.
- Construct a Workbook to open the file.
- Get the worksheet where you want to get headers or footer.
- Get the header or footer with a specific section ID.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Header/Footer Reader Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
function escapeHtml(str) {
if (str === null || str === undefined) return '';
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate a new Workbook from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
const sheet = workbook.worksheets.get(0);
// Gets left section of header
const headerLeft = sheet.pageSetup.header(0);
// Gets center section of header
const headerCenter = sheet.pageSetup.header(1);
// Gets right section of header
const headerRight = sheet.pageSetup.header(2);
// Gets center section of footer
const footerCenter = sheet.pageSetup.footer(1);
const resultHtml = [
`<p><strong>Left Header:</strong> ${escapeHtml(headerLeft)}</p>`,
`<p><strong>Center Header:</strong> ${escapeHtml(headerCenter)}</p>`,
`<p><strong>Right Header:</strong> ${escapeHtml(headerRight)}</p>`,
`<p><strong>Center Footer:</strong> ${escapeHtml(footerCenter)}</p>`
].join('');
document.getElementById('result').innerHTML = resultHtml;
});
</script>
</html>
Parse Headers and Footers to Command List
The header or footer text can contain special commands, for example, a placeholder for the page number, current date, or text formatting attributes.
Special commands are represented by a single letter with a leading ampersand ("&").
The header and footer strings are constructed using ABNF grammar. It’s not easy to understand without a viewer.
Aspose.Cells for JavaScript via C++ provides PageSetup.commands(string) method to parse headers and footers as a command list.
The following codes show how to parse the header or footer as a command list and process commands:
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Header/Footer Commands Example</title>
</head>
<body>
<h1>Header/Footer Commands Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate a Workbook from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
const sheet = workbook.worksheets.get(0);
// Gets left section of header
const headerSection = sheet.pageSetup.header(0);
const commands = sheet.pageSetup.commands(headerSection) || [];
const items = [];
commands.forEach(c => {
const type = c.type;
switch (type) {
case AsposeCells.HeaderFooterCommandType.SheetName:
// embedded the name of the sheet to header or footer
items.push('<li>SheetName command found (embeds sheet name)</li>');
break;
default:
items.push(`<li>Command type: ${type}</li>`);
break;
}
});
if (!items.length) {
items.push('<li>No header/footer commands found.</li>');
}
resultDiv.innerHTML = `<ul>${items.join('')}</ul>`;
// Save the (possibly unchanged) workbook and provide a download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'HeaderFooter_result.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Result Workbook';
});
</script>
</html>