1. 生成excel
#include <xlnt/xlnt.hpp>
int main()
{
xlnt::workbook wb;
xlnt::worksheet ws = wb.active_sheet();
ws.cell("A1").value(5);
ws.cell("B2").value("string data");
ws.cell("C3").formula("=RAND()");
ws.merge_cells("C3:C4");
ws.freeze_panes("B2");
wb.save("example.xlsx");
return 0;
}
2. 读取指定表单
xlnt::workbook wb;
wb.load("example.xlsx");
xlnt::worksheet ws = wb.sheet("Sheet1");
std::cout << "正在读取工作表: " << ws.title() << std::endl;
3. 读取指定单元格
auto cell_value = ws.cell("A1").value();
auto cell_value = ws.cell(xlnt::cell_reference(1, 1)).value();
auto cell_value = ws.cell(1, 1).value();
4. 获取行数或列数
auto row_count = ws.rows();
auto column_count = ws.columns();
5. 读取指定范围
#include <xlnt/xlnt.hpp>
#include <iostream>
#include <vector>
int main() {
xlnt::workbook wb;
wb.load("example.xlsx");
auto ws = wb.active_sheet();
std::vector<std::string> row_data;
for (const auto& cell : ws.range("1:1")) {
row_data.push_back(cell.to_string());
}
for (const auto& data : row_data) {
std::cout << data << " ";
}
return 0;
}