将XLSX文件转换为PDF格式,使用JavaScript通过C++

将Excel转换为PDF

这个示例使用Excel文件(SampleInput.xlsx)作为模板。工作簿包含带有图表和图片的工作表。每个工作表使用不同类型的格式,包括字体、属性、颜色、阴影效果和边框。第一个工作表有柱状图,最后一个工作表有图片。

模板Excel文件

模板文件包含三个工作表,包括图表和作为媒体的图片。第一个工作表有图表,最后一个工作表包含图片,如下面的截图所示。

todo:image_alt_text todo:image_alt_text
第一个工作表 (销售预测) 第二个工作表 (销售报告)
todo:image_alt_text todo:image_alt_text
第三个工作表 (数据录入) 最后一个工作表 (图像)

转换过程

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example - Excel to PDF</title>
    </head>
    <body>
        <h1>Excel 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;"></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) {
                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 with the uploaded file
            const workbook = new Workbook(new Uint8Array(arrayBuffer));

            // Saving the PDF file
            const outputData = workbook.save(SaveFormat.Pdf);
            const blob = new Blob([outputData], { type: 'application/pdf' });
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'Output.out.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>

结果

当上述代码运行后,在应用程序目录的Files文件夹中创建了一个PDF文件。 以下屏幕截图显示了PDF页面。 请注意,页眉和页脚也在输出PDF文件中保留。

todo:image_alt_text todo:image_alt_text
第一个工作表 (销售预测) 第二个工作表 (销售报告)
todo:image_alt_text todo:image_alt_text
第三个工作表 (数据录入) 最后一个工作表 (图像)