マージンの設定(JavaScript経由C++)
Contents
[
Hide
]
Aspose.CellsはMicrosoft Excelのページ設定オプションを完全にサポートしています。開発者は印刷プロセスを制御するためにワークシートのページ設定設定を構成する必要があります。このトピックでは、Aspose.Cellsを使用してページ余白を設定する方法について説明します。
マージンの設定
Aspose.Cells for JavaScriptをC++で使用すると、Excelファイルを表すWorkbookクラスがあります。WorkbookクラスにはWorkbook.worksheetsコレクションが含まれ、Excelファイル内の各ワークシートにアクセスできます。ワークシートはWorksheetクラスで表されます。
Worksheet.pageSetupプロパティはワークシートのページ設定オプションを設定するために使用されます。Worksheet.pageSetup属性はWorksheet.pageSetupクラスのオブジェクトで、印刷されるワークシートのページレイアウトオプションを設定することを可能にします。Worksheet.pageSetupクラスはさまざまなプロパティとメソッドを提供し、ページ設定オプションの設定に使用されます。
ページ余白
Worksheet.pageSetupクラスメンバーを使用してページの余白(左、右、上、下)を設定します。以下にページ余白を指定するために使用されるいくつかのメンバーを示します:
<!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>
ページの中央に配置
ページを横方向および縦方向に中央揃えに配置することは可能です。そのための便利なメンバーとしてWorksheet.pageSetupクラスのPageSetup.centerHorizontallyと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>
ヘッダーとフッタのマージン
ヘッダーとフッターの余白を設定するには、Worksheet.pageSetupクラスのメンバーであるPageSetup.headerMarginと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>