Create a spreadsheet document by providing a file name

Creating an .xlsx from scratch requires package relationships, content types, a workbook part, a workbook relationship item, and at least one worksheet part.

In ooxmlsdk, create the package with SpreadsheetDocument::create(SpreadsheetDocumentType::Workbook), add the workbook part, add worksheet parts from the workbook part, write the root XML, and save the package to the file or writer your application owns.

Choose the package type and file extension together. A normal workbook uses .xlsx; macro-enabled workbooks, templates, and add-ins use different extensions and content types. Excel can reject a file when the package type and extension do not match.

Minimal package pieces

A minimal workbook includes:

  • [Content_Types].xml,
  • _rels/.rels pointing to xl/workbook.xml,
  • xl/workbook.xml,
  • xl/_rels/workbook.xml.rels,
  • at least one xl/worksheets/sheetN.xml.

This minimal writer creates that structure in memory:

#![allow(unused)]
fn main() {
pub fn create_spreadsheet_document() -> Result<Vec<u8>, Box<dyn std::error::Error>> {
  let mut document = SpreadsheetDocument::create(SpreadsheetDocumentType::Workbook);
  let workbook_part = document.add_new_part_auto_id::<WorkbookPart>()?;
  let worksheet_part = workbook_part.add_new_part_auto_id::<_, WorksheetPart>(&mut document)?;
  let worksheet_relationship_id = workbook_part
    .get_id_of_part(&document, &worksheet_part)
    .expect("worksheet relationship id")
    .to_string();

  workbook_part.set_data(
    &mut document,
    format!(
      r#"<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Sheet1" sheetId="1" r:id="{worksheet_relationship_id}"/></sheets></workbook>"#
    )
    .into_bytes(),
  )?;
  worksheet_part.set_data(
    &mut document,
    br#"<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData/></worksheet>"#.to_vec(),
  )?;

  let mut buffer = Cursor::new(Vec::new());
  document.save(&mut buffer)?;
  Ok(buffer.into_inner())
}
}

At the SpreadsheetML layer, the workbook root owns the sheets collection. Each sheet entry stores the display name, a workbook-local sheetId, and an r:id relationship to the worksheet part. The worksheet part itself owns the worksheet root and sheetData.

For template workflows, SpreadsheetDocument::create_from_template opens an .xltx or .xltm as an editable workbook package. Use document_type() to inspect the current type and change_document_type(...) when converting the package content type deliberately.