anatomy/
anatomy.rs

1//! Anatomy of a simple karo program.
2
3use karo::{col_range, index, Format, NumFormat, Workbook};
4
5fn main() -> karo::Result<()> {
6    // Create a new workbook.
7    let mut workbook = Workbook::new();
8
9    // Add some cell formats.
10    let mut myformat1 = Format::default();
11    let mut myformat2 = Format::default();
12
13    // Set the bold property for the first format.
14    myformat1.font.bold = true;
15
16    // Set a number format for the second format.
17    myformat2.num_format = NumFormat::Custom("$#,##0.00".to_string());
18
19    {
20        // Add a worksheet with a user defined name.
21        let worksheet1 = workbook.add_worksheet(Some("Demo"))?;
22
23        // Widen the first column to make the text clearer.
24        worksheet1.set_column(col_range(0, 0)?, 20.0, None)?;
25
26        // Write some unformatted data.
27        worksheet1.write_string(index(0, 0)?, "Peach", None)?;
28        worksheet1.write_string(index(1, 0)?, "Plum", None)?;
29
30        // Write formatted data.
31        worksheet1.write_string(index(2, 0)?, "Pear", Some(&myformat1))?;
32
33        // Formats can be reused.
34        worksheet1.write_string(
35            index(3, 0)?,
36            "Persimmon",
37            Some(&myformat1),
38        )?;
39
40        // Write some numbers.
41        worksheet1.write_number(index(5, 0)?, 123.0, None)?;
42        worksheet1.write_number(
43            index(6, 0)?,
44            4567.555,
45            Some(&myformat2),
46        )?;
47    }
48
49    {
50        let worksheet2 = workbook.add_worksheet(None)?;
51
52        worksheet2.write_string(
53            index(0, 0)?,
54            "Some text",
55            Some(&myformat1),
56        )?;
57    }
58
59    workbook.write_file("anatomy.xlsx")?;
60
61    Ok(())
62}