This package allows you to easily read and write simple Excel and CSV files. It is a Rust port of spatie/simple-excel.
CSV reads and writes stream row by row. XLSX writes use constant-memory worksheets backed by temporary files. Spreadsheet reads and ODS writes are not constant-memory. See the memory usage sections in the reading guide and writing guide.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Requires Rust 1.89 or newer.
cargo add spatie-simple-excelHere's an example on how to read an Excel or CSV file.
# fn main() -> Result<(), spatie_simple_excel::Error> {
# let path_to_file = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/header-and-rows.csv");
use spatie_simple_excel::SimpleExcelReader;
for row in SimpleExcelReader::create(path_to_file)?.get_rows()? {
let row = row?;
println!("{}", row["email"]);
# assert!(row["email"].as_str().unwrap().contains('@'));
}
# Ok(())
# }If the path ends with .csv a CSV file is assumed. If it ends with .xlsx, an Excel file is assumed. .ods files are supported too.
Here's how you can write a CSV file:
# fn main() -> Result<(), spatie_simple_excel::Error> {
# let directory = tempfile::tempdir()?;
# let path_to_csv = directory.path().join("export.csv");
use spatie_simple_excel::SimpleExcelWriter;
let mut writer = SimpleExcelWriter::create(&path_to_csv)?;
writer
.add_row([("first_name", "John"), ("last_name", "Doe")])?
.add_row([("first_name", "Jane"), ("last_name", "Doe")])?;
writer.close()?;
# assert_eq!(std::fs::read_to_string(path_to_csv)?, "\u{feff}first_name,last_name\nJohn,Doe\nJane,Doe\n");
# Ok(())
# }Process rows with Rust iterators. Call close() to observe write errors; destruction only attempts best-effort finalization.
See the documentation, reading guide and writing guide.
cargo test --workspace --locked --all-targets --all-features
cargo test --workspace --locked --doc --all-featuresSee all quality checks. The README and Markdown guides are tested by rustdoc, including setup on hidden # lines.
See CHANGELOG.md.
See CONTRIBUTING.md and the Rust API Guidelines.
Using an AI coding agent? Load Apollo's rust-best-practices skill when writing or reviewing code in this package.
Report vulnerabilities privately using the instructions in SECURITY.md.
- Freek Van der Herten
- Thomas Scharler, author of the upstream ODS engine
- All contributors
The MIT License (MIT). See LICENSE.md.
