Reading files | simple-excel-rust | Spatie      

 SPATIE  

simple-excel-rust
=================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Simple-excel-rust](https://spatie.be/docs/simple-excel-rust/v1)  Reading files

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/simple-excel-rust/v1) 

  Reading files    
- [ Introduction ](https://spatie.be/docs/simple-excel-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/simple-excel-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/simple-excel-rust/v1/installation-and-setup)
- [ Reading files ](https://spatie.be/docs/simple-excel-rust/v1/reading-files)
- [ Writing files ](https://spatie.be/docs/simple-excel-rust/v1/writing-files)
- [ Testing ](https://spatie.be/docs/simple-excel-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/simple-excel-rust/v1/changelog)

 Reading files
=============

###  On this page 

1. [ Reading a CSV ](#content-reading-a-csv)

Reading a CSV
-----------------------------------------------------------------------------------------------

Imagine you have a CSV with this content.

```
email,first_name
john@example.com,john
jane@example.com,jane
```

```
let rows = SimpleExcelReader::create(path_to_csv)?.get_rows()?;

for row in rows {
    let row_properties = row?;
    println!("{}", row_properties["email"]);
}
```

Rows keep column order. Use `row["email"]` for a known header, `row.get("email")` for an optional header, or `row[0]` for a numeric column. Indexing a missing key panics; `get` returns `None`. Values are typed `Value` variants; CSV values are strings. With headers, short rows are padded with empty strings, and extra columns are discarded. Duplicate header names keep the last value at the first key's position.

### Reading an Excel file

Reading an Excel file is identical to reading a CSV file. Just make sure that the path given to the `create` method of `SimpleExcelReader` ends with `xlsx`. ODS files use the same interface with an `ods` extension.

Use `SimpleExcelReader::create_with_type(path, FileType::Csv)` when the path has no extension or a different one. `FileType` is exported by this crate.

Reader configuration consumes and returns the reader. Files open when you request rows, headers or sheet metadata, not when you call `create`.

### Working with iterators

`get_rows` returns an iterator of `Result<Row, Error>`. Process each row with a `for` loop or Rust's iterator adapters. Read errors terminate the iterator.

Here's a quick, silly example where we only want to process rows that have a `first_name` that contains more than 5 characters.

```
let rows = SimpleExcelReader::create(path_to_csv)?.get_rows()?;
for row in rows {
    let row_properties = row?;
    if row_properties["first_name"].as_str().is_some_and(|name| name.len() > 5) {
        println!("{}", row_properties["first_name"]);
    }
}
```

Use `collect::<Result<Vec<_>, _>>()?` when you deliberately want all rows in memory. Do not use `filter_map(Result::ok)` unless discarding read errors is intentional. Each `get_rows()` call opens a fresh independent iterator, with the same skip and take settings. Drop the iterator to close the file; no reader `close()` call is needed.

<a id="memory-usage"></a>

### Memory usage

CSV reads stream through `csv` and `encoding_rs_io`. Memory depends on the current record and fixed-size buffers, not the total row count. Encoding conversion also happens incrementally.

XLSX reads use `calamine`, which loads the selected sheet and shared strings into memory. Keeping formulas or preserving date formatting also loads formula or style ranges. Iterating avoids a second full copy of the sheet, but does not make XLSX reading constant-memory.

ODS reads load the entire workbook into memory. For inputs too large to fit in memory, use CSV. Calling `collect` retains the returned rows in memory for every format.

### Reading a file without headers

If the file you are reading does not contain a header row, use `no_header_row()`.

```
let rows = SimpleExcelReader::create(path_to_csv)?
    .no_header_row()
    .get_rows()?;
```

### Manually setting the headers

If you would like to use a specific array of values for the headers, use `use_headers()`.

```
let rows = SimpleExcelReader::create(path_to_csv)?
    .use_headers(["email_address", "given_name"])
    .get_rows()?;
```

If your file already contains a header row, it will be ignored and replaced with your custom headers. If it does not contain a header row, also use `no_header_row()`.

### Working with multiple sheet documents

Excel files can include multiple spreadsheets. Select the sheet with `from_sheet()` using a one-based index, or `from_sheet_name()` using its exact name. `get_sheet_names()` retrieves all sheet names and `has_sheet()` checks for a name.

```
let reader = SimpleExcelReader::create(path_to_xlsx)?;
let sheets = reader.get_sheet_names()?;
let has_sheet = reader.has_sheet("sheet1")?;
let rows = reader.clone().from_sheet(2).get_rows()?;
let named_rows = reader.from_sheet_name("sheet2").get_rows()?;
```

An absent sheet returns `Error::SheetNotFound`. CSV exposes one sheet named `Sheet1`.

### Retrieving header row values

Use `get_headers()` to retrieve the header row as an optional vector. If you set custom headers, these are returned instead of the file's headers. `get_original_headers()` returns the file's headers after trimming and formatting but before custom replacement.

```
let headers = SimpleExcelReader::create(path_to_csv)?.get_headers()?;
```

### Dealing with headers that are not on the first line

Use `header_on_row()` to indicate the row at which the headers are present. Any data above this row is discarded. The argument starts at 0. Blank rows do not count unless spreadsheet empty rows are explicitly preserved.

```
let rows = SimpleExcelReader::create(path_to_xlsx)?
    .trim_header_row()
    .header_on_row(2)
    .get_rows()?;
```

### Trimming headers

If the header row contains additional characters, use `trim_header_row()`. It removes space, tab, newline, carriage return, NUL and vertical tab. `trim_header_row_with("a..z")` accepts a character set with ASCII ranges. Unicode characters are treated as characters rather than individual UTF-8 bytes.

### Convert headers to snake\_case

Use `headers_to_snake_case()` to convert headers such as `First Name`, `jobTitle` and `Column2` into `first_name`, `job_title` and `column_2`.

### Manually formatting headers

Use `format_headers_using()` with a closure. Formatting runs after trimming and snake\_case conversion.

```
let rows = SimpleExcelReader::create(path_to_csv)?
    .format_headers_using(|header| format!("{header}_simple_excel"))
    .get_rows()?;
```

### Trimming and formatting values

Just like you can clean up header names, you can clean up the data values themselves. Use `trim_values()` to strip whitespace from every string value. `trim_values_with("*")` strips a set of characters from both ends, not a suffix. Non-string values, such as dates, are untouched.

For full control, use `format_values_using()`. The closure receives the value and its header key. For unkeyed rows, the key is the numeric index rendered as a string.

```
let rows = SimpleExcelReader::create(path_to_csv)?
    .trim_values()
    .format_values_using(|value, key| match value {
        Value::String(text) if key == "email" => text.to_lowercase().into(),
        other => other,
    })
    .get_rows()?;
```

### Manually working with the reader object

`get_reader()` opens the underlying `calamine::Sheets` reader. `get_csv_reader()` opens the configured `csv::Reader`. These are independent engines: changing them does not change later `get_rows()` calls. All engine crates are re-exported where needed for advanced use.

### Limiting the result set

The `take` method limits the number of data rows. The `skip` method defines where to start after the header. For example, `.skip(10).take(5)` returns data rows 11 through 15. Both settings are reapplied on every `get_rows()` call. `take(0)` returns no rows.

### Reading cells that contain formulas

Normally, cells containing formulas return the cached computed value stored in the file. Use `keep_formulas()` to return `Value::Formula` strings instead. This package does not evaluate formulas. Files without cached results may return an empty value or the engine's stored default.

Formula strings start with `=` in both XLSX and ODS; the ODS namespace prefix is removed. Spreadsheet cell errors, such as `#DIV/0!`, are returned as `Value::Error`.

### Preserve date formatting

By default spreadsheet dates and times are returned as `Value::DateTime(chrono::NaiveDateTime)`. They have no timezone. Durations use `Value::Duration`. Use `preserve_date_time_formatting()` to return XLSX date strings using the stored number format.

```
let rows = SimpleExcelReader::create(path_to_xlsx)?
    .preserve_date_time_formatting()
    .get_rows()?;
```

Date formatting uses `ssfmt`'s Excel number-format support. It does not reproduce every locale-specific Excel display rule. Built-in format IDs 14, 22 and 47 display as `m/d/yyyy`, `m/d/yyyy h:mm` and `mm:ss.0`. Invalid date values or unparseable date formats return errors. The date-format option only applies to XLSX.

### Preserve empty rows

Use `preserve_empty_rows()` to preserve spreadsheet empty rows within the used range, including leading empty rows. Trailing rows beyond that range cannot be recovered. CSV blank lines are ignored.

Leading and interior empty cells keep their column positions. Unkeyed spreadsheet rows omit trailing blank cells; keyed rows pad missing values with empty strings.

### Using an alternative delimiter, enclosure or encoding

The CSV delimiter defaults to a comma, and the enclosure defaults to a double quote. Use `use_delimiter(b';')`, `use_field_enclosure(b'\'')` and `use_encoding("SHIFT_JIS")` to change them. Delimiters and enclosures must be distinct, non-NUL ASCII bytes other than CR or LF. Encoding labels follow the WHATWG registry. Malformed encoded sequences become U+FFFD, and BOMs are removed during decoding.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
