Impostare i margini con JavaScript via C++
Impostazione margini
Aspose.Cells for JavaScript via C++ fornisce una classe, Workbook, che rappresenta un file Excel. La classe Workbook contiene la raccolta Workbook.worksheets che consente l’accesso a ogni foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet.
La proprietà Worksheet.pageSetup viene utilizzata per impostare le opzioni di configurazione della pagina di un foglio di lavoro. L’attributo Worksheet.pageSetup è un oggetto della classe Worksheet.pageSetup che permette agli sviluppatori di impostare diverse opzioni di layout pagine per un foglio di lavoro stampato. La classe Worksheet.pageSetup fornisce varie proprietà e metodi usati per impostare le opzioni di configurazione della pagina.
Margini di Pagina
Impostare i margini della pagina (sinistra, destra, superiore, inferiore) usando i membri della classe Worksheet.pageSetup. Di seguito sono elencati alcuni membri utilizzati per specificare i margini della pagina:
<!DOCTYPE html>
<html>
<head>
<title>Set Page Margins Example</title>
</head>
<body>
<h1>Set Page Margins 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, 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');
if (!fileInput.files.length) {
// Proceed with a new empty workbook if no file selected
}
const file = fileInput.files.length ? fileInput.files[0] : null;
let workbook;
if (file) {
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
const worksheets = workbook.worksheets;
const worksheet = worksheets.get(0);
const pageSetup = worksheet.pageSetup;
pageSetup.bottomMargin = 2;
pageSetup.leftMargin = 1;
pageSetup.rightMargin = 1;
pageSetup.topMargin = 3;
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'SetMargins_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Page margins set successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Centra sulla Pagina
È possibile centrare qualcosa su una pagina orizzontalmente e verticalmente. Per questo, ci sono alcuni membri utili della classe Worksheet.pageSetup, PageSetup.centerHorizontally e PageSetup.centerVertically.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Center On Page</title>
</head>
<body>
<h1>Center On Page 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, 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 () => {
// Create a workbook object (blank workbook)
const workbook = new Workbook();
// Get the worksheets in the workbook
const worksheets = workbook.worksheets;
// Get the first (default) worksheet
const worksheet = worksheets.get(0);
// Get the pagesetup object
const pageSetup = worksheet.pageSetup;
// Specify Center on page Horizontally and Vertically
pageSetup.centerHorizontally = true;
pageSetup.centerVertically = true;
// Save the Workbook in Excel 97-2003 format (.xls)
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'CenterOnPage_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook processed successfully! Click the download link to get the file.</p>';
});
</script>
</html>
Margini Intestazione e Piè di Pagina
Imposta i margini di intestazione e piè di pagina con membri della classe Worksheet.pageSetup come PageSetup.headerMargin e PageSetup.footerMargin.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Set Header/Footer Margins</title>
</head>
<body>
<h1>Set Header/Footer Margins 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 } = 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 () => {
// Create a new workbook (equivalent to new AsposeCells.Workbook() in JavaScript)
const workbook = new Workbook();
// Get the worksheets collection
const worksheets = workbook.worksheets;
// Get the first (default) worksheet
const worksheet = worksheets.get(0);
// Get the pageSetup object
const pageSetup = worksheet.pageSetup;
// Specify Header / Footer margins (converted from setHeaderMargin/setFooterMargin)
pageSetup.headerMargin = 2;
pageSetup.footerMargin = 2;
// Save the Workbook as Excel 97-2003 format
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'CenterOnPage_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook processed successfully. Click the download link to get the file.</p>';
});
</script>
</html>