
xlsxwriter-rs
Rust Library for Excel XLSX Generation
Create Fully Compatible Excel XLSX Files with Formatting, Merged Cells, Autofilters & Images via Rust API.
What is xlsxwriter-rs?
xlsxwriter-rs is a high-performance Rust binding for the widely used libxlsxwriter C library, enabling developers to generate fully compatible Excel XLSX files directly from Rust applications. It supports comprehensive Excel formatting, including fonts, colors, alignment, borders, and number formats, as well as advanced features like merged cells, autofilters, data validation, and embedded PNG/JPEG images. Designed for reliability and speed, xlsxwriter-rs is ideal for server-side report generation, data export pipelines, and any scenario requiring programmatic Excel file creation in Rust.
The library leverages Rust’s memory safety and zero-cost abstractions to provide a safe and efficient API for Excel generation without requiring Microsoft Excel or external dependencies like COM. It integrates seamlessly with Cargo and supports optional feature flags for customizing behavior, such as disabling MD5 checksums or using system zlib. With its clean, idiomatic Rust interface and comprehensive documentation, xlsxwriter-rs empowers developers to build robust, cross-platform Excel generation tools while maintaining full control over file structure and content.
Getting Started with xlsxwriter-rs
To begin using xlsxwriter-rs, add it to your Cargo.toml via cargo add xlsxwriter or manually include [dependencies] with xlsxwriter = "0.1". The library requires LLVM and Clang for bindgen compilation. For Windows, install Visual Studio and LLVM, then set LIBCLANG_PATH to the LLVM bin directory. After setup, clone the repository, run git submodule update --init --recursive to fetch the C library, and build examples with cargo run --example hello_spreadsheet. Full documentation and examples are available on the xlsxwriter-rs GitHub.
Install xlsxwriter-rs via Cargo
cargo add xlsxwriterFull Excel Formatting Support
xlsxwriter-rs provides comprehensive formatting capabilities, allowing developers to customize fonts, colors, borders, alignment, and number formats for individual cells or ranges. You can apply bold, italic, underline, font size, and custom colors to text, set background fill colors, define cell borders with various styles (thin, medium, thick), and control horizontal/vertical alignment. This level of control ensures generated Excel files match professional reporting standards. Formatting is applied using the Format struct, which supports method chaining for intuitive configuration. This feature is essential for creating visually distinct reports, dashboards, and data exports that improve readability and user experience.
Apply Font Color and Alignment to Cells
let mut sheet1 = workbook.add_worksheet(None)?;
sheet1.write_string(
0, 0,
"Red text",
Some(&Format::new().set_font_color(FormatColor::Red))
)?;
sheet1.write_string(
2, 0,
"Centered green text",
Some(&Format::new()
.set_font_color(FormatColor::Green)
.set_align(FormatAlignment::CenterAcross)
.set_vertical_align(FormatVerticalAlignment::VerticalCenter)
)
)?;Merged Cells and Ranges
The library supports merging rectangular ranges of cells, which is critical for creating headers, titles, and grouped data sections in Excel reports. Using the merge_range method, you can span multiple rows and columns while applying formatting to the entire merged block. This ensures consistent styling and layout across complex spreadsheets. Merged cells are fully compatible with Excel’s rendering engine and preserve formatting such as text alignment, borders, and background colors. This feature is especially useful for financial statements, dashboards, and forms where visual grouping enhances data comprehension and presentation quality.
Merge a Range with Custom Formatting
sheet1.merge_range(
2, 0,
3, 2,
"Hello, world",
Some(&Format::new()
.set_font_color(FormatColor::Green)
.set_align(FormatAlignment::CenterAcross)
.set_vertical_align(FormatVerticalAlignment::VerticalCenter)
)
)?;Autofilters and Data Validation
xlsxwriter-rs enables programmatic creation of autofilters and data validation rules, allowing users to sort, filter, and validate data directly in Excel. You can apply an autofilter to a range of cells using set_autofilter, and define drop-down lists for user input via data validation. This supports list-based validation, numeric ranges, dates, and custom formulas. These features enhance interactivity and data integrity in spreadsheets, making them suitable for forms, templates, and collaborative data entry. The generated XLSX files retain full functionality when opened in Excel, LibreOffice, or Google Sheets, ensuring broad compatibility.
Apply Autofilter and Data Validation
// Apply autofilter to A1:C10
sheet1.set_autofilter("A1:C10")?;
// Add data validation (drop-down list) in B2
sheet1.data_validation(
"B2",
DataValidation::new_list(&["Option1", "Option2", "Option3"])
)?;Embed Images and Hyperlinks
You can embed PNG and JPEG images directly into Excel worksheets using xlsxwriter-rs, supporting positioning, sizing, and scaling. This is ideal for adding logos, charts, or visual indicators to reports. Additionally, the library supports hyperlinks and URLs with customizable formatting (e.g., blue text and underlines), enabling clickable links to websites, documents, or other sheets. These capabilities make xlsxwriter-rs suitable for creating rich, interactive reports and dashboards that combine data, visuals, and navigation. All embedded content is fully compatible with Excel’s rendering engine and preserves layout integrity across platforms.
Write URL and Embed Image
// Write a clickable URL
sheet1.write_url(
1, 1,
"https://github.com/informationsea/xlsxwriter-rs",
Some(&Format::new()
.set_font_color(FormatColor::Blue)
.set_underline(FormatUnderline::Single)
)
)?;
// Embed an image (requires image path)
// sheet1.insert_image(5, 0, "logo.png")?;