Aggiungere e recuperare dati

Aggiunta di dati alle celle

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

Aspose.Cells consente ai programmatori 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 ai programmatori di aggiungere diversi tipi di dati alle celle. Utilizzando queste versioni sovraccaricate del metodo PutValue, è possibile aggiungere valori booleani, stringhe, double, integer o data/ora, ecc. alla cella.

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleData = dirPath + u"sampleData.xlsx";
//Path of output excel file
U16String outputData = outPath + u"outputData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the second worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(1);
//Adding a string value to the cell
worksheet.GetCells().Get(u"A1").PutValue(u"Hello World");
//Adding a double value to the cell
worksheet.GetCells().Get(u"A2").PutValue(20.5);
//Adding an integer value to the cell
worksheet.GetCells().Get(u"A3").PutValue(15);
//Adding a boolean value to the cell
worksheet.GetCells().Get(u"A4").PutValue(true);
//Setting the display format of the date
Cell cell = worksheet.GetCells().Get(u"A5");
Style style = cell.GetStyle();
style.SetNumber(15);
cell.SetStyle(style);
//Save the workbook
workbook.Save(outputData);
Aspose::Cells::Cleanup();

Miglioramento dell’efficienza

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

Recupero dei dati dalle celle

Aspose.Cells fornisce una classe Workbook che rappresenta un file Microsoft Excel. La classe Workbook contiene una raccolta Worksheets che consente l’accesso ai fogli di lavoro nel file. Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet fornisce una raccolta Cells. Ogni elemento nella raccolta Cells rappresenta un oggetto della classe Cell.

La classe Cell fornisce diversi metodi che consentono ai programmatori di recuperare i valori dalle celle in base ai loro tipi di dati. Questi metodi includono:

Quando un campo non è compilato, le celle con GetDoubleValue o GetFloatValue generano un’eccezione.

Anche il tipo di dati contenuto in una cella può essere verificato utilizzando il metodo GetType della classe Cell. Infatti, 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
CellValueType_IsBool Specifica che il valore della cella è Booleano.
CellValueType_IsDateTime Specifica che il valore della cella è data/ora.
CellValueType_IsNull Rappresenta una cella vuota.
CellValueType_IsNumeric Specifica che il valore della cella è numerico.
CellValueType_IsString Specifica che il valore della cella è una stringa.
CellValueType_IsUnknown Specifica che il valore della cella è sconosciuto.
Puoi anche usare i tipi di valore predefiniti sopra indicati per confrontare il Tipo dei dati presenti in ogni cella.
Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Path of input
U16String dirPath(u"");
//Path of output
U16String outPath(u"");
//Path of input excel file
U16String sampleData = dirPath + u"sampleData.xlsx";
//Read input excel file
Workbook workbook(sampleData);
//Accessing the third worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(2);
//Get cells from sheet
Cells cells = worksheet.GetCells();
//Variable declarations
U16String strVal;
Date dateVal;
double dblVal;
bool boolVal;
Enumerator<Cell> enCell = cells.GetEnumerator();
while (enCell.MoveNext())
{
Cell cell = enCell.GetCurrent();
switch (cell.GetType())
{
//Evaluating the data type of the cell data for string value
case CellValueType::IsString:
std::cout <<"Cell Value Type Is String." << std::endl;
strVal = cell.GetStringValue();
break;
//Evaluating the data type of the cell data for double value
case CellValueType::IsNumeric:
std::cout <<"Cell Value Type Is Numeric." << std::endl;
dblVal = cell.GetDoubleValue();
break;
//Evaluating the data type of the cell data for boolean value
case CellValueType::IsBool:
std::cout <<"Cell Value Type Is Bool." << std::endl;
boolVal = cell.GetBoolValue();
break;
//Evaluating the data type of the cell data for date/time value
case CellValueType::IsDateTime:
std::cout <<"Cell Value Type Is DateTime." << std::endl;
dateVal = cell.GetDateTimeValue();
break;
//Evaluating the unknown data type of the cell data
case CellValueType::IsUnknown:
cell.GetStringValue();
break;
default:
break;
}
}
Aspose::Cells::Cleanup();