Gestire i dati dei file di Excel

Come aggiungere dati alle celle

Aspose.Cells fornisce una classe, Workbook, che rappresenta un file Microsoft Excel. La classe Workbook contiene una raccolta Worksheets che consente di accedere a ciascun foglio di lavoro nel file Excel. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta Cells. Ogni elemento della raccolta Cells rappresenta un oggetto della classe Cell

Aspose.Cells permette agli sviluppatori di aggiungere dati alle celle nei fogli di lavoro chiamando il metodo PutValue della classe Cell. Aspose.Cells fornisce versioni sovraccaricate del metodo PutValue che consentono agli sviluppatori di aggiungere diversi tipi di dati alle celle. Utilizzando queste versioni sovraccaricate del metodo PutValue, è possibile aggiungere valori booleani, stringhe, doppie, interi o valori data/ora, ecc. alla cella.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Adding a string value to the cell
worksheet.Cells["A1"].PutValue("Hello World");
// Adding a double value to the cell
worksheet.Cells["A2"].PutValue(20.5);
// Adding an integer value to the cell
worksheet.Cells["A3"].PutValue(15);
// Adding a boolean value to the cell
worksheet.Cells["A4"].PutValue(true);
// Adding a date/time value to the cell
worksheet.Cells["A5"].PutValue(DateTime.Now);
// Setting the display format of the date
Style style = worksheet.Cells["A5"].GetStyle();
style.Number = 15;
worksheet.Cells["A5"].SetStyle(style);
// Saving the Excel file
workbook.Save(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, è consigliabile aggiungere valori alle celle, prima per righe e poi per colonne. Questo approccio migliora notevolmente l’efficienza delle applicazioni.

Come recuperare i dati dalle celle

Aspose.Cells fornisce una classe, Workbook che rappresenta un file di Microsoft Excel. La classe Workbook contiene una collezione Worksheets che consente di accedere 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 consentono agli sviluppatori di recuperare i valori dalle celle in base ai loro tipi di dati. Queste proprietà includono:

  • StringValue: restituisce il valore di stringa della cella.
  • DoubleValue: restituisce il valore decimale della cella.
  • BoolValue: restituisce il valore booleano della cella.
  • DateTimeValue: restituisce il valore data/ora della cella.
  • FloatValue: restituisce il valore in virgola mobile della cella.
  • IntValue: restituisce il valore intero della cella.

Quando un campo non è compilato, le celle con DoubleValue o FloatValue generano un’eccezione.

Il tipo di dati contenuti in una cella può anche essere verificato utilizzando la proprietà Type della classe Cell. Infatti, la proprietà Type 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.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Opening an existing workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing first worksheet
Worksheet worksheet = workbook.Worksheets[0];
foreach (Cell cell1 in worksheet.Cells)
{
// Variables to store values of different data types
string stringValue;
double doubleValue;
bool boolValue;
DateTime dateTimeValue;
// Passing the type of the data contained in the cell for evaluation
switch (cell1.Type)
{
// Evaluating the data type of the cell data for string value
case CellValueType.IsString:
stringValue = cell1.StringValue;
Console.WriteLine("String Value: " + stringValue);
break;
// Evaluating the data type of the cell data for double value
case CellValueType.IsNumeric:
doubleValue = cell1.DoubleValue;
Console.WriteLine("Double Value: " + doubleValue);
break;
// Evaluating the data type of the cell data for boolean value
case CellValueType.IsBool:
boolValue = cell1.BoolValue;
Console.WriteLine("Bool Value: " + boolValue);
break;
// Evaluating the data type of the cell data for date/time value
case CellValueType.IsDateTime:
dateTimeValue = cell1.DateTimeValue;
Console.WriteLine("DateTime Value: " + dateTimeValue);
break;
// Evaluating the unknown data type of the cell data
case CellValueType.IsUnknown:
stringValue = cell1.StringValue;
Console.WriteLine("Unknown Value: " + stringValue);
break;
// Terminating the type checking of type of the cell data is null
case CellValueType.IsNull:
break;
}
}

Argomenti avanzati