Treballar amb cèl·lules i model de dades
Treballar amb cèl·lules i model de dades
El Cell class és la principal interfície per llegir i escriure valors de cèl·lules, fórmules i estils individuals. Accedeix a una cel·la mitjançant el sistema d’accessibilitat del full de treball. Cells col·lecció utilitzant una cadena d’adreces.
Escriure valors a cèl·lules
Utilització: Cell::PutValue per escriure text, números, booleans o dates en una cèl·lula:
#include "aspose/cells_foss/Workbook.h"
#include "aspose/cells_foss/Worksheet.h"
#include "aspose/cells_foss/Cell.h"
using namespace Aspose::Cells_FOSS;
int main() {
Workbook workbook;
Worksheet& sheet = workbook.GetWorksheets()[0];
sheet.GetCells()["A1"].PutValue("Product");
sheet.GetCells()["B1"].PutValue("Price");
sheet.GetCells()["A2"].PutValue("Widget");
sheet.GetCells()["B2"].PutValue(9.99);
sheet.GetCells()["C2"].PutValue(true);
workbook.Save("data.xlsx");
return 0;
}Ajust de fórmules
Utilització: Cell::SetFormula(formula) Les fórmules s’evaluen per Excel o lectors compatibles quan es obrir el fitxer.
sheet.GetCells()["B4"].SetFormula("=SUM(B2:B3)");
sheet.GetCells()["C4"].SetFormula("=AVERAGE(B2:B3)");Llegir valors de cèl·lula
Llegeix el valor actual d’una cèl·lula amb: Cell::GetValue(), que retorna un CellValue Convertir a un tipus específic utilitzant l’accesori adequat:
CellValue val = sheet.GetCells()["B2"].GetValue();
double price = val.AsDouble();
std::string text = sheet.GetCells()["A2"].GetStringValue();El CellValue la classe proporciona accessors tipats:
| Method | Tipus de retorn |
|---|---|
AsDouble() | double |
AsInteger() | int |
AsBool() | bool |
AsString() | std::string |
AsDateTime() | DateTime |
Accedir a cèl·lules per filas i columnes
A més de les cadenes d’adreces, accedeixen a cèl·lules per indicis de filas i columnes basats en zero:
// Address string
Cell& cellA1 = sheet.GetCells()["A1"];
// Row/column index (0-based)
int row = cellA1.GetRow();
int col = cellA1.GetColumn();Cèl·lules fusionades
Utilització: Cells::Merge per fusionar un interval rectangular de cel·les en una:
CellArea area = CellArea::CreateCellArea("A1", "C1");
sheet.GetCells().Merge(area.StartRow, area.StartColumn,
area.EndRow - area.StartRow + 1,
area.EndColumn - area.StartColumn + 1);Obtenir text de visualització
Per recuperar el valor de la cèl·lula com a text formatat (aplicant formats numèrics), utilitzar Cell::GetDisplayStringValue():
std::string display = sheet.GetCells()["B2"].GetDisplayStringValue();Consells i millors pràctiques
- Prefer
PutValuesobreSetValueper a tasques directesPutValueté sobrecargas per a tots els tipus primitius. - Utilització:
GetStringValue()per recuperar text sense conversió de tipus; usarGetValue().AsDouble()quan necessites precisió numèrica. - Sempre trucar.
SetFormulaper a cèl·lules de fórmula, noPutValue("=SUM(...)")aquest últim emmagatzemà una cadena literal, no una fórmula. - Utilització:
CellArea::CreateCellArea(start, end)per construir objectes de rangs per a operacions d’aglutinació i formatatge.
Problemàtiques comuns
| Issue | Cause | Correcció |
|---|---|---|
GetValue() torna buida. | No s’ha escrit cap cèl·lula. | Escriure un valor amb: PutValue abans de llegir. |
| Formula no avaluada | Fòrmula guardada com a cadena | Utilització: SetFormula(expr) en lloc de: PutValue(expr) |
| Fracàs de la fusió | Dimensions de rang invalides | Ensure endRow >= startRow i de la endColumn >= startColumn |
| Desacord de tipus en lectura | Llamant malament. As*() mètode | Ajusteu l’accessori amb l ‘original. PutValue tipus de |
Resum de referència d’API
| Classe/mètode | Description |
|---|---|
Cell::PutValue(value) | Escriure un valor literal a la cèl·lula. |
Cell::SetFormula(formula) | Configurar una cadena de fórmules d’Excel |
Cell::GetValue() | Obtindre el valor de la cèl·lula com a CellValue |
Cell::GetStringValue() | Obtindre el valor de la cèl·lula com a cadena . |
Cell::GetDisplayStringValue() | Recuperar la cadena d’exhibició formatada |
Cell::GetRow() | Obtindre l’índex de fila basat en zero |
Cell::GetColumn() | Obtindre l’índex de columna basat en zero |
Cell::GetStyle() | Obtindre l’estil actual de cèl·lula |
Cell::SetStyle(style) | Aplicar un estil modificat a la cel·la |
CellValue::AsDouble() | Convertir el valor de la cèl·lula al doble |
CellValue::AsString() | Convertir valor de cèl·lula en cadena |
Cells::Merge(row, col, rows, cols) | Fondre un rang rectangular |