
rust_xlsxwriter
Open Source Rust Library for Excel XLSX Files
Generate Excel 2007+ XLSX Files with Full Formatting, Charts, Formulas & Images via Rust API.
What is rust_xlsxwriter?
rust_xlsxwriter is a high-performance, pure Rust crate for generating Excel XLSX files without requiring Excel or external dependencies. It enables developers to programmatically create spreadsheets with text, numbers, dates, formulas, charts, images, merged cells, conditional formatting, data validation, and more. Designed for reliability and speed, it closely matches Excel’s native file format and supports constant memory mode for large datasets. The library is a faithful Rust rewrite of the popular Python XlsxWriter library, with idiomatic APIs and comprehensive documentation.
rust_xlsxwriter focuses exclusively on writing Excel files—no reading or modification of existing files. It supports modern Excel features like dynamic arrays, sparklines, checkboxes, textboxes, tables, autofilters, defined names, and workbook themes. With optional features like Serde serialization, chrono/jiff date support, and Polars integration, it adapts to diverse Rust ecosystems. Its performance is second only to the C version, delivering 3.8× faster execution than Python’s XlsxWriter. For full details, see the rust_xlsxwriter GitHub repository.
Getting Started with rust_xlsxwriter
rust_xlsxwriter is available on crates.io and can be installed via `cargo add rust_xlsxwriter`. For enhanced functionality, enable optional features like `constant_memory`, `chrono`, `serde`, or `polars`. The library requires Rust 1.56+ and compiles to native binaries with no external runtime dependencies beyond the `zip` crate (by default). It is fully documented with a user guide, tutorial, and cookbook. For detailed examples and source code, visit the rust_xlsxwriter GitHub repository.
Install rust_xlsxwriter via Cargo
cargo add rust_xlsxwriterWrite Formatted Cells with Text, Numbers & Dates
rust_xlsxwriter provides a simple and intuitive API to write formatted content to Excel cells. Developers can apply custom formats for numbers, dates, fonts, borders, alignment, and colors using the `Format` struct. The library supports writing plain text, numeric values, dates, formulas, and URLs with optional formatting. It also supports rich multi-format strings for mixed formatting within a single cell. This makes it ideal for generating reports, dashboards, and data exports where visual clarity and structure are essential. The API ensures high fidelity with Excel’s native formatting engine.
How to Write Formatted Text, Numbers & Dates via Rust?
use rust_xlsxwriter::*;
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
let bold = Format::new().set_bold();
let decimal = Format::new().set_num_format("0.00");
let date_fmt = Format::new().set_num_format("yyyy-mm-dd");
worksheet.write_with_format(0, 0, "Hello", &bold)?;
worksheet.write(1, 0, 123.456)?;
worksheet.write_with_format(2, 0, 789.012, &decimal)?;
let date = ExcelDateTime::from_ymd(2023, 12, 25)?;
worksheet.write_with_format(3, 0, &date, &date_fmt)?;
workbook.save("formatted.xlsx")?;Create Charts, Sparklines & Images
rust_xlsxwriter supports generating professional charts, sparklines, and embedding images directly into Excel files. Developers can create bar, line, pie, scatter, and area charts with customizable axes, legends, and data series. Sparklines provide compact trend visualization within cells. Images (PNG, JPEG, GIF, BMP) can be inserted and positioned with scaling and anchoring options. These visual elements enhance data storytelling and reporting capabilities. The library ensures compatibility with Excel 2007+ and preserves rendering fidelity across platforms.
How to Add a Chart & Image to Excel via Rust?
use rust_xlsxwriter::*;
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
// Write sample data
for i in 0..5 {
worksheet.write_number(i, 0, (i + 1) as f64 * 10.0)?;
}
// Create a chart
let mut chart = Chart::new(ChartType::Column);
chart.add_series().set_values("Sheet1!$A$1:$A$5");
worksheet.insert_chart(0, 2, &chart)?;
// Insert an image
let image = Image::new("logo.png")?;
worksheet.insert_image(5, 2, &image)?;
workbook.save("chart_image.xlsx")?;Advanced Formatting: Merged Cells, Tables & Conditional Formatting
rust_xlsxwriter enables advanced Excel formatting features such as merged ranges, structured tables, autofilters, and conditional formatting rules. Developers can merge cells for headers or labels, apply table styles with banded rows/columns, and define filters for user interactivity. Conditional formatting supports color scales, data bars, icon sets, and custom formulas to highlight trends or anomalies. These features improve data readability and interactivity in generated workbooks. The API abstracts complex XML structures into intuitive Rust methods, ensuring maintainability and type safety.
How to Merge Cells & Add Tables with Conditional Formatting?
use rust_xlsxwriter::*;
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
// Merge cells
let merge_fmt = Format::new()
.set_border(FormatBorder::Thin)
.set_align(FormatAlign::Center);
worksheet.merge_range(0, 0, 0, 2, "Report Header", &merge_fmt)?;
// Add table
worksheet.add_table(1, 0, 5, 2, Table::new().set_columns(&[
("Item", None),
("Value", None),
("Status", None),
]))?;
// Conditional formatting
worksheet.conditional_format(
2, 0, 5, 2,
ConditionalFormat::new()
.set_type(ConditionalFormatType::Cell)
.set_format(&Format::new().set_bg_color("#D8E4BC"))
.set_formula("=A2>100")
)?;
workbook.save("advanced.xlsx")?;Performance Optimization & Large File Support
rust_xlsxwriter is optimized for speed and memory efficiency, especially for large datasets. It supports constant memory mode (`constant_memory` feature), which writes data incrementally to disk, minimizing heap usage. This mode is ideal for generating multi-megabyte Excel files without OOM errors. Benchmarks show it runs ~3.8× faster than Python’s XlsxWriter and only ~14% slower than the native C library. Optional features like `zmij` and `zlib` further accelerate numeric writes and compression. These capabilities make it suitable for batch reporting, ETL pipelines, and server-side Excel generation.
How to Enable Constant Memory Mode for Large Files?
cargo add rust_xlsxwriter -F constant_memoryuse rust_xlsxwriter::*;
let mut workbook = Workbook::new();
workbook.set_constant_memory(true); // Enable constant memory mode
let worksheet = workbook.add_worksheet();
for i in 0..100_000 {
worksheet.write_number(i, 0, i as f64)?;
}
workbook.save("large_file.xlsx")?;