Working with Excel in Rust: Libraries and Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Excel in Rust: Libraries and Code Examples

The Rust language, known for its performance and safety, is excellent for processing tabular data. If you need to read, create, or edit Excel files (formats .xlsx, .xls), the Rust ecosystem has several mature libraries. In this article, we will look at three main ones: calamine (reading), rust_xlsxwriter (writing), and xlsx (full cycle). You will learn how to install them and see working code examples.



calamine Library — Reading Excel Files

calamine is a fast and lightweight library for reading Excel files (xlsx, xls, xlsb, ods). It requires no external dependencies and works out of the box. It supports reading cells, sheets, formulas, and metadata.



Installation

[dependencies]calamine = "0.26"


Example: Reading Data from a File

use calamine::{open_workbook, Reader, Xlsx};

fn main() -> Result<(), Box<dyn std::error::Error>> { // Open the file let mut workbook: Xlsx<_> = open_workbook("data.xlsx")?;

// Get the sheet by name if let Some(Ok(range)) = workbook.worksheet_range("Sheet1") { for row in range.rows() { for cell in row { // Each cell is a DataType match cell { calamine::Data::String(s) => print!("{} ", s), calamine::Data::Float(f) => print!("{} ", f), calamine::Data::Int(i) => print!("{} ", i), _ => print!("? "), } } println!(); } }

Ok(())}


This code opens the file data.xlsx, reads the sheet Sheet1, and prints all cells row by row. calamine automatically determines the data type (string, number, date, etc.).



rust_xlsxwriter Library — Creating and Writing Excel

rust_xlsxwriter is a powerful library for creating xlsx files. It supports formatting, cell merging, charts, conditional formatting, and much more. It is a direct counterpart to the Python library xlsxwriter.



Installation

[dependencies]rust_xlsxwriter = "0.76"


Example: Creating a Simple Report

use rust_xlsxwriter::{Workbook, Format, Color};

fn main() -> Result<(), Box<dyn std::error::Error>> { let mut workbook = Workbook::new();

// Create a format for headers let header_format = Format::new() .set_bold() .set_font_color(Color::White) .set_background_color(Color::Blue);

// Add a sheet let sheet = workbook.add_worksheet();

// Write headers sheet.write_with_format(0, 0, "Name", &header_format)?; sheet.write_with_format(0, 1, "Age", &header_format)?; sheet.write_with_format(0, 2, "City", &header_format)?;

// Write data sheet.write(1, 0, "Anna")?; sheet.write(1, 1, 28)?; sheet.write(1, 2, "Moscow")?;

sheet.write(2, 0, "Ivan")?; sheet.write(2, 1, 35)?; sheet.write(2, 2, "Saint Petersburg")?;

// Save to file workbook.save("report.xlsx")?;

println!("File report.xlsx successfully created!"); Ok(())}


In this example, we create a file with formatted headers (bold text, white font on a blue background) and two rows of data. rust_xlsxwriter also supports charts, images, and pivot tables.



xlsx Library — Full Cycle (Read + Write)

xlsx is a universal library that allows both reading and creating xlsx files. It is less popular than

Recommendations