| Crates.io | rlsx |
| lib.rs | rlsx |
| version | 1.0.1 |
| created_at | 2025-12-11 12:27:08.740076+00 |
| updated_at | 2025-12-20 08:16:40.228285+00 |
| description | Parse Excel files in Rust |
| homepage | |
| repository | https://github.com/NEXORA-Studios/Lib.Rust.Rlsx |
| max_upload_size | |
| id | 1979576 |
| size | 67,811 |
A fast and lightweight Rust library for parsing Excel (.xlsx) files.
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);
}
}
}
parse_xlsx_to_json<P: AsRef<Path>>(path: P) -> Result<String, XlsxError>Parses an Excel file and returns its contents as a JSON string.
parse_xlsx_to_workbook<P: AsRef<Path>>(path: P) -> Result<Workbook, XlsxError>Parses an Excel file and returns a structured Workbook object.
WorkbookRepresents an entire Excel workbook containing multiple sheets.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Workbook {
pub sheets: Vec<Sheet>,
}
SheetRepresents 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
}
CellRepresents 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
}
CellValueRepresents 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
}
CellStyleRepresents 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:
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 errorquick-xml for fast XML parsingContributions are welcome! Please feel free to submit a Pull Request.
cargo buildcargo testcargo fmtcargo clippyThis project is licensed under the MIT License - see the LICENSE file for details.
zip crate for ZIP archive handlingquick-xml crate for XML parsingserde and serde_json for JSON serializationchrono for date/time handlingIf you have any questions, issues, or feature requests, please create an issue on the GitHub repository.