Skip to main content

CellFormat

Struct CellFormat 

Source
pub struct CellFormat {
Show 21 fields pub number_format: Option<String>, pub bold: bool, pub italic: bool, pub font_size: Option<f64>, pub font_color: Option<String>, pub fill_color: Option<String>, pub horizontal_alignment: Option<HorizontalAlignment>, pub vertical_alignment: Option<VerticalAlignment>, pub border: Border, pub font_name: Option<String>, pub underline: bool, pub strike: bool, pub wrap_text: bool, pub indent: Option<u32>, pub text_rotation: Option<u16>, pub shrink_to_fit: bool, pub pattern_fill: Option<PatternFill>, pub gradient_fill: Option<GradientFill>, pub locked: Option<bool>, pub formula_hidden: Option<bool>, pub named_style: Option<String>,
}
Expand description

A cell’s style: a number format, basic font attributes (bold/italic/size/color), a solid background fill color, borders, and horizontal/vertical alignment.

Not deduplicated on the model itself — the writer deduplicates identical CellFormats into shared xl/styles.xml entries (numFmts/fonts/fills/cellXfs) when serializing, exactly like Workbook’s own shared-strings deduplication for repeated text (see writer.rs::collect_cell_formats/build_styles).

Fields§

§number_format: Option<String>

A custom number format code (e.g. "0.00", "#,##0"). Always registered as a custom format (numFmtId >= 164) when written, even if the code happens to match one of Excel’s built-in format codes — this crate does not maintain the built-in-format lookup table (BuiltinFormats), so it never reuses a built-in id. Reading back a real .xlsx’s built-in number format (any numFmtId not declared in that file’s own <numFmts>) is not modeled either: it reads back as None.

§bold: bool

Bold font weight.

§italic: bool

Italic font style.

§font_size: Option<f64>

Font size in points. None uses the workbook default (11pt) — note that writing any other font attribute (bold, italic, or a color) also resolves this to the default 11pt in the generated xl/styles.xml font entry (a single <font> element can’t leave “unset” attributes that fall back independently), so reading a round-tripped format back may see Some(11.0) rather than None — a known, accepted round-trip nuance.

§font_color: Option<String>

Font color, as an 8-hex-digit ARGB string (e.g. "FFFF0000" for opaque red). None uses the workbook default (opaque black) — same round-trip nuance as font_size applies here too (and to font_name below).

§fill_color: Option<String>

Solid background fill color, as an 8-hex-digit ARGB string. None leaves the cell unfilled.

§horizontal_alignment: Option<HorizontalAlignment>

Horizontal alignment. None uses Excel’s own default (general — text left-aligned, numbers right-aligned).

§vertical_alignment: Option<VerticalAlignment>

Vertical alignment. None uses Excel’s own default (bottom).

§border: Border

This cell’s borders (top/bottom/left/right/diagonal). Border::default() (all None/false) draws no border at all, matching every other CellFormat field’s “unset means Excel’s own default” posture.

§font_name: Option<String>

Font name/family (e.g. "Calibri", "Arial"). None uses the workbook default (Excel’s own “Calibri”). Only a single Latin font name is modeled, not CT_Font’s separate east-Asian/complex-script overrides (family/scheme/charset are not modeled either). Same unset-resolves-to-the-default round-trip nuance as font_size/ font_color above applies here too: writing any other font attribute also resolves this to Some("Calibri") in the generated <font> entry, so an originally-None font_name may read back as Some("Calibri") rather than None.

§underline: bool

Underline. Only a plain single underline is modeled (CT_Font’s ST_UnderlineValues has several more: double/singleAccounting/ doubleAccounting) — same on/off posture RichTextRun’s bold/italic already use (unlike word-ooxml’s much richer UnderlineStyle enum).

§strike: bool

Strikethrough.

§wrap_text: bool

Wraps text onto multiple lines within the cell (wrapText).

§indent: Option<u32>

Indentation level (indent, roughly 3 character-widths per level).

§text_rotation: Option<u16>

Text rotation, in Excel’s own raw ST_CellAlignment/textRotation units — 0 to 90 for counter-clockwise degrees, 91 to 180 for clockwise degrees (stored as 90 - angle, e.g. a plain -45° tilt is 135), or the special value 255 for top-to-bottom vertical text. Written and read back verbatim — converting a plain signed angle into this encoding is the caller’s responsibility, same “storage, not a conversion helper” posture as PrintSettings::header’s raw &L/&C/&R codes.

§shrink_to_fit: bool

Shrinks the font size to fit the cell’s width instead of wrapping or overflowing (shrinkToFit).

§pattern_fill: Option<PatternFill>

A non-solid pattern fill (<patternFill patternType=".."> with a foreground/background color pair). Mutually exclusive with fill_color/gradient_fill in practice (Excel itself only ever uses one <fill> child); if more than one is set, the writer prefers gradient_fill, then pattern_fill, then fill_color, see writer.rs::resolve_fill.

§gradient_fill: Option<GradientFill>

A gradient fill (<gradientFill>). Only the linear (angle-based) gradient form is modeled, not CT_GradientFill’s “path” (radial, from-center) form. See pattern_fill’s doc comment for the precedence rule when more than one fill field is set.

§locked: Option<bool>

Whether this cell is locked when the sheet is protected (<protection locked=".">, CT_CellProtection) — independent of Sheet::protection actually being turned on: Excel itself treats every cell as locked by default, so protection only has a visible effect once Sheet::protection is Some. None leaves Excel’s own default (locked).

