Different Ways to Open Files with Node.js via C++

How to Open an Excel File via a Path

Developers can open a Microsoft Excel file using its file path on the local computer by specifying it in the Workbook class constructor. Simply pass the path in the constructor as a string. Aspose.Cells will automatically detect the file format type.

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

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Book1.xlsx");
// Opening through Path
// Creating a Workbook object and opening an Excel file using its file path
const workbook1 = new AsposeCells.Workbook(filePath);
console.log("Workbook opened using path successfully!");

How to Open an Excel File via a Stream

It is also simple to open an Excel file as a stream. To do so, use an overloaded version of the constructor that takes the Stream object that contains the file.

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

    // The path to the documents directory.
    const dataDir = path.join(__dirname, "data");
    const filePath = path.join(dataDir, "Book2.xls");
    // Opening through Stream
    // Create a Stream object
    const fstream = fs.createReadStream(filePath);

    // Creating a Workbook object, open the file from a Stream object
    // that contains the content of the file and should support seeking
    const chunks = [];
    fstream.on('data', (chunk) => {
        chunks.push(chunk);
    });
}

How to Open a File with Data Only

To open a file with data only, use the LoadOptions and LoadFilter classes to set the related attributes and options of the classes for the template file to be loaded.

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

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Load only specific sheets with data and formulas
// Other objects, items etc. would be discarded

// Instantiate LoadOptions specified by the LoadFormat
const loadOptions = new AsposeCells.LoadOptions(AsposeCells.LoadFormat.Xlsx);

// Set LoadFilter property to load only data & cell formatting
loadOptions.setLoadFilter(new AsposeCells.LoadFilter(AsposeCells.LoadDataFilterOptions.CellData));

// Create a Workbook object and opening the file from its path
const workbook = new AsposeCells.Workbook(path.join(dataDir, "Book1.xlsx"), loadOptions);
console.log("File data imported successfully!");

How to Load Visible Sheets Only

While loading a Workbook, sometimes you may only need data in visible worksheets in a workbook. Aspose.Cells allows you to skip data in invisible worksheets while loading a workbook. To do this, create a custom function that inherits the LoadFilter class and pass its instance to LoadOptions.getLoadFilter() property.

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

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

// Create a sample workbook
// and put some data in the first cell of all 3 sheets
const createWorkbook = new AsposeCells.Workbook();
createWorkbook.getWorksheets().get("Sheet1").getCells().get("A1").putValue("Aspose");
createWorkbook.getWorksheets().add("Sheet2").getCells().get("A1").putValue("Aspose");
createWorkbook.getWorksheets().add("Sheet3").getCells().get("A1").putValue("Aspose");
createWorkbook.getWorksheets().get("Sheet3").setIsVisible(false);
createWorkbook.save(samplePath);

// Load the sample workbook
const loadOptions = new AsposeCells.LoadOptions();
loadOptions.setLoadFilter(new AsposeCells.LoadFilter()); // Corrected line by defining LoadFilter properly

const loadWorkbook = new AsposeCells.Workbook(samplePath, loadOptions);
console.log(`Sheet1: A1: ${loadWorkbook.getWorksheets().get("Sheet1").getCells().get("A1").getValue()}`);
console.log(`Sheet2: A1: ${loadWorkbook.getWorksheets().get("Sheet2").getCells().get("A1").getValue()}`);
console.log(`Sheet3: A1: ${loadWorkbook.getWorksheets().get("Sheet3").getCells().get("A1").getValue()}`);

Here is the implementation of the CustomLoad class referenced in the above snippet.

const { Workbook, LoadDataFilterOptions } = require("aspose.cells.node");

class CustomLoad {
    startSheet(sheet) {
        if (sheet.isVisible()) {
            // Load everything from a visible worksheet
            this.loadDataFilterOptions = LoadDataFilterOptions.All;
        } else {
            // Load nothing
            this.loadDataFilterOptions = LoadDataFilterOptions.Structure;
        }
    }
}