Skip to content
项目

MathNya/umya-spreadsheet

在 GitHub 查看

关于

用于读取和写入电子表格文件的纯 Rust 库。

效率与自动化

这些数字如何统计

实测星标增长

24小时
-1
7天
-1
15天
+1
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

umya-spreadsheet

Result Image

Crates.io Github CI Crates.io GitHub Sponsor

Description

umya-spreadsheet is a library written in pure Rust to read and write xlsx file.

Version 4.0.0-beta.1 has been released

With the update to zip, the error types have been updated. We plan to update the section on error handling again later. For this reason, we are releasing this as a beta version for now.

Usage

Installation

Add the following code to Cargo.toml

[dependencies]
umya-spreadsheet = "3.1.1"

# WebAssembly support
umya-spreadsheet = { version = "3.1.1", features = ["js"] }

# Use only png for image processing
umya-spreadsheet = { version = "3.1.1", features = ["image/png"] }

Add the following code to main.rs

extern crate umya_spreadsheet;

Read file

let path = std::path::Path::new("./tests/test_files/aaa.xlsx");
let mut book = umya_spreadsheet::reader::xlsx::read(path).unwrap();

Read file (Lazy)

Delays the loading of the worksheet until it is needed.
When loading a file with a large amount of data, response improvement can be expected.

let path = std::path::Path::new("./tests/test_files/aaa.xlsx");
let mut book = umya_spreadsheet::reader::xlsx::lazy_read(path).unwrap();

New file

let mut book = umya_spreadsheet::new_file();

Print margins

PageMargins::has_left() (and has_right, has_top, has_bottom, has_header, has_footer) distinguishes an unset margin from an explicitly supplied zero. Writing preserves explicit values and uses Excel defaults only for unset margins.

Write file

let path = std::path::Path::new("./tests/result_files/bbb.xlsx");
let _unused =  umya_spreadsheet::writer::xlsx::write(&book, path);

Write file with password

let path = std::path::Path::new("./tests/result_files/bbb.xlsx");
let _unused =  umya_spreadsheet::writer::xlsx::write_with_password(&book, path, "password");
let from_path = std::path::Path::new("./tests/test_files/aaa.xlsx");
let to_path = std::path::Path::new("./tests/result_files/bbb.xlsx");
let _unused =  umya_spreadsheet::writer::xlsx::set_password(&from_path, &to_path, "password");

Read Value

let mut book = umya_spreadsheet::new_file();
book.sheet_by_name("Sheet1").unwrap().cell("A1").value();
book.sheet_by_name("Sheet1").unwrap().cell((1, 1)).value();
book.sheet_by_name("Sheet1").unwrap().cell((1, 1)).value();
book.sheet_mut(&0).unwrap().cell((1, 1)).value();

Change Value

let mut book = umya_spreadsheet::new_file();
book.sheet_by_name_mut("Sheet1").unwrap().cell_mut("A1").set_value("TEST1");
book.sheet_mut(&0).unwrap().cell_mut("A1").set_value("TEST2");

Move Values

let range = "A1:A3";
let row = 10;
let column = 2;
book.sheet_by_name_mut("Sheet1").unwrap().move_range(range, &row, &column);

Change Style

let mut book = umya_spreadsheet::new_file();
let mut style = book.sheet_by_name_mut("Sheet1").unwrap().style_mut("A1");
// fill color on red.
style.set_background_color(umya_spreadsheet::Color::COLOR_RED);

New Chart

let mut book = umya_spreadsheet::new_file();
// Add Chart
let mut from_marker = umya_spreadsheet::structs::drawing::spreadsheet::MarkerType::default();
from_marker.set_coordinate("C1");
let mut to_marker = umya_spreadsheet::structs::drawing::spreadsheet::MarkerType::default();
to_marker.set_coordinate("D11");
let area_chart_series_list = vec![
    "Sheet1!$A$1:$A$10",
    "Sheet1!$B$1:$B$10",
];
let mut chart = umya_spreadsheet::structs::Chart::default();
chart.new_chart(
    umya_spreadsheet::structs::ChartType::LineChart,
    from_marker,
    to_marker,
    area_chart_series_list,
);
book.sheet_by_name_mut("Sheet1").unwrap()
    .add_chart(chart);

Struct

Pass the book as a Workbook to modify it in other functions.


let mut book = umya_spreadsheet::new_file();
let _unused =  book.new_sheet("Sheet2");
update_excel(&mut book);

fn update_excel(book:  &mut Workbook) {
   book.sheet_by_name_mut("Sheet2").unwrap().cell_mut("A1").set_value("Test"); 
}

See the next chapter for implementation status and more detailed usage.

Support Status

Function detail example
file reader xlsx, xlsm here.
file lazy_reader xlsx, xlsm here.
file writer xlsx, xlsm here.
csv here.
file writer with password xlsx, xlsm here.
worksheet read, new, copy here.
cell value read, edit, formatted value. here.
cell style read, edit here.
columns read, edit, auto width here.
rows read, edit
charts read, edit here.
drawings read, edit(Still might be inconvenient.)
images read, edit here.
ole objects read, edit(Still might be inconvenient.)

License

MIT

Contributing

Contributions by way of pull requests are welcome! Please make sure your code uses:

  • cargo fmt for formatting
  • clippy
cargo +nightly fmt --all
cargo clippy --all-features --all-targets -- -D warnings

Acknowledgements

These are the people who have supported the umya-spreadsheet project. We would like to express our heartfelt gratitude to them.

8191, agentjill, AlexZhu2001, attila-lin, bachhh, ben-schreiber, boseongkim32, c-git, colin-kiegel, cstkingkey, DaimoniX, developer0hye, fabianboesiger, ggodlewski, gitter-badger, hackers267, HiroyukiFuruno, iancormac84, jimchan3301, jmbrunskill, john-dc252, jon-convexent, JunWang-Lin, kazuk, keithadler, kjh618, KyGost, lanej, LeandroMarceddu, LuisFigueroaG, matt-duch, mxsrm, nikvoid, oxabz, patrickomatic, piaoger, popen2, PSU3D0, RoloEdits, Sachin-Bhat, SamuelMarks, schungx, sentinelt, sjfhsjfh, tomgroenwoldt, tzfun, ubamrein, usagi, verticka, vonkruel, WilliamTCarroll, wolfiesch, xamgore, yerlibilgin, yoonghm, zjhsd2007

Projects using umya-spreadsheet

Community projects built with umya-spreadsheet (not endorsements).

  • spreadsheet-mcp - MCP server for spreadsheet analysis/editing (xlsx/xlsm via umya-spreadsheet)
  • formualizer - Arrow-backed spreadsheet engine and formula parser with excel parity (xlsx/xlsm via umya-spreadsheet)

探索相关项目

加载中…