§formula_hidden: Option<bool>

Whether this cell’s formula is hidden from the formula bar when the sheet is protected (<protection hidden="..">). None leaves Excel’s own default (not hidden). Meaningless on a non-formula cell, same as real Excel.

§named_style: Option<String>

Links this format to one of Workbook::named_cell_styles by name (resolved into that entry’s index, written as this format’s cellXfs xfId attribute). This is what makes a cell show up as e.g. “Good”/“Heading 1” in Excel’s own Cell Styles gallery (highlighted/selected) rather than merely happening to look the same. None (the default) writes xfId="0", i.e. based on the implicit "Normal" style, same as every CellFormat before this point. A name not found in Workbook::named_cell_styles at write time silently falls back to 0 too — see that field’s own doc comment.

Implementations§

Source§

impl CellFormat

Source

pub fn new() -> CellFormat

Creates a default cell format (no overrides at all — equivalent to not setting a format, only useful as a starting point for the with_* builder methods).

Examples found in repository?
examples/xlsx_tables.rs (line 88)
16fn main() -> office_toolkit::Result<()> {
17    let path = output_path("xlsx_tables.xlsx");
18
19    let plain_sheet = Sheet::new("Plain table")
20        .with_row(Row::new().with_text("Name").with_text("Score"))
21        .with_row(Row::new().with_text("Alice").with_number(92.0))
22        .with_row(Row::new().with_text("Bob").with_number(87.0))
23        .with_table(ExcelTable::new(
24            "ScoresTable",
25            "A1:B3",
26            vec!["Name", "Score"],
27        ));
28
29    let calculated_sheet = Sheet::new("Calculated and totals")
30        .with_row(
31            Row::new()
32                .with_text("Item")
33                .with_text("Qty")
34                .with_text("Unit price")
35                .with_text("Line total"),
36        )
37        .with_row(
38            Row::new()
39                .with_text("Widget")
40                .with_number(3.0)
41                .with_number(9.99)
42                .with_cell(Cell::formula("B2*C2").with_cached_value(29.97)),
43        )
44        .with_row(
45            Row::new()
46                .with_text("Gadget")
47                .with_number(2.0)
48                .with_number(19.99)
49                .with_cell(Cell::formula("B3*C3").with_cached_value(39.98)),
50        )
51        .with_row(Row::new().with_cell(Cell::text("Total")))
52        .with_table(
53            ExcelTable::with_columns(
54                "OrdersTable",
55                "A1:D4",
56                vec![
57                    TableColumn::new("Item"),
58                    TableColumn::new("Qty").with_totals_row_function(TotalsRowFunction::Sum),
59                    TableColumn::new("Unit price"),
60                    TableColumn::new("Line total")
61                        .with_calculated_formula(
62                            "OrdersTable[[#This Row],[Qty]]*OrdersTable[[#This Row],[Unit price]]",
63                        )
64                        .with_totals_row_function(TotalsRowFunction::Sum),
65                ],
66            )
67            .with_totals_row(true),
68        );
69
70    let styled_sheet = Sheet::new("Custom style and sort")
71        .with_row(Row::new().with_text("City").with_text("Population"))
72        .with_row(Row::new().with_text("Springfield").with_number(120_000.0))
73        .with_row(Row::new().with_text("Shelbyville").with_number(95_000.0))
74        .with_table(
75            ExcelTable::new("CitiesTable", "A1:B3", vec!["City", "Population"])
76                .with_table_style_name("CustomTableStyle")
77                .with_sort_state(
78                    SortState::new("A2:B3")
79                        .with_condition(SortCondition::new("B2:B3").with_descending(true)),
80                ),
81        );
82
83    let workbook = Workbook::new()
84        .with_table_style(
85            TableStyle::new("CustomTableStyle")
86                .with_element(TableStyleElement::new(
87                    TableStyleElementType::HeaderRow,
88                    CellFormat::new()
89                        .with_bold(true)
90                        .with_fill_color("FF2E74B5")
91                        .with_font_color("FFFFFFFF"),
92                ))
93                .with_element(TableStyleElement::new(
94                    TableStyleElementType::FirstRowStripe,
95                    CellFormat::new().with_fill_color("FFF2F2F2"),
96                )),
97        )
98        .with_sheet(plain_sheet)
99        .with_sheet(calculated_sheet)
100        .with_sheet(styled_sheet);
101
102    workbook.save_to_file(&path)?;
103    println!("Wrote {}", path.display());
104    Ok(())
105}
More examples
Hide additional examples
examples/xlsx_conditional_formatting_and_validation.rs (line 28)
17fn main() -> office_toolkit::Result<()> {
18    let path = output_path("xlsx_conditional_formatting_and_validation.xlsx");
19
20    let cell_is_sheet = Sheet::new("Cell-is rule")
21        .with_row(Row::new().with_number(45.0))
22        .with_row(Row::new().with_number(72.0))
23        .with_row(Row::new().with_number(91.0))
24        .with_conditional_formatting_rule(ConditionalFormattingRule::cell_is(
25            "A1:A3",
26            ComparisonOperator::GreaterThan,
27            "70",
28            CellFormat::new()
29                .with_fill_color("FFC6EFCE")
30                .with_font_color("FF006100"),
31        ));
32
33    let scales_and_bars_sheet = Sheet::new("Color scale and data bar")
34        .with_row(Row::new().with_number(10.0))
35        .with_row(Row::new().with_number(50.0))
36        .with_row(Row::new().with_number(90.0))
37        .with_conditional_formatting_rule(ConditionalFormattingRule::color_scale(
38            "A1:A3",
39            vec![
40                ColorScaleStop::new(CfvoPosition::Min, "FFF8696B"),
41                ColorScaleStop::new(CfvoPosition::Max, "FF63BE7B"),
42            ],
43        ))
44        .with_row(Row::new().with_number(10.0))
45        .with_row(Row::new().with_number(50.0))
46        .with_row(Row::new().with_number(90.0))
47        .with_conditional_formatting_rule(ConditionalFormattingRule::data_bar(
48            "B1:B3",
49            CfvoPosition::Min,
50            CfvoPosition::Max,
51            "FF638EC6",
52        ));
53
54    let icon_set_and_ranking_sheet = Sheet::new("Icon set, top-10, duplicates")
55        .with_row(Row::new().with_number(1.0))
56        .with_row(Row::new().with_number(2.0))
57        .with_row(Row::new().with_number(3.0))
58        .with_conditional_formatting_rule(ConditionalFormattingRule::icon_set(
59            "A1:A3",
60            IconSetType::ThreeTrafficLights,
61        ))
62        .with_row(Row::new().with_number(4.0))
63        .with_row(Row::new().with_number(5.0))
64        .with_row(Row::new().with_number(6.0))
65        .with_conditional_formatting_rule(ConditionalFormattingRule::top10(
66            "B1:B3",
67            1,
68            false,
69            CellFormat::new().with_bold(true),
70        ))
71        .with_row(Row::new().with_text("A"))
72        .with_row(Row::new().with_text("A"))
73        .with_row(Row::new().with_text("B"))
74        .with_conditional_formatting_rule(ConditionalFormattingRule::duplicate_values(
75            "C1:C3",
76            CellFormat::new().with_fill_color("FFFFC7CE"),
77        ));
78
79    let data_validation_sheet = Sheet::new("Data validation")
80        .with_row(Row::new().with_text("Pick one"))
81        .with_data_validation_rule(DataValidationRule::list("A1:A1", "\"Small,Medium,Large\""))
82        .with_row(Row::new().with_number(0.0))
83        .with_data_validation_rule(
84            DataValidationRule::whole_number("B1:B1", ComparisonOperator::LessThanOrEqual, "100")
85                .with_input_message(
86                    Message::new("Enter a whole number from 0 to 100.").with_title("Valid range"),
87                )
88                .with_error_message(
89                    Message::new("That value is out of range.").with_title("Invalid entry"),
90                ),
91        );
92
93    let workbook = Workbook::new()
94        .with_sheet(cell_is_sheet)
95        .with_sheet(scales_and_bars_sheet)
96        .with_sheet(icon_set_and_ranking_sheet)
97        .with_sheet(data_validation_sheet);
98
99    workbook.save_to_file(&path)?;
100    println!("Wrote {}", path.display());
101    Ok(())
102}
examples/xlsx_cell_formatting.rs (line 22)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_number_format(self, format: impl Into<String>) -> CellFormat

