Working with Excel in Rust: Library Overview and Code Examples
The Rust language, known for its performance and safety, provides excellent capabilities for working with Excel spreadsheets. In this article, we will look at three of the most popular libraries: rust_xlsxwriter, calamine, and xlsx. You will learn how to read, create, and modify .xlsx files, as well as how to apply advanced formatting features.
Why Rust for Working with Excel?
Rust is ideal for data processing tasks due to:
- Zero-cost abstractions — high speed when working with large files;
- Safe memory management — eliminating leaks and segmentation faults;
- Cross-platform compatibility — code runs on Windows, macOS, and Linux;
- Excellent integration with other systems via FFI.
The rust_xlsxwriter Library: Creating Excel Files
rust_xlsxwriter is a modern library for creating Excel files with rich formatting capabilities. It supports styles, cell merging, formulas, and conditional formatting.
Installation
Add to Cargo.toml:
[dependencies]rust_xlsxwriter = "0.77"Example: Creating a Formatted Report
Let's create a file with headers, data, and a formula:
use rust_xlsxwriter::*;
fn main() -> Result<(), XlsxError> { let mut workbook = Workbook::new(); let worksheet = workbook.add_worksheet();
// Create styles let header_format = Format::new() .set_bold() .set_font_color(Color::White) .set_background_color(Color::Blue) .set_border(FormatBorder::Thin);
let money_format = Format::new() .set_num_format("$#,##0.00") .set_border(FormatBorder::Thin);
// Headers worksheet.write_with_format(0, 0, "Product", &header_format)?; worksheet.write_with_format(0, 1, "Quantity", &header_format)?; worksheet.write_with_format(0, 2, "Price", &header_format)?; worksheet.write_with_format(0, 3, "Total", &header_format)?;
// Data let items = vec![ ("Laptop", 5, 1200.00), ("Mouse", 10, 25.50), ("Keyboard", 7, 45.00), ];
for (i, (name, qty, price)) in items.iter().enumerate() { let row = i as u32 + 1; worksheet.write(row, 0, name)?; worksheet.write(row, 1, *qty)?; worksheet.write_with_format(row, 2, *price, &money_format)?; // Formula: quantity * price worksheet.write_formula(row, 3, &format!("B{}*C{}", row+1, row+1))?; }
// Auto-width columns worksheet.set_column_width(0, 15)?; worksheet.set_column_width(1, 12)?; worksheet.set_column_width(2, 10)?; worksheet.set_column_width(3, 12)?;
workbook.save("report.xlsx")?; println!("File report.xlsx created!"); Ok(())}The calamine Library: Reading Excel Files
calamine is a fast parser for reading Excel files (.xlsx, .xls, .xlsb). It does not require Excel to be installed and works entirely in memory.
Installation
[dependencies]calamine = "0.24"Example: Reading Data from a File
Let's read the previously created file and output the data to the console:
use calamine::{open_workbook, Reader, Xlsx};
fn main() -> Result<(), Box> { let path = "report.xlsx"; let mut workbook: Xlsx<_> = open_workbook(path)?;
if let Some(Ok(range)) = workbook.worksheet_range("Sheet1") { println!("Contents of file {}:", path); println!("{:-<40}", "");
for row in range.rows() { for cell in row { match cell { calamine::Data::Stri