셀 및 데이터 모델 작업
셀 및 데이터 모델 작업
Cell 클래스는 개별 스프레드시트 셀 값, 수식 및 스타일을 읽고 쓰기 위한 기본 인터페이스입니다. 주소 문자열을 사용하여 워크시트의 Cells 컬렉션을 통해 셀에 접근합니다.
셀에 값 쓰기
Cell::PutValue를 사용하여 셀에 텍스트, 숫자, 불리언 또는 날짜를 기록합니다:
#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;
}수식 설정
Cell::SetFormula(formula)를 사용하여 Excel 호환 수식을 작성합니다. 파일을 열 때 Excel 또는 호환 리더에 의해 수식이 평가됩니다.
sheet.GetCells()["B4"].SetFormula("=SUM(B2:B3)");
sheet.GetCells()["C4"].SetFormula("=AVERAGE(B2:B3)");셀 값 읽기
Cell::GetValue()를 사용하여 셀의 현재 값을 읽으면 CellValue 객체가 반환됩니다. 적절한 접근자를 사용하여 특정 유형으로 변환합니다:
CellValue val = sheet.GetCells()["B2"].GetValue();
double price = val.AsDouble();
std::string text = sheet.GetCells()["A2"].GetStringValue();CellValue 클래스는 타입이 지정된 접근자를 제공합니다:
| 메서드 | 반환 타입 |
|---|---|
AsDouble() | double |
AsInteger() | int |
AsBool() | bool |
AsString() | std::string |
AsDateTime() | DateTime |
행 및 열을 기준으로 셀에 접근하기
주소 문자열 외에도, 0부터 시작하는 행 및 열 인덱스로 셀에 접근할 수 있습니다:
// Address string
Cell& cellA1 = sheet.GetCells()["A1"];
// Row/column index (0-based)
int row = cellA1.GetRow();
int col = cellA1.GetColumn();셀 병합
Cells::Merge를 사용하여 직사각형 범위의 셀을 하나로 병합합니다:
CellArea area = CellArea::CreateCellArea("A1", "C1");
sheet.GetCells().Merge(area.StartRow, area.StartColumn,
area.EndRow - area.StartRow + 1,
area.EndColumn - area.StartColumn + 1);표시 텍스트 가져오기
셀 값을 형식이 적용된 표시 텍스트(숫자 서식 적용)로 가져오려면 Cell::GetDisplayStringValue()를 사용합니다:
std::string display = sheet.GetCells()["B2"].GetDisplayStringValue();팁 및 모범 사례
- 직접 리터럴 할당에는
PutValue을(를)SetValue보다 선호하세요 —PutValue는 모든 기본 형식에 대한 오버로드를 제공합니다. - 형 변환 없이 텍스트를 가져오려면
GetStringValue()을(를) 사용하고; 숫자 정밀도가 필요할 때는GetValue().AsDouble()을(를) 사용하세요. - 수식 셀에는 항상
SetFormula을(를) 호출하고PutValue("=SUM(...)")을(를) 사용하지 마세요 — 후자는 수식이 아니라 문자열 리터럴을 저장합니다. - 병합 및 서식 작업을 위한 범위 객체를 만들려면
CellArea::CreateCellArea(start, end)을(를) 사용하세요.
일반적인 문제
| 문제 | 원인 | 수정 |
|---|---|---|
GetValue()가 빈 값을 반환합니다 | 셀에 기록되지 않았습니다 | 읽기 전에 PutValue로 값을 기록하십시오 |
| 수식이 평가되지 않음 | 수식이 문자열로 저장됨 | PutValue(expr) 대신 SetFormula(expr)을(를) 사용하십시오 |
| 병합 실패 | 잘못된 범위 차원 | endRow >= startRow와 endColumn >= startColumn을(를) 확인하십시오 |
| 읽기 시 타입 불일치 | 잘못된 As*() 메서드 호출 | 액세서를 원래 PutValue 유형에 맞추세요 |
API Reference 요약
| 클래스/메서드 | 설명 |
|---|---|
Cell::PutValue(value) | 셀에 리터럴 값을 기록하세요 |
Cell::SetFormula(formula) | Excel 수식 문자열을 설정합니다 |
Cell::GetValue() | 셀 값을 CellValue로 가져옵니다 |
Cell::GetStringValue() | 셀 값을 문자열로 가져옵니다 |
Cell::GetDisplayStringValue() | 형식이 지정된 표시 문자열을 가져옵니다 |
Cell::GetRow() | 0 기반 행 인덱스를 가져옵니다 |
Cell::GetColumn() | 0 기반 열 인덱스를 가져옵니다 |
Cell::GetStyle() | 현재 셀 스타일을 가져옵니다 |
Cell::SetStyle(style) | 셀에 수정된 스타일을 적용합니다 |
CellValue::AsDouble() | 셀 값을 double로 변환 |
CellValue::AsString() | 셀 값을 문자열로 변환 |
Cells::Merge(row, col, rows, cols) | 직사각형 범위를 병합 |