使用 Node.js 通过 C++ 结合 Node.js 与 C++ 实现 Node.js 结合图像处理

将工作簿转换为TIFF

一个Excel文件可以包含多个工作表和多页。 WorkbookRender 允许你将Excel转换为带有多页的TIFF。此外,你可以控制TIFF的多个选项,例如 ImageOrPrintOptions.getTiffCompression()ImageOrPrintOptions.getTiffColorDepth()、 分辨率(ImageOrPrintOptions.getHorizontalResolution()ImageOrPrintOptions.getVerticalResolution()).

以下代码片段显示了如何将Excel转换为具有多个页面的TIFF。源Excel文件生成的TIFF图像附在此供参考。

const AsposeCells = require("aspose.cells.node");
const path = require("path");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "workbook-to-tiff-with-mulitiple-pages.xlsx");

// Load the workbook
const wb = new AsposeCells.Workbook(filePath);

const imgOptions = new AsposeCells.ImageOrPrintOptions();
imgOptions.setImageType(AsposeCells.ImageType.Tiff);

// Set resolution to 200
imgOptions.setHorizontalResolution(200);
imgOptions.setVerticalResolution(200);

// Set TIFF compression to LZW
imgOptions.setTiffCompression(AsposeCells.TiffCompression.CompressionLZW);

const workbookRender = new AsposeCells.WorkbookRender(wb, imgOptions);
workbookRender.toImage("workbook-to-tiff-with-mulitiple-pages.tiff");

将工作表转换为图像

工作表包含您想要分析的数据。例如,工作表可以包含参数、总计、百分比、异常和计算。

作为开发人员,您可能需要将工作表呈现为图像。例如,您可能需要在应用程序或网页中使用工作表的图像。您可能希望将图像插入到 Microsoft Word 文档、PDF 文件、PowerPoint 演示文稿或其他文档类型中。简而言之,您希望将工作表呈现为图像,以便在其他地方使用它。

SheetRender ImageOrPrintOptions WorkbookRender

SheetRender 类代表要渲染为图像的工作表。它具有重载方法 SheetRender.toImage(number, string),可以将工作表转换为具有不同属性或选项的图像文件。它返回一个 Buffer 对象,你可以将图像文件保存到磁盘或流中。支持多种图像格式,例如 BMP、PNG、GIF、JPG、JPEG、TIFF、EMF。

以下代码片段显示了如何将Excel文件中的工作表转换为图像文件。

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// Source directory
const sourceDir = path.join(__dirname, "data");
// Output directory
const outputDir = path.join(__dirname, "output");

const filePath = path.join(sourceDir, "sampleConvertWorksheetToImageByPage.xlsx");
const workbook = new AsposeCells.Workbook(filePath);

const sheet = workbook.getWorksheets().get(0);

const options = new AsposeCells.ImageOrPrintOptions();
options.setHorizontalResolution(200);
options.setVerticalResolution(200);
options.setImageType(AsposeCells.ImageType.Tiff);

// Sheet2Image By Page conversion
const sr = new AsposeCells.SheetRender(sheet, options);
for (let j = 0; j < sr.getPageCount(); j++) 
{
let path = "outputConvertWorksheetToImageByPage_" + (j + 1) + ".tif";
sr.toImage(j, path);
}

将工作表转换为SVG

SVG代表可缩放矢量图形。SVG是基于XML标准的二维矢量图形规范。自1999年以来,它一直是由万维网联盟(W3C)开发的开放标准。

从版本 7.1.0 起,Aspose.Cells for Node.js via C++ 已支持将工作表转换为 SVG 图像。以下代码片段演示了如何将Excel文件中的工作表转换为 SVG 图像文件。

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const outputDir = path.join(dataDir, "output");

// Instantiate a workbook
const workbook = new AsposeCells.Workbook();

// Put sample text in the first cell of the first worksheet in the newly created workbook
workbook.getWorksheets().get(0).getCells().get("A1").putValue("DEMO TEXT ON SHEET1");

// Add second worksheet in the workbook
workbook.getWorksheets().add(AsposeCells.SheetType.Worksheet);

// Set text in the first cell of the second sheet
workbook.getWorksheets().get(1).getCells().get("A1").putValue("DEMO TEXT ON SHEET2");

// Set currently active sheet index to 1 i.e. Sheet2
workbook.getWorksheets().setActiveSheetIndex(1);

// Save workbook to SVG. It shall render the active sheet only to SVG
workbook.save(path.join(outputDir, "ConvertWorksheetToSVG_out.svg"));

高级主题