Skip to content

Bug: XLSX table not loaded if the relationship target is absolute #587

@raptium

Description

@raptium

Current behavior

In the worksheet relationships file (xl/worksheets/_rels/sheet1.xml.rels), a table relationship with an absolute target like:

<Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" Target="/xl/tables/table1.xml" Id="rId1"/>

is ignored by calamine. The current implementation of read_table_metadata() handles relative paths using ../ but does not normalize or resolve targets beginning with a leading slash.

When the target is:

<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" Target="../tables/table1.xml"/>

the table loads as expected.

calamine/src/xlsx/mod.rs

Lines 570 to 580 in e76bcd7

if target.starts_with("../") {
// this is an incomplete implementation, but should be good enough for excel
let new_index =
base_folder.rfind('/').expect("Must be a parent folder");
let full_path =
format!("{}{}", &base_folder[..new_index], &target[2..]);
table_locations.push(full_path);
} else if target.is_empty() { // do nothing
} else {
table_locations.push(target);
}

target here will later be the path to find the table file in the zip archive.

Expected behavior

Tables in XLSX file should be loaded correctly.

Sample code to reproduce

use calamine::{Error, Xlsx, open_workbook};
use std::env;

fn main() -> Result<(), Error> {
    let args: Vec<String> = env::args().collect();

    if args.len() < 2 {
        eprintln!("Usage: {} <xlsx_file>", args[0]);
        std::process::exit(1);
    }

    let file_path = &args[1];

    let mut workbook: Xlsx<_> = open_workbook(file_path)?;

    workbook.load_tables().map_err(|e| Error::Xlsx(e))?;
    if workbook.table_names().is_empty() {
        eprintln!("No tables found in the workbook");
        std::process::exit(1);
    } else {
        println!("Tables found in the workbook: {:?}", workbook.table_names());
    }

    Ok(())
}

Test file

table.xlsx

Environment

- calamine version: 0.32.0
- Cargo.toml dependency line for calamine: calamine = "0.32.0"
- rustc version: 1.91.1 (ed61e7d7e 2025-11-07)
- Excel/OpenOffice/LibreOffice version: Version 16.103.2 (25112216)
- OS: macOS

Checklist

  • I have added a complete sample program that compiles in Rust.
  • I have added a test file.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions