Node.js ve C++ kullanarak Dosyaları Açmanın Farklı Yolları

Bir Excel Dosyasını Yol Üzerinden Nasıl Açılır

Geliştiriciler, bir Microsoft Excel dosyasını, yerel bilgisayardaki dosya yolunu Workbook sınıf yapısında belirterek açabilir. Yolu, bir dize olarak geçirmeniz yeterlidir. Aspose.Cells otomatik olarak dosya formatını tespit eder.

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!");

Bir Akış Üzerinden Excel Dosyasını Nasıl Açılır

Bir Excel dosyasını akış olarak da açmak oldukça basittir. Bunu yapmak için, dosyayı içeren Stream nesnesini alan aşırı yüklenmiş yapıcıyı kullanın.

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 file and it should support seeking
const chunks = [];
fstream.on('data', (chunk) => {
chunks.push(chunk);

Yalnızca Veri ile Bir Dosyayı Nasıl Açılır

Sadece verileri içeren bir dosyayı açmak için, ilgili özellikleri ve seçenekleri ayarlamak üzere LoadOptions ve LoadFilter sınıflarını kullanın.

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!");

Yalnızca Görünür Sayfaları Yükleme

Workbook yüklerken, bazen yalnızca çalışma kitabında görünür çalışanlardaki verilere ihtiyacınız olabilir. Aspose.Cells, bir çalışma kitabını yüklerken görünmez çalışanlardaki verileri atlamanıza izin verir. Bunu yapmak için, LoadFilter sınıfından türeyen özelleştirilmiş bir fonksiyon oluşturun ve örneğini LoadOptions.getLoadFilter() özelliğine aktarın.

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 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(`Sheet1: A2: ${loadWorkbook.getWorksheets().get("Sheet2").getCells().get("A1").getValue()}`);
console.log(`Sheet1: A3: ${loadWorkbook.getWorksheets().get("Sheet3").getCells().get("A1").getValue()}`);

Yukarıdaki kod örneğinde referans verilen CustomLoad sınıfının uygulaması burada.

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

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