Working with Excel in Rust: Overview of Libraries and Code Examples
Rust is a systems programming language increasingly used for creating high-performance utilities, including for processing tabular data. Unlike Python, where pandas or openpyxl are the standard, the Rust ecosystem for working with Excel is smaller but offers several mature and fast solutions. In this article, we will cover three main libraries: rust_xlsxwriter (writing), calamine (reading), and xlsx (universal). You will learn how to read, create, and modify XLSX files using Rust.
Why Use Rust for Excel?
The main advantages of Rust when working with spreadsheets:
- Speed — compiled code processes millions of rows faster than interpreted alternatives.
- Safety — a strict type system and ownership model prevent memory leaks and access errors.
- Cross-platform — binaries run on Windows, Linux, macOS without dependencies.
- Small size — the final executable file can be less than 5 MB.
1. Writing Excel: rust_xlsxwriter
rust_xlsxwriter is a fork of the popular xlsxwriter library, adapted for Rust. It allows you to create XLSX files from scratch, supports formatting, cell merging, charts, and custom styles.
Installation:
// Cargo.toml[dependencies]rust_xlsxwriter = "0.64"Example: Creating a Simple Report
use rust_xlsxwriter::*;
fn main() -> Result<(), XlsxError> { let mut workbook = Workbook::new(); let sheet = workbook.add_worksheet();
// Header let header_format = Format::new() .set_bold() .set_font_color(Color::White) .set_background_color(Color::Blue);
sheet.write_string_with_format(0, 0, "Name", &header_format)?; sheet.write_string_with_format(0, 1, "Sales", &header_format)?; sheet.write_string_with_format(0, 2, "Date", &header_format)?;
// Data let data = vec![ ("Product A", 1500, "2024-01-15"), ("Product B", 2300, "2024-01-16"), ("Product C", 800, "2024-01-17"), ];
for (row, (name, sales, date)) in data.iter().enumerate() { sheet.write_string(row as u32 + 1, 0, name)?; sheet.write_number(row as u32 + 1, 1, *sales as f64)?; sheet.write_string(row as u32 + 1, 2, date)?; }
// Auto column width sheet.set_column_width(0, 15)?; sheet.set_column_width(1, 12)?; sheet.set_column_width(2, 14)?;
workbook.save("report.xlsx")?; println!("File report.xlsx created"); Ok(())}The library supports all basic types: numbers, strings, dates, formulas, hyperlinks. Fonts, fills, borders, and alignment are available for formatting.
2. Reading Excel: calamine
calamine is a fast parser for XLSX/XLS/XLSB/ODS files. It has no external dependencies and works for reading only. It is ideal for extracting data from large spreadsheets.
Installation:
[dependencies]calamine = "0.24"Example: Reading All Data from a Sheet
use calamine::{open_workbook, Reader, Xlsx};
fn main() -> Result<(), Box> { let path = "report.xlsx"; let mut workbook: Xlsx<_> = open_workbook(path)?;
// Get the first sheet if let Some(Ok(range)) = workbook.worksheet_range("Sheet1") { for row in range.rows() { for cell in row { // Output the cell value as a string print!("{} | ", cell.to_string()); } println!(); Also in library
Sending SMS in C++: LibCurl API Library and REST Requests
Working with CSV in Rust: the csv library and practical examples
Database Migrations in Node.js: Tool Overview and Examples