通过 C++ 获取带有 JavaScript 的页眉或页脚
Contents
[
Hide
]
页眉和页脚只出现在页面布局视图、打印预览和打印页面上。
如果要同时查看多个工作表的页眉或页脚,也可以使用页面设置对话框。
对于其他工作表类型,如图表工作表或图表,只能通过使用页面设置对话框来插入页眉和页脚。
在MS Excel中获取页眉和页脚
- 单击要查看或更改页眉或页脚的工作表。
- 在“视图”选项卡的“工作簿视图”组中,点击“页面布局”。 Excel会以页面布局视图显示工作表。
- 要查看或编辑页眉或页脚,请单击工作表页面顶部或底部的左侧、中间或右侧页眉或页脚文本框(在页眉下方或在页脚上方)。
通过 C++ 获取 Aspose.Cells for JavaScript 的页眉和页脚
使用 PageSetup.header(number) 和 PageSetup.footer(number) 方法,JavaScript 开发者可以轻松从文件中获取页眉或页脚。
- 构建一个工作簿以打开文件。
- 获取包含你要获取标题或页脚的工作表。
- 使用特定节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>
解析页眉和页脚为命令列表
标题或页脚文本可以包含特殊命令,例如用于页码、当前日期或文本格式属性的占位符。
特殊命令由带有前导“&”的单个字母表示。
标题和页脚字符串采用 ABNF 语法构建。不使用查看器很难理解。
Aspose.Cells for JavaScript 通过 C++ 提供 PageSetup.commands(string) 方法来解析页眉和页脚,作为命令列表。
以下代码展示了如何将标题或页脚解析为命令列表并处理命令:
<!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>