通过JavaScript的页面设置功能使用C++
Contents
[
Hide
]
介绍
使用C++的Aspose.Cells for JavaScript,你可以操作Excel工作簿的各种页面设置功能。这些功能包括设置页面大小、方向、边距等。合理配置这些参数可以改善打印和视图体验。
设置页面大小和方向
您可以使用PageSetup类指定工作表的页面大小和方向。它提供了各种属性以管理页面尺寸和布局。
示例代码
以下是演示如何设置页面大小和方向的示例代码片段。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Page Setup Example</title>
</head>
<body>
<h1>Page Setup Example</h1>
<p>You may optionally upload an existing Excel file to modify. If none is selected, a new workbook will be created.</p>
<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 } = 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');
let workbook;
if (fileInput.files && fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Access the first worksheet
const worksheet = workbook.worksheets.get(0);
// Set the page size to A4 (paperSize = 0)
worksheet.pageSetup.paperSize = 0;
// Set the page orientation to Landscape (orientation = 1)
worksheet.pageSetup.orientation = 1;
// Save the workbook as XLSX and provide a download link
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'PageSetupExample.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Page setup updated successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
设置页边距
您还可以使用相同的PageSetup类设置页面的边距。边距可以调整为左、右、上和下。
示例代码
以下是设置工作表边距的方法。