Lägg till och hämta data
Lägga till data i celler
Aspose.Cells tillhandahåller en klass Workbook som representerar en Microsoft Excel-fil. Workbook klassen innehåller en Worksheets samling som möjliggör åtkomst till varje kalkylblad i Excel-filen. Ett kalkylblad representeras av Worksheet klassen. Worksheet klassen tillhandahåller en Cells samling. Varje objekt i Cells samlingen representerar ett objekt av Cell klassen.
Aspose.Cells tillåter utvecklare att lägga till data i cellerna i kalkylblad genom att anropa Cell klassens PutValue metod. Aspose.Cells tillhandahåller överlagrade versioner av PutValue metoden som låter utvecklare lägga till olika typer av data i celler. Genom att använda dessa överlagrade versioner av PutValue metoden är det möjligt att lägga till en Boolean, sträng, double, heltal eller datum/tid, etc. värden till cellen.
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(); |
Förbättra effektiviteten
Om du använder PutValue metoden för att lägga till en stor mängd data i ett kalkylblad, bör du lägga till värden i cellerna, först efter rader och sedan efter kolumner. Detta tillvägagångssätt förbättrar avsevärt effektiviteten i dina applikationer.
Hämta data från celler
Aspose.Cells tillhandahåller en klass Workbook som representerar en Microsoft Excel-fil. Workbook klassen innehåller en Worksheets samling som möjliggör åtkomst till kalkylblad i filen. Ett kalkylblad representeras av Worksheet klassen. Worksheet klassen tillhandahåller en Cells samling. Varje objekt i Cells samlingen representerar ett objekt av Cell klassen.
Cell klassen tillhandahåller flera metoder som låter utvecklare hämta värden från cellerna enligt deras datatyper. Dessa metoder inkluderar:
- GetStringValue, returnerar cellens strängvärde.
- GetDoubleValue, returnerar cellens dubbelvärde.
- GetBoolValue, returnerar cellens booleanvärde.
- GetDateTimeValue, returnerar cellens datum/tid-värde.
- GetFloatValue, returnerar cellens flytvärde.
- GetIntValue, returnerar cellens heltalsvärde.
När ett fält inte är ifyllt, kastar celler med GetDoubleValue eller GetFloatValue ett undantag.
Datatypen som finns i en cell kan också kontrolleras genom att använda Cell klassens GetType metod. I själva verket är Cell klassens GetType metod baserad på CellValueType uppräkningen vars fördefinierade värden listas nedan:
Cellvärdestyper | Beskrivning |
---|---|
CellValueType_IsBool | Specificerar att cellvärde är Boolean. |
CellValueType_IsDateTime | Specificerar att cellvärde är datum/tid. |
CellValueType_IsNull | Representerar en tom cell. |
CellValueType_IsNumeric | Specificerar att cellvärde är numeriskt. |
CellValueType_IsString | Specificerar att cellvärde är sträng. |
CellValueType_IsUnknown | Specificerar att cellvärde är okänt. |
Du kan också använda ovanstående fördefinierade cellvärdestyper för att jämföra med datatypen som finns i varje cell. |
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(); |