使用JavaScript via C++将指定的工作表保存为PDF
Contents
[
Hide
]
默认情况下,Aspose.Cells 将工作簿中的所有可见工作表保存为 PDF 文件。通过 PdfSaveOptions.sheetSet 选项,可以将指定的工作表保存到 PDF 文件中。例如,可以将活动工作表保存为 PDF,保存所有(包括隐藏)工作表为 PDF,或保存自定义多工作表为 PDF。
将活动工作表保存为 PDF
如果只想导出活动工作表为 PDF,可以将 SheetSet.active 传递给 PdfSaveOptions.sheetSet 选项实现。
源文件 sheetset-example.xlsx 的工作表 Sheet2 为活动工作表。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells SheetSet to PDF Example</title>
<meta charset="utf-8" />
</head>
<body>
<h1>SheetSet to PDF Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx" />
<button id="runExample">Convert to PDF</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, PdfSaveOptions, SheetSet } = 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();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Prepare PdfSaveOptions and set active sheet set
const pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.sheetSet = SheetSet.active;
// Save workbook to PDF using PdfSaveOptions
const outputData = workbook.save(SaveFormat.Pdf, pdfSaveOptions);
const blob = new Blob([outputData], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
resultDiv.innerHTML = '<p style="color: green;">Conversion completed successfully! Click the download link to get the PDF.</p>';
});
</script>
</html>
** 保存所有工作表为 PDF**
SheetSet.visible 表示工作簿中的可见工作表,而 SheetSet.all 表示工作簿中的所有工作表,包括可见和隐藏/不可见的工作表。如果你想将所有工作表导出为 PDF,你可以只传递 SheetSet.all 给 PdfSaveOptions.sheetSet 选项。
源文件sheetset-example.xlsx包含所有四个工作表,其中隐藏了工作表Sheet3。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells: SheetSet to PDF 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, PdfSaveOptions, SheetSet, 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) {
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();
// Open the template excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Set all sheets to output
const pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.sheetSet = SheetSet.all;
// Save the pdf file with PdfSaveOptions
const outputData = workbook.save(SaveFormat.Pdf, pdfSaveOptions);
const blob = new Blob([outputData], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
document.getElementById('result').innerHTML = '<p style="color: green;">PDF generated successfully! Click the download link to get the PDF file.</p>';
});
</script>
</html>
将指定的工作表保存为 PDF
如果想导出多个自定义工作表为 PDF,可以通过传递多个工作表索引到 PdfSaveOptions.sheetSet 选项实现。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Convert Specific Sheets to PDF 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, PdfSaveOptions, SheetSet, 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) {
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();
// Opening the Excel file through the file input
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Set custom multiple sheets (Sheet1, Sheet3) to output
const sheetSet = new SheetSet([0, 2]);
const pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.sheetSet = sheetSet;
// Save the pdf file with PdfSaveOptions
const outputData = workbook.save(SaveFormat.Pdf, pdfSaveOptions);
const blob = new Blob([outputData], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
document.getElementById('result').innerHTML = '<p style="color: green;">PDF created successfully! Click the download link to get the PDF.</p>';
});
</script>
</html>
** 将工作表重新排序为 PDF**
如果你想在不修改源文件的情况下,将工作表(例如反向顺序)重新排序为 PDF,可以通过传递重新排序的工作表索引到 PdfSaveOptions.sheetSet 选项来实现。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>SheetSet to PDF Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert to PDF</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, PdfSaveOptions, SheetSet, 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) {
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();
// Instantiating a Workbook object by opening the Excel file from the file input
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Reorder sheets(Sheet1, Sheet2, Sheet3, Sheet4) to sheets(Sheet4, Sheet3, Sheet2, Sheet1)
const sheetSet = new SheetSet([3, 2, 1, 0]);
const pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.sheetSet = sheetSet;
// Save the pdf file with PdfSaveOptions
const outputData = workbook.save(SaveFormat.Pdf, pdfSaveOptions);
const blob = new Blob([outputData], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
document.getElementById('result').innerHTML = '<p style="color: green;">PDF created successfully! Click the download link to get the PDF file.</p>';
});
</script>
</html>
如果您的电子表格包含公式,最好在将电子表格呈现为PDF格式之前调用 Workbook.calculateFormula()。这样做将确保重新计算依赖于公式的值,并在PDF中呈现正确的值。