用 JavaScript 通过 C++ 将工作表范围导出为图片
Contents
[
Hide
]
可能的使用场景
你可以使用 Aspose.Cells for JavaScript 通过 C++ 将工作表制作成图片。但是,有时你只需要将工作表中的部分单元格范围导出为图片。本文介绍如何实现这一点。
导出工作表中的单元格范围为图像
为了取一个范围的图片,请设置打印区域为所需范围,然后将所有边距设置为0,同时将ImageOrPrintOptions.onePagePerSheet设置为true。以下代码示范了如何获取D8:G16范围的图片。下面是示例Excel文件(47153160.xlsx)的截图,可用于代码测试,任何Excel文件都可以使用此代码。
示例 Excel 文件及其导出图像的屏幕截图

执行该代码仅创建范围 D8:G16 的图像。

示例代码
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Export Range To Image</title>
</head>
<body>
<h1>Export Range Of Cells In Worksheet To Image</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, ImageOrPrintOptions, ImageType, SheetRender } = 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();
// Create workbook from uploaded file.
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access the first worksheet
const worksheet = workbook.worksheets.get(0);
// Set the print area with your desired range
worksheet.pageSetup.printArea = "D8:G16";
// Set all margins as 0
worksheet.pageSetup.leftMargin = 0;
worksheet.pageSetup.rightMargin = 0;
worksheet.pageSetup.topMargin = 0;
worksheet.pageSetup.bottomMargin = 0;
// Set OnePagePerSheet option as true and image options
const options = new ImageOrPrintOptions();
options.onePagePerSheet = true;
options.imageType = ImageType.Jpeg;
options.horizontalResolution = 200;
options.verticalResolution = 200;
// Take the image of your worksheet
const sr = new SheetRender(worksheet, options);
const outputData = sr.toImage(0);
const blob = new Blob([outputData], { type: 'image/jpeg' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputExportRangeOfCellsInWorksheetToImage.jpg';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Image';
document.getElementById('result').innerHTML = '<p style="color: green;">Image exported successfully! Click the download link to download the image.</p>';
});
</script>
</html>