panes/
panes.rs

1use edit_xlsx::{WorkSheetCol, Format, FormatAlignType, FormatBorderType, FormatColor, WorkSheetRow, Workbook, WorkbookResult, Write};
2
3fn main() -> WorkbookResult<()> {
4    let header_format = Format::default()
5        .set_bold()
6        .set_align(FormatAlignType::Center)
7        .set_align(FormatAlignType::VerticalCenter)
8        .set_border(FormatBorderType::Medium)
9        .set_background_color(FormatColor::RGB(126, 75, 12));
10    let center_format = Format::default().set_align(FormatAlignType::Center);
11
12    // Create a new workbook
13    let mut workbook = Workbook::new();
14
15    //
16    // Example 1. Freeze pane on the top row.
17    //
18    let worksheet1 = workbook.add_worksheet_by_name("Panes 1")?;
19    worksheet1.freeze_panes("A2")?;
20    // Other sheet formatting.
21    worksheet1.set_columns_width("A:I", 16.0)?;
22    worksheet1.set_row_height(0, 20.0)?;
23    worksheet1.set_selection("C3:C3")?;
24
25    // Some text to demonstrate scrolling.
26    for col in 1..=9 {
27        worksheet1.write_with_format((1, col), "Scroll down", &header_format)?;
28    }
29
30    for row in 2..100 {
31        for col in 1..=9 {
32            worksheet1.write_with_format((row, col), row, &center_format)?;
33        }
34    }
35
36    //
37    // Example 2. Freeze pane on the left column.
38    //
39    let worksheet2 = workbook.add_worksheet_by_name("Panes 2")?;
40    worksheet2.freeze_panes("B1")?;
41
42    // Other sheet formatting.
43    worksheet2.set_columns_width("A:A", 16.0)?;
44    worksheet2.set_selection("C3:C3")?;
45
46    // Some text to demonstrate scrolling.
47    for row in 1..=50 {
48        worksheet2.write_with_format((row, 1), "Scroll right", &header_format)?;
49        for col in 2..=26 {
50            worksheet2.write_with_format((row, col), col, &center_format)?;
51        }
52    }
53
54    //
55    // Example 3. Freeze pane on the top row and left column.
56    //
57    let worksheet3 = workbook.add_worksheet_by_name("Panes 3")?;
58    worksheet3.freeze_panes((2, 2))?;
59
60    // Other sheet formatting.
61    worksheet3.set_columns_width("A:Z", 16.0)?;
62    worksheet3.set_row_height(1, 20.0)?;
63    worksheet3.set_selection("C3:C3")?;
64    worksheet3.write_with_format((1, 1), "", &header_format)?;
65
66    // Some text to demonstrate scrolling.
67    for col in 2..=26 {
68        worksheet3.write_with_format((1, col), "Scroll down", &header_format)?;
69    }
70
71    for row in 2..=50 {
72        worksheet3.write_with_format((row, 1), "Scroll right", &header_format)?;
73        for col in 2..=26 {
74            worksheet3.write_with_format((row, col), col, &center_format)?;
75        }
76    }
77
78    //
79    // Example 4. Split pane on the top row and left column.
80    //
81    // The divisions must be specified in terms of row and column dimensions.
82    //
83    let worksheet4 = workbook.add_worksheet_by_name("Panes 4")?;
84    // Set the default row height is 17 and set the column width is 13
85    worksheet4.set_columns_width("A:Z", 13.0)?;
86    worksheet4.set_default_row(17.0);
87    worksheet4.split_panes(2.0 * 13.0, 2.0 * 17.0)?;
88    worksheet4.set_selection("C3:C3")?;
89
90    // Some text to demonstrate scrolling.
91    for col in 1..=26 {
92        worksheet4.write_with_format((1, col), "Scroll", &center_format)?;
93    }
94    for row in 1..=50 {
95        worksheet4.write_with_format((row, 1), "Scroll", &center_format)?;
96        for col in 1..=26 {
97            worksheet4.write_with_format((row, col), col, &center_format)?;
98        }
99    }
100
101    workbook.save_as("examples/panes.xlsx")?;
102    Ok(())
103}