Sets a custom number format code.

Examples found in repository?
examples/xlsx_cell_formatting.rs (line 87)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_bold(self, bold: bool) -> CellFormat

Sets bold.

Examples found in repository?
examples/xlsx_tables.rs (line 89)
16fn main() -> office_toolkit::Result<()> {
17    let path = output_path("xlsx_tables.xlsx");
18
19    let plain_sheet = Sheet::new("Plain table")
20        .with_row(Row::new().with_text("Name").with_text("Score"))
21        .with_row(Row::new().with_text("Alice").with_number(92.0))
22        .with_row(Row::new().with_text("Bob").with_number(87.0))
23        .with_table(ExcelTable::new(
24            "ScoresTable",
25            "A1:B3",
26            vec!["Name", "Score"],
27        ));
28
29    let calculated_sheet = Sheet::new("Calculated and totals")
30        .with_row(
31            Row::new()
32                .with_text("Item")
33                .with_text("Qty")
34                .with_text("Unit price")
35                .with_text("Line total"),
36        )
37        .with_row(
38            Row::new()
39                .with_text("Widget")
40                .with_number(3.0)
41                .with_number(9.99)
42                .with_cell(Cell::formula("B2*C2").with_cached_value(29.97)),
43        )
44        .with_row(
45            Row::new()
46                .with_text("Gadget")
47                .with_number(2.0)
48                .with_number(19.99)
49                .with_cell(Cell::formula("B3*C3").with_cached_value(39.98)),
50        )
51        .with_row(Row::new().with_cell(Cell::text("Total")))
52        .with_table(
53            ExcelTable::with_columns(
54                "OrdersTable",
55                "A1:D4",
56                vec![
57                    TableColumn::new("Item"),
58                    TableColumn::new("Qty").with_totals_row_function(TotalsRowFunction::Sum),
59                    TableColumn::new("Unit price"),
60                    TableColumn::new("Line total")
61                        .with_calculated_formula(
62                            "OrdersTable[[#This Row],[Qty]]*OrdersTable[[#This Row],[Unit price]]",
63                        )
64                        .with_totals_row_function(TotalsRowFunction::Sum),
65                ],
66            )
67            .with_totals_row(true),
68        );
69
70    let styled_sheet = Sheet::new("Custom style and sort")
71        .with_row(Row::new().with_text("City").with_text("Population"))
72        .with_row(Row::new().with_text("Springfield").with_number(120_000.0))
73        .with_row(Row::new().with_text("Shelbyville").with_number(95_000.0))
74        .with_table(
75            ExcelTable::new("CitiesTable", "A1:B3", vec!["City", "Population"])
76                .with_table_style_name("CustomTableStyle")
77                .with_sort_state(
78                    SortState::new("A2:B3")
79                        .with_condition(SortCondition::new("B2:B3").with_descending(true)),
80                ),
81        );
82
83    let workbook = Workbook::new()
84        .with_table_style(
85            TableStyle::new("CustomTableStyle")
86                .with_element(TableStyleElement::new(
87                    TableStyleElementType::HeaderRow,
88                    CellFormat::new()
89                        .with_bold(true)
90                        .with_fill_color("FF2E74B5")
91                        .with_font_color("FFFFFFFF"),
92                ))
93                .with_element(TableStyleElement::new(
94                    TableStyleElementType::FirstRowStripe,
95                    CellFormat::new().with_fill_color("FFF2F2F2"),
96                )),
97        )
98        .with_sheet(plain_sheet)
99        .with_sheet(calculated_sheet)
100        .with_sheet(styled_sheet);
101
102    workbook.save_to_file(&path)?;
103    println!("Wrote {}", path.display());
104    Ok(())
105}
More examples
Hide additional examples
examples/xlsx_conditional_formatting_and_validation.rs (line 69)
17fn main() -> office_toolkit::Result<()> {
18    let path = output_path("xlsx_conditional_formatting_and_validation.xlsx");
19
20    let cell_is_sheet = Sheet::new("Cell-is rule")
21        .with_row(Row::new().with_number(45.0))
22        .with_row(Row::new().with_number(72.0))
23        .with_row(Row::new().with_number(91.0))
24        .with_conditional_formatting_rule(ConditionalFormattingRule::cell_is(
25            "A1:A3",
26            ComparisonOperator::GreaterThan,
27            "70",
28            CellFormat::new()
29                .with_fill_color("FFC6EFCE")
30                .with_font_color("FF006100"),
31        ));
32
33    let scales_and_bars_sheet = Sheet::new("Color scale and data bar")
34        .with_row(Row::new().with_number(10.0))
35        .with_row(Row::new().with_number(50.0))
36        .with_row(Row::new().with_number(90.0))
37        .with_conditional_formatting_rule(ConditionalFormattingRule::color_scale(
38            "A1:A3",
39            vec![
40                ColorScaleStop::new(CfvoPosition::Min, "FFF8696B"),
41                ColorScaleStop::new(CfvoPosition::Max, "FF63BE7B"),
42            ],
43        ))
44        .with_row(Row::new().with_number(10.0))
45        .with_row(Row::new().with_number(50.0))
46        .with_row(Row::new().with_number(90.0))
47        .with_conditional_formatting_rule(ConditionalFormattingRule::data_bar(
48            "B1:B3",
49            CfvoPosition::Min,
50            CfvoPosition::Max,
51            "FF638EC6",
52        ));
53
54    let icon_set_and_ranking_sheet = Sheet::new("Icon set, top-10, duplicates")
55        .with_row(Row::new().with_number(1.0))
56        .with_row(Row::new().with_number(2.0))
57        .with_row(Row::new().with_number(3.0))
58        .with_conditional_formatting_rule(ConditionalFormattingRule::icon_set(
59            "A1:A3",
60            IconSetType::ThreeTrafficLights,
61        ))
62        .with_row(Row::new().with_number(4.0))
63        .with_row(Row::new().with_number(5.0))
64        .with_row(Row::new().with_number(6.0))
65        .with_conditional_formatting_rule(ConditionalFormattingRule::top10(
66            "B1:B3",
67            1,
68            false,
69            CellFormat::new().with_bold(true),
70        ))
71        .with_row(Row::new().with_text("A"))
72        .with_row(Row::new().with_text("A"))
73        .with_row(Row::new().with_text("B"))
74        .with_conditional_formatting_rule(ConditionalFormattingRule::duplicate_values(
75            "C1:C3",
76            CellFormat::new().with_fill_color("FFFFC7CE"),
77        ));
78
79    let data_validation_sheet = Sheet::new("Data validation")
80        .with_row(Row::new().with_text("Pick one"))
81        .with_data_validation_rule(DataValidationRule::list("A1:A1", "\"Small,Medium,Large\""))
82        .with_row(Row::new().with_number(0.0))
83        .with_data_validation_rule(
84            DataValidationRule::whole_number("B1:B1", ComparisonOperator::LessThanOrEqual, "100")
85                .with_input_message(
86                    Message::new("Enter a whole number from 0 to 100.").with_title("Valid range"),
87                )
88                .with_error_message(
89                    Message::new("That value is out of range.").with_title("Invalid entry"),
90                ),
91        );
92
93    let workbook = Workbook::new()
94        .with_sheet(cell_is_sheet)
95        .with_sheet(scales_and_bars_sheet)
96        .with_sheet(icon_set_and_ranking_sheet)
97        .with_sheet(data_validation_sheet);
98
99    workbook.save_to_file(&path)?;
100    println!("Wrote {}", path.display());
101    Ok(())
102}
examples/xlsx_cell_formatting.rs (line 66)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_italic(self, italic: bool) -> CellFormat

Sets italic.

Source

pub fn with_font_size(self, size: f64) -> CellFormat

Sets the font size, in points.

Examples found in repository?
examples/xlsx_cell_formatting.rs (line 67)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_font_color(self, color: impl Into<String>) -> CellFormat

Sets the font color (8-hex-digit ARGB, e.g. "FFFF0000").

Examples found in repository?
examples/xlsx_tables.rs (line 91)
16fn main() -> office_toolkit::Result<()> {
17    let path = output_path("xlsx_tables.xlsx");
18
19    let plain_sheet = Sheet::new("Plain table")
20        .with_row(Row::new().with_text("Name").with_text("Score"))
21        .with_row(Row::new().with_text("Alice").with_number(92.0))
22        .with_row(Row::new().with_text("Bob").with_number(87.0))
23        .with_table(ExcelTable::new(
24            "ScoresTable",
25            "A1:B3",
26            vec!["Name", "Score"],
27        ));
28
29    let calculated_sheet = Sheet::new("Calculated and totals")
30        .with_row(
31            Row::new()
32                .with_text("Item")
33                .with_text("Qty")
34                .with_text("Unit price")
35                .with_text("Line total"),
36        )
37        .with_row(
38            Row::new()
39                .with_text("Widget")
40                .with_number(3.0)
41                .with_number(9.99)
42                .with_cell(Cell::formula("B2*C2").with_cached_value(29.97)),
43        )
44        .with_row(
45            Row::new()
46                .with_text("Gadget")
47                .with_number(2.0)
48                .with_number(19.99)
49                .with_cell(Cell::formula("B3*C3").with_cached_value(39.98)),
50        )
51        .with_row(Row::new().with_cell(Cell::text("Total")))
52        .with_table(
53            ExcelTable::with_columns(
54                "OrdersTable",
55                "A1:D4",
56                vec![
57                    TableColumn::new("Item"),
58                    TableColumn::new("Qty").with_totals_row_function(TotalsRowFunction::Sum),
59                    TableColumn::new("Unit price"),
60                    TableColumn::new("Line total")
61                        .with_calculated_formula(
62                            "OrdersTable[[#This Row],[Qty]]*OrdersTable[[#This Row],[Unit price]]",
63                        )
64                        .with_totals_row_function(TotalsRowFunction::Sum),
65                ],
66            )
67            .with_totals_row(true),
68        );
69
70    let styled_sheet = Sheet::new("Custom style and sort")
71        .with_row(Row::new().with_text("City").with_text("Population"))
72        .with_row(Row::new().with_text("Springfield").with_number(120_000.0))
73        .with_row(Row::new().with_text("Shelbyville").with_number(95_000.0))
74        .with_table(
75            ExcelTable::new("CitiesTable", "A1:B3", vec!["City", "Population"])
76                .with_table_style_name("CustomTableStyle")
77                .with_sort_state(
78                    SortState::new("A2:B3")
79                        .with_condition(SortCondition::new("B2:B3").with_descending(true)),
80                ),
81        );
82
83    let workbook = Workbook::new()
84        .with_table_style(
85            TableStyle::new("CustomTableStyle")
86                .with_element(TableStyleElement::new(
87                    TableStyleElementType::HeaderRow,
88                    CellFormat::new()
89                        .with_bold(true)
90                        .with_fill_color("FF2E74B5")
91                        .with_font_color("FFFFFFFF"),
92                ))
93                .with_element(TableStyleElement::new(
94                    TableStyleElementType::FirstRowStripe,
95                    CellFormat::new().with_fill_color("FFF2F2F2"),
96                )),
97        )
98        .with_sheet(plain_sheet)
99        .with_sheet(calculated_sheet)
100        .with_sheet(styled_sheet);
101
102    workbook.save_to_file(&path)?;
103    println!("Wrote {}", path.display());
104    Ok(())
105}
More examples
Hide additional examples
examples/xlsx_conditional_formatting_and_validation.rs (line 30)
17fn main() -> office_toolkit::Result<()> {
18    let path = output_path("xlsx_conditional_formatting_and_validation.xlsx");
19
20    let cell_is_sheet = Sheet::new("Cell-is rule")
21        .with_row(Row::new().with_number(45.0))
22        .with_row(Row::new().with_number(72.0))
23        .with_row(Row::new().with_number(91.0))
24        .with_conditional_formatting_rule(ConditionalFormattingRule::cell_is(
25            "A1:A3",
26            ComparisonOperator::GreaterThan,
27            "70",
28            CellFormat::new()
29                .with_fill_color("FFC6EFCE")
30                .with_font_color("FF006100"),
31        ));
32
33    let scales_and_bars_sheet = Sheet::new("Color scale and data bar")
34        .with_row(Row::new().with_number(10.0))
35        .with_row(Row::new().with_number(50.0))
36        .with_row(Row::new().with_number(90.0))
37        .with_conditional_formatting_rule(ConditionalFormattingRule::color_scale(
38            "A1:A3",
39            vec![
40                ColorScaleStop::new(CfvoPosition::Min, "FFF8696B"),
41                ColorScaleStop::new(CfvoPosition::Max, "FF63BE7B"),
42            ],
43        ))
44        .with_row(Row::new().with_number(10.0))
45        .with_row(Row::new().with_number(50.0))
46        .with_row(Row::new().with_number(90.0))
47        .with_conditional_formatting_rule(ConditionalFormattingRule::data_bar(
48            "B1:B3",
49            CfvoPosition::Min,
50            CfvoPosition::Max,
51            "FF638EC6",
52        ));
53
54    let icon_set_and_ranking_sheet = Sheet::new("Icon set, top-10, duplicates")
55        .with_row(Row::new().with_number(1.0))
56        .with_row(Row::new().with_number(2.0))
57        .with_row(Row::new().with_number(3.0))
58        .with_conditional_formatting_rule(ConditionalFormattingRule::icon_set(
59            "A1:A3",
60            IconSetType::ThreeTrafficLights,
61        ))
62        .with_row(Row::new().with_number(4.0))
63        .with_row(Row::new().with_number(5.0))
64        .with_row(Row::new().with_number(6.0))
65        .with_conditional_formatting_rule(ConditionalFormattingRule::top10(
66            "B1:B3",
67            1,
68            false,
69            CellFormat::new().with_bold(true),
70        ))
71        .with_row(Row::new().with_text("A"))
72        .with_row(Row::new().with_text("A"))
73        .with_row(Row::new().with_text("B"))
74        .with_conditional_formatting_rule(ConditionalFormattingRule::duplicate_values(
75            "C1:C3",
76            CellFormat::new().with_fill_color("FFFFC7CE"),
77        ));
78
79    let data_validation_sheet = Sheet::new("Data validation")
80        .with_row(Row::new().with_text("Pick one"))
81        .with_data_validation_rule(DataValidationRule::list("A1:A1", "\"Small,Medium,Large\""))
82        .with_row(Row::new().with_number(0.0))
83        .with_data_validation_rule(
84            DataValidationRule::whole_number("B1:B1", ComparisonOperator::LessThanOrEqual, "100")
85                .with_input_message(
86                    Message::new("Enter a whole number from 0 to 100.").with_title("Valid range"),
87                )
88                .with_error_message(
89                    Message::new("That value is out of range.").with_title("Invalid entry"),
90                ),
91        );
92
93    let workbook = Workbook::new()
94        .with_sheet(cell_is_sheet)
95        .with_sheet(scales_and_bars_sheet)
96        .with_sheet(icon_set_and_ranking_sheet)
97        .with_sheet(data_validation_sheet);
98
99    workbook.save_to_file(&path)?;
100    println!("Wrote {}", path.display());
101    Ok(())
102}
examples/xlsx_cell_formatting.rs (line 68)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_fill_color(self, color: impl Into<String>) -> CellFormat

Sets the solid background fill color (8-hex-digit ARGB).

Examples found in repository?
examples/xlsx_tables.rs (line 90)
16fn main() -> office_toolkit::Result<()> {
17    let path = output_path("xlsx_tables.xlsx");
18
19    let plain_sheet = Sheet::new("Plain table")
20        .with_row(Row::new().with_text("Name").with_text("Score"))
21        .with_row(Row::new().with_text("Alice").with_number(92.0))
22        .with_row(Row::new().with_text("Bob").with_number(87.0))
23        .with_table(ExcelTable::new(
24            "ScoresTable",
25            "A1:B3",
26            vec!["Name", "Score"],
27        ));
28
29    let calculated_sheet = Sheet::new("Calculated and totals")
30        .with_row(
31            Row::new()
32                .with_text("Item")
33                .with_text("Qty")
34                .with_text("Unit price")
35                .with_text("Line total"),
36        )
37        .with_row(
38            Row::new()
39                .with_text("Widget")
40                .with_number(3.0)
41                .with_number(9.99)
42                .with_cell(Cell::formula("B2*C2").with_cached_value(29.97)),
43        )
44        .with_row(
45            Row::new()
46                .with_text("Gadget")
47                .with_number(2.0)
48                .with_number(19.99)
49                .with_cell(Cell::formula("B3*C3").with_cached_value(39.98)),
50        )
51        .with_row(Row::new().with_cell(Cell::text("Total")))
52        .with_table(
53            ExcelTable::with_columns(
54                "OrdersTable",
55                "A1:D4",
56                vec![
57                    TableColumn::new("Item"),
58                    TableColumn::new("Qty").with_totals_row_function(TotalsRowFunction::Sum),
59                    TableColumn::new("Unit price"),
60                    TableColumn::new("Line total")
61                        .with_calculated_formula(
62                            "OrdersTable[[#This Row],[Qty]]*OrdersTable[[#This Row],[Unit price]]",
63                        )
64                        .with_totals_row_function(TotalsRowFunction::Sum),
65                ],
66            )
67            .with_totals_row(true),
68        );
69
70    let styled_sheet = Sheet::new("Custom style and sort")
71        .with_row(Row::new().with_text("City").with_text("Population"))
72        .with_row(Row::new().with_text("Springfield").with_number(120_000.0))
73        .with_row(Row::new().with_text("Shelbyville").with_number(95_000.0))
74        .with_table(
75            ExcelTable::new("CitiesTable", "A1:B3", vec!["City", "Population"])
76                .with_table_style_name("CustomTableStyle")
77                .with_sort_state(
78                    SortState::new("A2:B3")
79                        .with_condition(SortCondition::new("B2:B3").with_descending(true)),
80                ),
81        );
82
83    let workbook = Workbook::new()
84        .with_table_style(
85            TableStyle::new("CustomTableStyle")
86                .with_element(TableStyleElement::new(
87                    TableStyleElementType::HeaderRow,
88                    CellFormat::new()
89                        .with_bold(true)
90                        .with_fill_color("FF2E74B5")
91                        .with_font_color("FFFFFFFF"),
92                ))
93                .with_element(TableStyleElement::new(
94                    TableStyleElementType::FirstRowStripe,
95                    CellFormat::new().with_fill_color("FFF2F2F2"),
96                )),
97        )
98        .with_sheet(plain_sheet)
99        .with_sheet(calculated_sheet)
100        .with_sheet(styled_sheet);
101
102    workbook.save_to_file(&path)?;
103    println!("Wrote {}", path.display());
104    Ok(())
105}
More examples
Hide additional examples
examples/xlsx_conditional_formatting_and_validation.rs (line 29)
17fn main() -> office_toolkit::Result<()> {
18    let path = output_path("xlsx_conditional_formatting_and_validation.xlsx");
19
20    let cell_is_sheet = Sheet::new("Cell-is rule")
21        .with_row(Row::new().with_number(45.0))
22        .with_row(Row::new().with_number(72.0))
23        .with_row(Row::new().with_number(91.0))
24        .with_conditional_formatting_rule(ConditionalFormattingRule::cell_is(
25            "A1:A3",
26            ComparisonOperator::GreaterThan,
27            "70",
28            CellFormat::new()
29                .with_fill_color("FFC6EFCE")
30                .with_font_color("FF006100"),
31        ));
32
33    let scales_and_bars_sheet = Sheet::new("Color scale and data bar")
34        .with_row(Row::new().with_number(10.0))
35        .with_row(Row::new().with_number(50.0))
36        .with_row(Row::new().with_number(90.0))
37        .with_conditional_formatting_rule(ConditionalFormattingRule::color_scale(
38            "A1:A3",
39            vec![
40                ColorScaleStop::new(CfvoPosition::Min, "FFF8696B"),
41                ColorScaleStop::new(CfvoPosition::Max, "FF63BE7B"),
42            ],
43        ))
44        .with_row(Row::new().with_number(10.0))
45        .with_row(Row::new().with_number(50.0))
46        .with_row(Row::new().with_number(90.0))
47        .with_conditional_formatting_rule(ConditionalFormattingRule::data_bar(
48            "B1:B3",
49            CfvoPosition::Min,
50            CfvoPosition::Max,
51            "FF638EC6",
52        ));
53
54    let icon_set_and_ranking_sheet = Sheet::new("Icon set, top-10, duplicates")
55        .with_row(Row::new().with_number(1.0))
56        .with_row(Row::new().with_number(2.0))
57        .with_row(Row::new().with_number(3.0))
58        .with_conditional_formatting_rule(ConditionalFormattingRule::icon_set(
59            "A1:A3",
60            IconSetType::ThreeTrafficLights,
61        ))
62        .with_row(Row::new().with_number(4.0))
63        .with_row(Row::new().with_number(5.0))
64        .with_row(Row::new().with_number(6.0))
65        .with_conditional_formatting_rule(ConditionalFormattingRule::top10(
66            "B1:B3",
67            1,
68            false,
69            CellFormat::new().with_bold(true),
70        ))
71        .with_row(Row::new().with_text("A"))
72        .with_row(Row::new().with_text("A"))
73        .with_row(Row::new().with_text("B"))
74        .with_conditional_formatting_rule(ConditionalFormattingRule::duplicate_values(
75            "C1:C3",
76            CellFormat::new().with_fill_color("FFFFC7CE"),
77        ));
78
79    let data_validation_sheet = Sheet::new("Data validation")
80        .with_row(Row::new().with_text("Pick one"))
81        .with_data_validation_rule(DataValidationRule::list("A1:A1", "\"Small,Medium,Large\""))
82        .with_row(Row::new().with_number(0.0))
83        .with_data_validation_rule(
84            DataValidationRule::whole_number("B1:B1", ComparisonOperator::LessThanOrEqual, "100")
85                .with_input_message(
86                    Message::new("Enter a whole number from 0 to 100.").with_title("Valid range"),
87                )
88                .with_error_message(
89                    Message::new("That value is out of range.").with_title("Invalid entry"),
90                ),
91        );
92
93    let workbook = Workbook::new()
94        .with_sheet(cell_is_sheet)
95        .with_sheet(scales_and_bars_sheet)
96        .with_sheet(icon_set_and_ranking_sheet)
97        .with_sheet(data_validation_sheet);
98
99    workbook.save_to_file(&path)?;
100    println!("Wrote {}", path.display());
101    Ok(())
102}
examples/xlsx_cell_formatting.rs (line 35)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_horizontal_alignment( self, alignment: HorizontalAlignment, ) -> CellFormat

