A fast and lightweight Rust library for parsing Excel (.xlsx) files.
- Parse Excel (.xlsx) files into structured data models
- Export Excel files to JSON format
- Support for multiple sheets in a single workbook
- Cell value types: String, Number, Boolean, Date, and Empty
- Basic cell styling support
- Shared strings parsing
- Worksheet parsing with cell and row-based access
Add this to your Cargo.toml file:
[dependencies]
rlsx = "0.1.0"use std::path::Path;
use rlsx::parse_xlsx_to_json;
fn main() {
let path = Path::new("example.xlsx");
match parse_xlsx_to_json(path) {
Ok(json) => {
println!("Successfully parsed XLSX file to JSON");
println!("JSON: {}", json);
}
Err(e) => {
println!("Error parsing XLSX file: {}", e);
}
}
}use std::path::Path;
use rlsx::{parse_xlsx_to_workbook, Workbook};
fn main() {
let path = Path::new("example.xlsx");
match parse_xlsx_to_workbook(path) {
Ok(workbook) => {
println!("Successfully parsed XLSX file to Workbook structure");
println!("Number of sheets: {}", workbook.sheets.len());
// Access sheets
for (i, sheet) in workbook.sheets.iter().enumerate() {
println!("Sheet {}: {}", i + 1, sheet.name);
println!("Number of cells: {}", sheet.cells.len());
println!("Number of rows: {}", sheet.rows.len());
}
}
Err(e) => {
println!("Error parsing XLSX file: {}", e);
}
}
}Parses an Excel file and returns its contents as a JSON string.
Parses an Excel file and returns a structured Workbook object.
Represents an entire Excel workbook containing multiple sheets.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Workbook {
pub sheets: Vec<Sheet>,
}Represents a single sheet within a workbook.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Sheet {
pub name: String, // Sheet name
pub cells: Vec<Cell>, // All cells in the sheet
pub rows: Vec<Vec<Cell>>, // Cells organized by rows
}Represents a single cell in a worksheet.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Cell {
pub address: String, // Cell address (e.g., "A1")
pub value: CellValue, // Cell value
pub style: Option<CellStyle>, // Cell styling
}Represents the value of a cell with different types.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CellValue {
String(String), // Text values
Number(f64), // Numeric values
Bool(bool), // Boolean values
Date(DateTime<Utc>), // Date/time values
Empty, // Empty cells
}Represents the styling information for a cell.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CellStyle {
pub bg_color: Option<String>, // Background color (e.g., "#FFEEAA")
pub fg_color: Option<String>, // Foreground color
pub font_family: Option<String>, // Font family
pub bold: bool, // Bold text
pub italic: bool, // Italic text
pub underline: bool, // Underlined text
pub font_size: Option<f64>, // Font size
}The rlsx library is structured as follows:
- lib.rs: Main library entry point exposing public API
- model.rs: Core data structures (Workbook, Sheet, Cell, etc.)
- workbook.rs: Workbook parsing logic
- worksheet.rs: Worksheet parsing logic
- shared_strings.rs: Shared strings table parsing
- styles.rs: Styles parsing
- theme.rs: Theme parsing
- zip_reader.rs: ZIP archive handling for XLSX files
- json_export.rs: JSON export functionality
- error.rs: Error handling
- Zip Archive Handling: The XLSX file is opened as a ZIP archive
- Shared Strings Parsing: If present, the shared strings table is parsed
- Styles Parsing: If present, style information is parsed
- Workbook Parsing: The workbook structure is parsed to get sheet information
- Worksheet Parsing: Each worksheet is parsed individually
- Cell Processing: Cells are extracted with their values and styles
- Data Structuring: The parsed data is organized into the Workbook structure
- Optional JSON Conversion: The Workbook can be converted to JSON format
use std::path::Path;
use rlsx::parse_xlsx_to_workbook;
fn main() {
let path = Path::new("example.xlsx");
if let Ok(workbook) = parse_xlsx_to_workbook(path) {
// Get the first sheet
if let Some(sheet) = workbook.sheets.first() {
// Access a specific cell (e.g., A1)
if let Some(cell) = sheet.cells.iter().find(|c| c.address == "A1") {
match &cell.value {
rlsx::CellValue::String(s) => println!("A1: {}", s),
rlsx::CellValue::Number(n) => println!("A1: {}", n),
rlsx::CellValue::Bool(b) => println!("A1: {}", b),
rlsx::CellValue::Date(d) => println!("A1: {}", d),
rlsx::CellValue::Empty => println!("A1: Empty"),
}
}
}
}
}use std::path::Path;
use rlsx::parse_xlsx_to_workbook;
fn main() {
let path = Path::new("example.xlsx");
if let Ok(workbook) = parse_xlsx_to_workbook(path) {
if let Some(sheet) = workbook.sheets.first() {
println!("Sheet: {}", sheet.name);
println!("Row data:");
for (row_idx, row) in sheet.rows.iter().enumerate() {
print!("Row {}: ", row_idx + 1);
for (col_idx, cell) in row.iter().enumerate() {
if col_idx > 0 {
print!(" | ");
}
match &cell.value {
rlsx::CellValue::String(s) => print!("{}", s),
rlsx::CellValue::Number(n) => print!("{}", n),
rlsx::CellValue::Bool(b) => print!("{}", b),
rlsx::CellValue::Date(d) => print!("{}", d.format("%Y-%m-%d")),
rlsx::CellValue::Empty => print!("(empty)"),
}
}
println!();
}
}
}
}The library returns an XlsxError enum with the following variants:
FileNotFound: The specified XLSX file was not foundZipError: Error reading the ZIP archiveXmlParsing: Error parsing XML contentIoError: I/O error during file operationsUtf8Error: UTF-8 encoding errorParseError: General parsing error
- The library uses
quick-xmlfor fast XML parsing - ZIP archive handling is optimized for large files
- Shared strings are parsed once and reused for better performance
- Cell values are parsed lazily when needed
- Currently only supports reading XLSX files (no writing support)
- Some advanced Excel features may not be supported
- Formula evaluation is not implemented
- Macro-enabled files (.xlsm) are not supported
Contributions are welcome! Please feel free to submit a Pull Request.
- Clone the repository
- Install dependencies with
cargo build - Run tests with
cargo test - Format code with
cargo fmt - Check clippy warnings with
cargo clippy
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Rust 🦀
- Uses
zipcrate for ZIP archive handling - Uses
quick-xmlcrate for XML parsing - Uses
serdeandserde_jsonfor JSON serialization - Uses
chronofor date/time handling
- Basic XLSX file parsing
- Support for multiple sheets
- Cell value parsing (String, Number, Boolean, Date, Empty)
- Basic styling support
- JSON export functionality
- Workbook structured data model
- Support for writing XLSX files
- Formula evaluation
- Enhanced styling support
- Chart parsing
- Macro support (.xlsm files)
- Performance optimizations
- Documentation improvements
If you have any questions, issues, or feature requests, please create an issue on the GitHub repository.