Skip to content

About

Read and write simple Excel and CSV files

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Read and write simple Excel and CSV files

Latest version on crates.io Tests License Total downloads

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.

Support us

Support Spatie's open source work

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.

Installation

Requires Rust 1.89 or newer.

cargo add spatie-simple-excel

Usage

Here'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.

Testing

cargo test --workspace --locked --all-targets --all-features
cargo test --workspace --locked --doc --all-features

See all quality checks. The README and Markdown guides are tested by rustdoc, including setup on hidden # lines.

Changelog

See CHANGELOG.md.

Contributing

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.

Security Vulnerabilities

Report vulnerabilities privately using the instructions in SECURITY.md.

Credits

License

The MIT License (MIT). See LICENSE.md.

About

Read and write simple Excel and CSV files

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages