Gestire i dati dei file di Excel

Come aggiungere dati alle celle

Aspose.Cells for Node.js via C++ fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene una collezione Worksheets che permette l’accesso a ogni foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una collezione Cells. Ogni elemento nella collezione Cells rappresenta un oggetto della classe Cell.

Aspose.Cells consente agli sviluppatori di aggiungere dati alle celle nei fogli di lavoro chiamando il metodo putValue della classe Cell. Aspose.Cells offre versioni sovrapposte del metodo putValue che permettono di aggiungere diversi tipi di dati alle celle. Utilizzando queste versioni sovrapposte del metodo putValue, è possibile aggiungere valori booleani, stringhe, double, interi o data/ora, ecc. alle celle.

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create directory if it is not already present.
const fs = require("fs");
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
// Instantiating a Workbook object
let workbook = new AsposeCells.Workbook();
// Obtaining the reference of the first worksheet
let worksheet = workbook.getWorksheets().get(0);
// Adding a string value to the cell
worksheet.getCells().get("A1").putValue("Hello World");
// Adding a double value to the cell
worksheet.getCells().get("A2").putValue(20.5);
// Adding an integer value to the cell
worksheet.getCells().get("A3").putValue(15);
// Adding a boolean value to the cell
worksheet.getCells().get("A4").putValue(true);
// Adding a date/time value to the cell
worksheet.getCells().get("A5").putValue(new Date());
// Setting the display format of the date
let style = worksheet.getCells().get("A5").getStyle();
style.setNumber(15);
worksheet.getCells().get("A5").setStyle(style);
// Saving the Excel file
workbook.save(path.join(dataDir, "output.out.xls"));

Come migliorare l’efficienza

Se si utilizza il metodo putValue per inserire una grande quantità di dati in un foglio di lavoro, si dovrebbe prima aggiungere i valori alle celle riga per riga e poi colonna per colonna. Questo approccio migliora notevolmente l’efficienza delle applicazioni.

Come recuperare i dati dalle celle

Aspose.Cells for Node.js via C++ fornisce una classe, Workbook che rappresenta un file Microsoft Excel. La classe Workbook contiene una collezione Worksheets che permette l’accesso ai fogli di lavoro nel file. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una collezione Cells. Ogni elemento nella collezione Cells rappresenta un oggetto della classe Cell.

La classe Cell fornisce diverse proprietà che permettono agli sviluppatori di recuperare valori dalle celle in base ai loro tipi di dati. Queste proprietà includono:

Quando un campo non è compilato, le celle con getDoubleValue() o getFloatValue() generano un’eccezione.

Il tipo di dati contenuti in una cella può essere verificato anche utilizzando il metodo getType() della classe Cell. In realtà, il metodo getType() della classe Cell si basa sull’enumerazione CellValueType i cui valori predefiniti sono elencati di seguito:

Tipi di Valore della Cella Descrizione
IsBool Specifica che il valore della cella è booleano.
IsDateTime Specifica che il valore della cella è data/ora.
IsNull Rappresenta una cella vuota.
IsNumeric Specifica che il valore della cella è numerico.
IsString Specifica che il valore della cella è una stringa.
IsUnknown Specifica che il valore della cella è sconosciuto.

È anche possibile utilizzare i tipi di valore di cella predefiniti sopra per confrontare con il tipo di dati presente in ogni cella.

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Opening an existing workbook
const workbook = new AsposeCells.Workbook("book1.xls");
// Accessing first worksheet
const worksheet = workbook.getWorksheets().get(0);
var cells = worksheet.getCells();
var maxRow = cells.getMaxRow();
var maxColumn = cells.getMaxColumn();
for (let i = 0; i <= maxRow; i++) {
for (let j = 0; j <= maxColumn; j++)
{
const cell1 = cells.get(i, j);
// Variables to store values of different data types
let stringValue;
let doubleValue;
let boolValue;
let dateTimeValue;
// Passing the type of the data contained in the cell for evaluation
switch (cell1.getType()) {
// Evaluating the data type of the cell data for string value
case AsposeCells.CellValueType.IsString:
stringValue = cell1.getStringValue();
console.log("String Value: " + stringValue);
break;
// Evaluating the data type of the cell data for double value
case AsposeCells.CellValueType.IsNumeric:
doubleValue = cell1.getDoubleValue();
console.log("Double Value: " + doubleValue);
break;
// Evaluating the data type of the cell data for boolean value
case AsposeCells.CellValueType.IsBool:
boolValue = cell1.getBoolValue();
console.log("Bool Value: " + boolValue);
break;
// Evaluating the data type of the cell data for date/time value
case AsposeCells.CellValueType.IsDateTime:
dateTimeValue = cell1.getDateTimeValue();
console.log("DateTime Value: " + dateTimeValue);
break;
// Evaluating the unknown data type of the cell data
case AsposeCells.CellValueType.IsUnknown:
stringValue = cell1.getStringValue();
console.log("Unknown Value: " + stringValue);
break;
// Terminating the type checking of type of the cell data is null
case AsposeCells.CellValueType.IsNull:
break;
}
}
}

Argomenti avanzati