如何使用 Node.js 将 Excel 转换为 JPG

本快速教程将指导 如何使用 Node.js 将 Excel 转换为 JPG。它包含设置开发环境的所有详细信息、步骤列表以及可运行的示例代码,以 使用 Node.js 将 Excel 导出为 JPG**。您还将了解根据要求自定义输出图像并将其保存到磁盘或直接打印到打印机的不同选项。

使用 Node.js 将 XLSX 转换为 JPG 的步骤

  1. 设置 IDE 使用 Aspose.Cells for Node.js 通过 Java 将 Excel 转换为 JPG
  2. 加载 workbook 并创建 ImageOrPrintOptions 对象来设置输出图像属性
  3. 设置图像类型和标志以根据内容自动调整单元格
  4. 使用 ImageOrPrintOptions 对象渲染工作表
  5. 循环浏览渲染工作表中的所有页面并另存为单独的 JPG

这些步骤说明了使用 Node.js* 将 *Excel 文件转换为 JPG 的过程。该过程需要加载工作簿并创建 ImageOrPrintOptions 类对象来自定义渲染过程,例如根据内容调整单元格大小,以及设置图像类型。渲染过程可能会创建需要解析的多个页面,以便将每个图像保存在单独的文件中。

使用 Node.js 将 Excel 转换为 JPG 的代码

var aspose = aspose || {};
aspose.cells = require("aspose.cells");
//Set the license
new aspose.cells.License().setLicense("License.lic");
// Load a workbook
var wb = new aspose.cells.Workbook("SampleExcel.xlsx");
// Create an instance of ImageOrPrintOptions
var imgOptions = new aspose.cells.ImageOrPrintOptions();
// Set the auto-fit flag
imgOptions.setCellAutoFit(true);
// Set the image type to JPEG
imgOptions.setImageType(aspose.cells.ImageType.JPEG);
// Select the sheet
var sheet = wb.getWorksheets().get(0);
// Create the SheetRender object
var sheetRender = new aspose.cells.SheetRender(sheet, imgOptions);
// Parse through all the pages
for (let j = 0; j < sheetRender.getPageCount(); j++)
{
// Save each image separately
sheetRender.toImage(j, "ToImage-out" + j + ".jpg");
}
console.log("Excel converted to image successfully");

上面的示例代码演示了使用 Node.js* 将 *Excel 转换为 JPG。您可以将图像类型设置为任何其他类型,例如 EMF、PICT、PNG、BMP、GIF 和 SVG。 SheetRender.toImage() 方法将渲染的工作表保存为单独的图像,而 toPrinter() 直接打印图像。

本文指导我们如何使用 Node.js 将 Excel 工作表转换为图像。如果您想了解将工作表转换为 SVG 的过程,请参阅 如何在 Node.js 中将 Excel 转换为 SVG 上的文章。

 简体中文