Skip to main content

xlsx_structure_and_layout/
xlsx_structure_and_layout.rs

1//! Excel sheet structure and layout: merged ranges, column widths and
2//! hidden/grouped columns, hidden/grouped rows, frozen panes, and manual
3//! page breaks. One tab per feature.
4//!
5//! Run with: `cargo run -p office-toolkit --example xlsx_structure_and_layout`
6
7use std::path::{Path, PathBuf};
8
9use office_toolkit::SaveToFile;
10use office_toolkit::excel::{Cell, ColumnSetting, FreezePane, Sheet};
11use office_toolkit::prelude::*;
12
13fn main() -> office_toolkit::Result<()> {
14    let path = output_path("xlsx_structure_and_layout.xlsx");
15
16    let merged_sheet = Sheet::new("Merged cells")
17        .with_row(Row::new().with_text("This title spans three columns"))
18        .with_merged_range("A1:C1");
19
20    let columns_sheet = Sheet::new("Columns")
21        .with_row(
22            Row::new()
23                .with_cell(Cell::text("Wide"))
24                .with_cell(Cell::text("Hidden"))
25                .with_cell(Cell::text("Grouped")),
26        )
27        .with_column_setting(ColumnSetting::new(0).with_width(30.0))
28        .with_column_setting(ColumnSetting::new(1).with_hidden(true))
29        .with_column_setting(
30            ColumnSetting::new(2)
31                .with_outline_level(1)
32                .with_collapsed(false),
33        );
34
35    let rows_sheet = Sheet::new("Rows")
36        .with_row(Row::new().with_text("Normal row"))
37        .with_row(Row::new().with_text("Taller row").with_height(30.0))
38        .with_row(Row::new().with_text("Hidden row").with_hidden(true))
39        .with_row(
40            Row::new()
41                .with_text("Grouped detail row")
42                .with_outline_level(1),
43        );
44
45    let frozen_panes_sheet = Sheet::new("Frozen panes")
46        .with_row(
47            Row::new()
48                .with_cell(Cell::text("Frozen header"))
49                .with_cell(Cell::text("Frozen header")),
50        )
51        .with_row(
52            Row::new()
53                .with_cell(Cell::text("Scrolling data"))
54                .with_cell(Cell::number(1.0)),
55        )
56        .with_freeze_panes(FreezePane::new(1, 1));
57
58    let page_breaks_sheet = Sheet::new("Page breaks")
59        .with_row(Row::new().with_text("Above this row: page break"))
60        .with_row(Row::new().with_text("Below the break"))
61        .with_row_break(1)
62        .with_column_break(0)
63        .with_outline_summary_below(false)
64        .with_outline_summary_right(false);
65
66    let workbook = Workbook::new()
67        .with_sheet(merged_sheet)
68        .with_sheet(columns_sheet)
69        .with_sheet(rows_sheet)
70        .with_sheet(frozen_panes_sheet)
71        .with_sheet(page_breaks_sheet);
72
73    workbook.save_to_file(&path)?;
74    println!("Wrote {}", path.display());
75    Ok(())
76}
77
78/// Every example in this directory writes its output under
79/// `tests-data/output/`, resolved relative to this crate's own manifest so
80/// it works no matter what directory `cargo run` was invoked from.
81fn output_path(filename: &str) -> PathBuf {
82    Path::new(env!("CARGO_MANIFEST_DIR"))
83        .join("../../tests-data/output")
84        .join(filename)
85}