设置 XLSX 单元格
set_cell(sheet_index, cell_ref, value) 以 0 起始的工作表索引和标准 A1 单元格引用(A1、B12、AA42)向工作表写入值。Office Oxide 接受字符串、数字、布尔和空。
周围工作表的内容 — 格式、合并、条件格式、命名区域、图表 — 原样保留。
写入基本类型
Python
from office_oxide import EditableDocument
with EditableDocument.open("budget.xlsx") as ed:
ed.set_cell(0, "A1", "Total") # 字符串
ed.set_cell(0, "B1", 42.5) # 数字(int 也可)
ed.set_cell(0, "C1", True) # 布尔
ed.set_cell(0, "D1", None) # 空
ed.save("budget.xlsx")
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")?;
JavaScript
import { EditableDocument } from 'office-oxide';
using wb = EditableDocument.open('budget.xlsx');
wb.setCell(0, 'A1', 'Total'); // 字符串
wb.setCell(0, 'B1', 42.5); // 数字
wb.setCell(0, 'C1', true); // 布尔
wb.setCell(0, 'D1', null); // 空
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"); // 字符串重载
wb.SetCell(0u, "B1", 42.5); // double 重载
wb.SetCell(0u, "C1", true); // bool 重载
wb.SetCellEmpty(0u, "D1"); // 清空
wb.Save("budget.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 # 第 1 行留给表头
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")
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")?;
定位其他工作表
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 写入 |
相关链接
- 在 DOCX/PPTX 中替换文本
- 编辑概览
- 从 openpyxl 迁移 — 同样的 XLSX 覆盖,快得多