Skip to content

XLSX 셀에 값 쓰기

set_cell(sheet_index, cell_ref, value)는 0 기반 시트 인덱스와 표준 A1 셀 참조(A1, B12, AA42)로 워크시트에 값을 씁니다. Office Oxide는 문자열, 숫자, 불리언, 빈 값을 지원합니다.

워크시트의 나머지 요소——서식, 셀 병합, 조건부 서식, 이름 있는 범위, 차트——는 그대로 유지됩니다.

기본 타입 쓰기

Rust

use office_oxide::edit::EditableDocument;
use office_oxide::xlsx::edit::CellValue;

let mut wb = EditableDocument::open("budget.xlsx")?;
wb.set_cell(0, "A1", CellValue::String("Total".into()))?;
wb.set_cell(0, "B1", CellValue::Number(42.5))?;
wb.set_cell(0, "C1", CellValue::Boolean(true))?;
wb.set_cell(0, "D1", CellValue::Empty)?;
wb.save("budget.xlsx")?;

Python

from office_oxide import EditableDocument

with EditableDocument.open("budget.xlsx") as ed:
    ed.set_cell(0, "A1", "Total")     # string
    ed.set_cell(0, "B1", 42.5)        # number (int also accepted)
    ed.set_cell(0, "C1", True)        # boolean
    ed.set_cell(0, "D1", None)        # empty
    ed.save("budget.xlsx")

JavaScript

import { EditableDocument } from 'office-oxide';

using wb = EditableDocument.open('budget.xlsx');

wb.setCell(0, 'A1', 'Total');   // string
wb.setCell(0, 'B1', 42.5);      // number
wb.setCell(0, 'C1', true);      // boolean
wb.setCell(0, 'D1', null);      // empty

wb.save('budget.xlsx');

Go

ed, _ := officeoxide.OpenEditable("budget.xlsx")
defer ed.Close()

ed.SetCell(0, "A1", officeoxide.NewStringCell("Total"))
ed.SetCell(0, "B1", officeoxide.NewNumberCell(42.5))
ed.SetCell(0, "C1", officeoxide.NewBoolCell(true))
ed.SetCell(0, "D1", officeoxide.NewEmptyCell())

ed.Save("budget.xlsx")

C#

using var wb = EditableDocument.Open("budget.xlsx");

wb.SetCell(0u, "A1", "Total");        // string overload
wb.SetCell(0u, "B1", 42.5);           // double overload
wb.SetCell(0u, "C1", true);           // bool overload
wb.SetCellEmpty(0u, "D1");            // clear a cell

wb.Save("budget.xlsx");

C

int err = 0;
OfficeEditableHandle *wb = office_editable_open("budget.xlsx", &err);

/* value_type: OFFICE_CELL_EMPTY|STRING|NUMBER|BOOLEAN; value_str only for STRING */
office_editable_set_cell(wb, 0, "A1", OFFICE_CELL_STRING,  "Total", 0.0,  &err);
office_editable_set_cell(wb, 0, "B1", OFFICE_CELL_NUMBER,  NULL,    42.5, &err);
office_editable_set_cell(wb, 0, "C1", OFFICE_CELL_BOOLEAN, NULL,    1.0,  &err);  /* nonzero = true */
office_editable_set_cell(wb, 0, "D1", OFFICE_CELL_EMPTY,   NULL,    0.0,  &err);

office_editable_save(wb, "budget.xlsx", &err);
office_editable_free(wb);

일괄 업데이트

여러 번의 쓰기 작업을 한 번의 열기/저장 사이클로 묶어 처리합니다.

Rust

let rows = [
    ("Acme",    120_000.0, true),
    ("Globex",   85_000.0, false),
    ("Initech",  62_500.0, true),
];

let mut ed = EditableDocument::open("dashboard.xlsx")?;
for (i, (name, revenue, active)) in rows.iter().enumerate() {
    let row = i + 2;
    ed.set_cell(0, &format!("A{row}"), CellValue::String((*name).into()))?;
    ed.set_cell(0, &format!("B{row}"), CellValue::Number(*revenue))?;
    ed.set_cell(0, &format!("C{row}"), CellValue::Boolean(*active))?;
}
ed.save("dashboard.xlsx")?;

Python

rows = [
    ("Acme",    120_000, True),
    ("Globex",   85_000, False),
    ("Initech",  62_500, True),
]

with EditableDocument.open("dashboard.xlsx") as ed:
    for i, (name, revenue, active) in enumerate(rows):
        row = i + 2  # leave row 1 for headers
        ed.set_cell(0, f"A{row}", name)
        ed.set_cell(0, f"B{row}", revenue)
        ed.set_cell(0, f"C{row}", active)
    ed.save("dashboard.xlsx")

C

const char *names[]   = { "Acme",   "Globex", "Initech" };
double      revenue[] = { 120000.0,  85000.0,  62500.0  };
int         active[]  = { 1,         0,        1        };

int err = 0;
OfficeEditableHandle *ed = office_editable_open("dashboard.xlsx", &err);
for (int i = 0; i < 3; i++) {
    char a[8], b[8], c[8];
    int row = i + 2;  /* leave row 1 for headers */
    snprintf(a, sizeof a, "A%d", row);
    snprintf(b, sizeof b, "B%d", row);
    snprintf(c, sizeof c, "C%d", row);
    office_editable_set_cell(ed, 0, a, OFFICE_CELL_STRING,  names[i], 0.0,        &err);
    office_editable_set_cell(ed, 0, b, OFFICE_CELL_NUMBER,  NULL,     revenue[i], &err);
    office_editable_set_cell(ed, 0, c, OFFICE_CELL_BOOLEAN, NULL,     active[i],  &err);
}
office_editable_save(ed, "dashboard.xlsx", &err);
office_editable_free(ed);

다른 시트 대상으로 지정하기

sheet_index는 워크북 내에서 0부터 시작하는 순서 위치이지, 시트 이름이 아닙니다. 이름으로 인덱스를 찾으려면 먼저 워크북을 읽어야 합니다.

Python

from office_oxide import Document, EditableDocument

with Document.open("budget.xlsx") as doc:
    sheet_names = [s.name() for s in doc.as_xlsx().sheets()]
print(sheet_names)   # ['Summary', 'Q1', 'Q2', 'Q3', 'Q4']

idx = sheet_names.index("Q3")
with EditableDocument.open("budget.xlsx") as ed:
    ed.set_cell(idx, "B5", 42_000)
    ed.save("budget.xlsx")

set_cell이 건드리는 것과 건드리지 않는 것

set_cell은 셀의 <v> 값과 <t> 타입을 씁니다. 다음은 하지 않습니다:

  • 수식 재계산. 재계산을 트리거하려면 Excel에서 파일을 열거나 별도의 계산 엔진을 사용하세요.
  • 새 문자열 추가를 제외한 shared-strings 테이블 수정. 기존 문자열은 계속 공유된 상태를 유지합니다.
  • 셀 서식, 조건부 서식, 이름 있는 범위 변경.

대상 셀에 수식이 있으면 set_cell이 수식을 정적 값으로 덮어씁니다. 수식을 명시적으로 쓰려면 형식별 xlsx::edit API를 사용하세요.

오류

증상 원인
OfficeError::Sheet(idx) (Rust) / IndexError (Python) sheet_index가 워크북의 시트 수 이상
OfficeError::CellRef("...") 셀 참조가 유효한 A1 표기가 아님
Number(value) 쓰기 후 Excel에서 셀이 비어 보임 해당 셀이 이전에 텍스트로 서식이 지정됨 — Excel에서 서식을 지우거나 형식별 API로 쓰세요

더 보기