Sets horizontal alignment.

Examples found in repository?
examples/xlsx_cell_formatting.rs (line 76)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_vertical_alignment(self, alignment: VerticalAlignment) -> CellFormat

Sets vertical alignment.

Examples found in repository?
examples/xlsx_cell_formatting.rs (line 77)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_border(self, border: Border) -> CellFormat

Sets this cell’s borders.

Examples found in repository?
examples/xlsx_cell_formatting.rs (line 22)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_font_name(self, font_name: impl Into<String>) -> CellFormat

Sets the font name/family.

Source

pub fn with_underline(self, underline: bool) -> CellFormat

Sets underline.

Source

pub fn with_strike(self, strike: bool) -> CellFormat

Sets strikethrough.

Source

pub fn with_wrap_text(self, wrap_text: bool) -> CellFormat

Sets whether text wraps onto multiple lines within the cell.

Source

pub fn with_indent(self, indent: u32) -> CellFormat

Sets the indentation level.

Examples found in repository?
examples/xlsx_cell_formatting.rs (line 82)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_text_rotation(self, text_rotation: u16) -> CellFormat

Sets the raw Excel text rotation value (see text_rotation’s doc comment for the encoding).

Source

pub fn with_shrink_to_fit(self, shrink_to_fit: bool) -> CellFormat

Sets whether the font size shrinks to fit the cell’s width.

Source

pub fn with_pattern_fill(self, pattern_fill: PatternFill) -> CellFormat

Sets a non-solid pattern fill.

Examples found in repository?
examples/xlsx_cell_formatting.rs (lines 40-44)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_gradient_fill(self, gradient_fill: GradientFill) -> CellFormat

Sets a gradient fill.

Examples found in repository?
examples/xlsx_cell_formatting.rs (lines 50-56)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}
Source

pub fn with_locked(self, locked: bool) -> CellFormat

Sets whether this cell is locked when the sheet is protected.

Source

pub fn with_formula_hidden(self, formula_hidden: bool) -> CellFormat

Sets whether this cell’s formula is hidden when the sheet is protected.

Source

pub fn with_named_style(self, name: impl Into<String>) -> CellFormat

Links this format to a named cell style (by name, resolved against Workbook::named_cell_styles at write time) and returns it for chaining.

Examples found in repository?
examples/xlsx_cell_formatting.rs (line 99)
15fn main() -> office_toolkit::Result<()> {
16    let path = output_path("xlsx_cell_formatting.xlsx");
17
18    let borders_sheet =
19        Sheet::new("Borders").with_row(
20            Row::new()
21                .with_cell(Cell::text("Thin all around").with_format(
22                    CellFormat::new().with_border(Border::all(BorderStyle::Thin, "FF000000")),
23                ))
24                .with_cell(Cell::text("Thick bottom only").with_format(
25                    CellFormat::new().with_border(
26                        Border::new().with_bottom(
27                            BorderEdge::new(BorderStyle::Thick).with_color("FFC00000"),
28                        ),
29                    ),
30                )),
31        );
32
33    let fills_sheet = Sheet::new("Fills")
34        .with_row(Row::new().with_cell(
35            Cell::text("Solid fill").with_format(CellFormat::new().with_fill_color("FFFFF2CC")),
36        ))
37        .with_row(
38            Row::new().with_cell(
39                Cell::text("Pattern fill").with_format(
40                    CellFormat::new().with_pattern_fill(
41                        PatternFill::new(PatternType::LightGray)
42                            .with_fg_color("FF4472C4")
43                            .with_bg_color("FFFFFFFF"),
44                    ),
45                ),
46            ),
47        )
48        .with_row(
49            Row::new().with_cell(Cell::text("Gradient fill").with_format(
50                CellFormat::new().with_gradient_fill(GradientFill::new(
51                    90.0,
52                    vec![
53                        GradientStop::new(0.0, "FFFFFFFF"),
54                        GradientStop::new(1.0, "FF4472C4"),
55                    ],
56                )),
57            )),
58        );
59
60    let fonts_and_alignment_sheet =
61        Sheet::new("Fonts and alignment")
62            .with_row(
63                Row::new().with_cell(
64                    Cell::text("Bold, red, 14pt").with_format(
65                        CellFormat::new()
66                            .with_bold(true)
67                            .with_font_size(14.0)
68                            .with_font_color("FFC00000"),
69                    ),
70                ),
71            )
72            .with_row(
73                Row::new().with_cell(
74                    Cell::text("Centered both ways").with_format(
75                        CellFormat::new()
76                            .with_horizontal_alignment(HorizontalAlignment::Center)
77                            .with_vertical_alignment(VerticalAlignment::Center),
78                    ),
79                ),
80            )
81            .with_row(Row::new().with_cell(
82                Cell::text("Indented text").with_format(CellFormat::new().with_indent(2)),
83            ));
84
85    let number_formats_sheet = Sheet::new("Number formats")
86        .with_row(Row::new().with_cell(
87            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("#,##0.00")),
88        ))
89        .with_row(Row::new().with_cell(
90            Cell::number(0.0825).with_format(CellFormat::new().with_number_format("0.00%")),
91        ))
92        .with_row(Row::new().with_cell(
93            Cell::number(1234.5).with_format(CellFormat::new().with_number_format("$#,##0.00")),
94        ));
95
96    let named_styles_sheet = Sheet::new("Named styles").with_row(
97        Row::new().with_cell(
98            Cell::text("Uses a named style")
99                .with_format(CellFormat::new().with_named_style("Emphasis")),
100        ),
101    );
102
103    let workbook = Workbook::new()
104        .with_named_cell_style(NamedCellStyle::new(
105            "Emphasis",
106            CellFormat::new()
107                .with_bold(true)
108                .with_font_color("FF4472C4"),
109        ))
110        .with_sheet(borders_sheet)
111        .with_sheet(fills_sheet)
112        .with_sheet(fonts_and_alignment_sheet)
113        .with_sheet(number_formats_sheet)
114        .with_sheet(named_styles_sheet);
115
116    workbook.save_to_file(&path)?;
117    println!("Wrote {}", path.display());
118    Ok(())
119}

Trait Implementations§

Source§

impl Clone for CellFormat

Source§

fn clone(&self) -> CellFormat

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CellFormat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for CellFormat

Source§

fn default() -> CellFormat

Returns the “default value” for a type. Read more
Source§

impl PartialEq for CellFormat

Source§

fn eq(&self, other: &CellFormat) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for CellFormat

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.