pub struct Format {
pub font: FormatFont,
pub border: FormatBorder,
pub fill: FormatFill,
pub align: FormatAlign,
}
Expand description
Format
struct, which used to edit the style of the Cell
.
§Fields
field | type | meaning |
---|---|---|
font | FormatFont | The Cell ’s font formats |
border | FormatBorder | The Cell ’s border formats |
fill | FormatFill | The Cell ’s fill(background) formats |
align | FormatAlign | The Cell ’s align formats |
Fields§
§font: FormatFont
§border: FormatBorder
§fill: FormatFill
§align: FormatAlign
Implementations§
Source§impl Format
impl Format
Sourcepub fn is_bold(&self) -> bool
pub fn is_bold(&self) -> bool
Checks whether the font is bold, learn more about it in FormatFont
.
§Returns
Returns true
if the font is bold, false
otherwise.
Sourcepub fn is_italic(&self) -> bool
pub fn is_italic(&self) -> bool
Checks whether the font is italic, learn more about it in FormatFont
.
§Returns
Returns true
if the font is italic, false
otherwise.
Sourcepub fn is_underline(&self) -> bool
pub fn is_underline(&self) -> bool
Checks whether the font is underline, learn more about it in FormatFont
.
§Returns
Returns true
if the font is underline, false
otherwise.
Sourcepub fn get_size(&self) -> f64
pub fn get_size(&self) -> f64
Retrieves the size of the font, learn more about it in FormatFont
.
§Returns
Returns the pounds of the font size.
Sourcepub fn get_background(&self) -> &FormatFill
pub fn get_background(&self) -> &FormatFill
Examples found in repository?
99pub fn xlsx_convert(
100 in_file_name: &Path,
101 out_file_name: &Path,
102) -> Result<Xlsx2AdocTestResults, Error> {
103 let workbook = Workbook::from_path(in_file_name);
104 let mut workbook = workbook.unwrap();
105 workbook.finish();
106
107 let reading_sheet = workbook.get_worksheet(1);
108 let sheet = reading_sheet.unwrap();
109 let default_row_hight = sheet.get_default_row();
110
111 println!(
112 "Rows {} -> ? ( {} )",
113 sheet.max_row(),
114 // formatted_row.len(),
115 default_row_hight
116 );
117
118 let mut hights = Vec::<f64>::new();
119 for _ in 0..sheet.max_row() {
120 hights.push(default_row_hight);
121 }
122
123 let widths = find_col_width(sheet)?;
124 let bounds = "|===\r";
125 let line = tab_header(&widths);
126
127 let mut output_file = File::create(out_file_name)?; // overwrites existing file
128 let mut writer = BufWriter::new(&mut output_file);
129
130 writer.write(line.as_bytes())?;
131 writer.write(bounds.as_bytes())?;
132
133 /* // todo test
134 writer.write("|1|2|3\r".as_bytes())?;
135 writer.write("|4|5|6\r".as_bytes())?;
136
137 writer.write(bounds.as_bytes())?;
138 */
139 for row in 0..sheet.max_row() {
140 println!("Row {} ({})", row, hights[row as usize]);
141 for col in 0..sheet.max_column() {
142 if col < sheet.max_column() {
143 writer.write("|".as_bytes())?;
144 }
145
146 let cell_content = sheet.read_cell((row + 1, col + 1)).unwrap_or_default();
147 let format = cell_content.format;
148 let mut text_color = FormatColor::Default;
149 let mut bg_color = FormatColor::Default;
150 let mut bg_bg_color = FormatColor::Default;
151 if format.is_some() {
152 let format = format.unwrap();
153 text_color = format.get_color().clone();
154 let ff = format.get_background().clone();
155 bg_color = ff.fg_color.clone();
156 bg_bg_color = ff.bg_color.clone();
157 }
158
159 let cell_format_string = format!(
160 "Text-Color = {:?} bg = {:?} bg_bg = {:?}",
161 text_color, bg_color, bg_bg_color
162 );
163 let cell_text = cell_content.text;
164 let text = match cell_text {
165 Some(t) => t,
166 None => "-".to_owned(),
167 };
168
169 println!(
170 "{} ({}) -> {} Format: {}",
171 col,
172 widths[(col) as usize],
173 text,
174 cell_format_string
175 );
176 writer.write(text.as_bytes())?;
177 }
178
179 writer.write("\r".as_bytes())?;
180 }
181 writer.write(bounds.as_bytes())?;
182
183 let xlsx_2_adoc_test_results = Xlsx2AdocTestResults { v1: 0, v2: 0 };
184 Ok(xlsx_2_adoc_test_results)
185}
Sourcepub fn get_borders(&self) -> &FormatBorder
pub fn get_borders(&self) -> &FormatBorder
Retrieves the borders of the cell, learn more about it in FormatBorder
.
§Returns
Returns the FormatBorder
of the cell.
Sourcepub fn get_color(&self) -> &FormatColor
pub fn get_color(&self) -> &FormatColor
Retrieves the color of the font, learn more about it in FormatFont
.
§Returns
Returns the FormatColor
of the font.
Examples found in repository?
99pub fn xlsx_convert(
100 in_file_name: &Path,
101 out_file_name: &Path,
102) -> Result<Xlsx2AdocTestResults, Error> {
103 let workbook = Workbook::from_path(in_file_name);
104 let mut workbook = workbook.unwrap();
105 workbook.finish();
106
107 let reading_sheet = workbook.get_worksheet(1);
108 let sheet = reading_sheet.unwrap();
109 let default_row_hight = sheet.get_default_row();
110
111 println!(
112 "Rows {} -> ? ( {} )",
113 sheet.max_row(),
114 // formatted_row.len(),
115 default_row_hight
116 );
117
118 let mut hights = Vec::<f64>::new();
119 for _ in 0..sheet.max_row() {
120 hights.push(default_row_hight);
121 }
122
123 let widths = find_col_width(sheet)?;
124 let bounds = "|===\r";
125 let line = tab_header(&widths);
126
127 let mut output_file = File::create(out_file_name)?; // overwrites existing file
128 let mut writer = BufWriter::new(&mut output_file);
129
130 writer.write(line.as_bytes())?;
131 writer.write(bounds.as_bytes())?;
132
133 /* // todo test
134 writer.write("|1|2|3\r".as_bytes())?;
135 writer.write("|4|5|6\r".as_bytes())?;
136
137 writer.write(bounds.as_bytes())?;
138 */
139 for row in 0..sheet.max_row() {
140 println!("Row {} ({})", row, hights[row as usize]);
141 for col in 0..sheet.max_column() {
142 if col < sheet.max_column() {
143 writer.write("|".as_bytes())?;
144 }
145
146 let cell_content = sheet.read_cell((row + 1, col + 1)).unwrap_or_default();
147 let format = cell_content.format;
148 let mut text_color = FormatColor::Default;
149 let mut bg_color = FormatColor::Default;
150 let mut bg_bg_color = FormatColor::Default;
151 if format.is_some() {
152 let format = format.unwrap();
153 text_color = format.get_color().clone();
154 let ff = format.get_background().clone();
155 bg_color = ff.fg_color.clone();
156 bg_bg_color = ff.bg_color.clone();
157 }
158
159 let cell_format_string = format!(
160 "Text-Color = {:?} bg = {:?} bg_bg = {:?}",
161 text_color, bg_color, bg_bg_color
162 );
163 let cell_text = cell_content.text;
164 let text = match cell_text {
165 Some(t) => t,
166 None => "-".to_owned(),
167 };
168
169 println!(
170 "{} ({}) -> {} Format: {}",
171 col,
172 widths[(col) as usize],
173 text,
174 cell_format_string
175 );
176 writer.write(text.as_bytes())?;
177 }
178
179 writer.write("\r".as_bytes())?;
180 }
181 writer.write(bounds.as_bytes())?;
182
183 let xlsx_2_adoc_test_results = Xlsx2AdocTestResults { v1: 0, v2: 0 };
184 Ok(xlsx_2_adoc_test_results)
185}
Sourcepub fn get_font(&self) -> &str
pub fn get_font(&self) -> &str
Retrieves the name of the font, learn more about it in FormatFont
.
§Returns
Returns the name of the font.
Sourcepub fn get_background_color(&self) -> &FormatColor
pub fn get_background_color(&self) -> &FormatColor
Retrieves the color of the cell’s background.
§Returns
Returns the FormatColor
of the cell’s background.
Sourcepub fn get_border_left(&self) -> &FormatBorderElement
pub fn get_border_left(&self) -> &FormatBorderElement
Retrieves the left border of the cell, learn more about it in FormatBorder
.
§Returns
Returns the FormatBorderElement
of the cell’s left border.
Sourcepub fn get_border_right(&self) -> &FormatBorderElement
pub fn get_border_right(&self) -> &FormatBorderElement
Retrieves the right border of the cell, learn more about it in FormatBorder
.
§Returns
Returns the FormatBorderElement
of the cell’s right border.
Sourcepub fn get_border_top(&self) -> &FormatBorderElement
pub fn get_border_top(&self) -> &FormatBorderElement
Retrieves the top border of the cell, learn more about it in FormatBorder
.
§Returns
Returns the FormatBorderElement
of the cell’s top border.
Sourcepub fn get_border_bottom(&self) -> &FormatBorderElement
pub fn get_border_bottom(&self) -> &FormatBorderElement
Retrieves the bottom border of the cell, learn more about it in FormatBorder
.
§Returns
Returns the FormatBorderElement
of the cell’s bottom border.
Source§impl Format
impl Format
Sourcepub fn set_bold(self) -> Self
pub fn set_bold(self) -> Self
Set the font to bold, learn more about it in FormatFont
.
§Returns
Returns the modified format with font bold set to true.
Examples found in repository?
3fn main() -> WorkbookResult<()> {
4 let mut workbook = Workbook::new();
5 let worksheet = workbook.get_worksheet_mut(1)?;
6
7 // Increase the cell size of the merged cells to highlight the formatting.
8 worksheet.set_columns_width("B:D", 18.0)?;
9 worksheet.set_row_height(4, 40.0)?;
10 worksheet.set_row_height(7, 30.0)?;
11 worksheet.set_row_height(8, 30.0)?;
12
13 // Create a format to use in the merged range.
14 let merge_format = Format::default()
15 .set_bold()
16 .set_border(FormatBorderType::Double)
17 .set_align(FormatAlignType::Center)
18 .set_align(FormatAlignType::VerticalCenter)
19 .set_background_color(FormatColor::RGB(255, 255, 0));
20
21 // Merge 3 cells.
22 worksheet.merge_range_with_format("B4:D4", "Merged Range", &merge_format)?;
23 // Merge 3 cells over two rows.
24 worksheet.merge_range_with_format("B7:D8", "Merged Range", &merge_format)?;
25
26 workbook.save_as("examples/merge.xlsx")?;
27 Ok(())
28}
More examples
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7 // write some text
8 WorkSheet::write(worksheet, "A1", "Hello")?;
9 worksheet.write("B1", "World")?;
10 worksheet.write("C1", "Rust")?;
11 // Adjust font size
12 let big = Format::default().set_size(32);
13 worksheet.write_with_format("B1", "big text", &big)?;
14 // Change font color
15 let red = Format::default().set_color(FormatColor::RGB(255, 119, 119));
16 worksheet.write_with_format("C1", "red text", &red)?;
17 // Change the font style
18 let bold = red.set_bold();
19 worksheet.write_with_format("D1", "red bold text", &bold)?;
20 // Change font
21 let font = Format::default().set_font("华文行楷");
22 worksheet.write_with_format("E1", "你好", &font)?;
23 // adjust the text align
24 let left_top = Format::default().set_align(FormatAlignType::Left).set_align(FormatAlignType::Top);
25 worksheet.write_with_format("A2", "left top", &left_top)?;
26 // add borders
27 let thin_border = Format::default().set_border(FormatBorderType::Thin);
28 worksheet.write_with_format("B2", "bordered text", &thin_border)?;
29 // add background
30 let red_background = Format::default().set_background_color(FormatColor::RGB(255, 119, 119));
31 worksheet.write_with_format("C2", "red", &red_background)?;
32 // add a number
33 worksheet.write("D2", std::f64::consts::PI)?;
34 // add a new worksheet and set a tab color
35 let worksheet = workbook.add_worksheet_by_name("Other examples")?;
36 worksheet.set_tab_color(&FormatColor::RGB(255, 153, 0)); // Orange
37 // Set a background.
38 worksheet.set_background("examples/pics/ferris.png")?;
39 // Create a format to use in the merged range.
40 let merge_format = Format::default()
41 .set_bold()
42 .set_border(FormatBorderType::Double)
43 .set_align(FormatAlignType::Center)
44 .set_align(FormatAlignType::VerticalCenter)
45 .set_background_color(FormatColor::RGB(255, 255, 0));
46 // Merge cells.
47 worksheet.merge_range_with_format("A1:C3", "Merged Range", &merge_format)?;
48 // Add an image
49 worksheet.insert_image("A4:C10", &"./examples/pics/rust.png")?;
50 workbook.save_as("examples/hello_world.xlsx")?;
51 Ok(())
52}
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, ¢er_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, ¢er_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, ¢er_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", ¢er_format)?;
93 }
94 for row in 1..=50 {
95 worksheet4.write_with_format((row, 1), "Scroll", ¢er_format)?;
96 for col in 1..=26 {
97 worksheet4.write_with_format((row, col), col, ¢er_format)?;
98 }
99 }
100
101 workbook.save_as("examples/panes.xlsx")?;
102 Ok(())
103}
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 // Add a general format
7 let bold = Format::default().set_bold();
8
9 //
10 // Example 1: A worksheet with outlined rows. It also includes SUBTOTAL()
11 // functions so that it looks like the type of automatic outlines that are
12 // generated when you use the Excel Data->SubTotals menu item.
13 //
14 let worksheet1 = workbook.add_worksheet_by_name("Outlined Rows")?;
15 worksheet1.set_row_level(2, 2)?;
16 worksheet1.set_row_level(3, 2)?;
17 worksheet1.set_row_level(4, 2)?;
18 worksheet1.set_row_level(5, 2)?;
19 worksheet1.set_row_level(6, 1)?;
20 worksheet1.set_row_level(7, 2)?;
21 worksheet1.set_row_level(8, 2)?;
22 worksheet1.set_row_level(9, 2)?;
23 worksheet1.set_row_level(10, 2)?;
24 worksheet1.set_row_level(11, 1)?;
25 create_sub_totals(worksheet1)?;
26
27 //
28 // Example 2: Create a worksheet with collapsed outlined rows.
29 // This is the same as the example 1 except that the all rows are collapsed.
30 // Note: We need to indicate the rows that contains the collapsed symbol '+'
31 // with the optional parameter, 'collapsed'.
32 //
33 let worksheet2 = workbook.add_worksheet_by_name("Collapsed Rows 1")?;
34 worksheet2.set_row_level(2, 2)?;
35 worksheet2.hide_row(2)?;
36 worksheet2.set_row_level(3, 2)?;
37 worksheet2.hide_row(3)?;
38 worksheet2.set_row_level(4, 2)?;
39 worksheet2.hide_row(4)?;
40 worksheet2.set_row_level(5, 2)?;
41 worksheet2.hide_row(5)?;
42 worksheet2.set_row_level(6, 1)?;
43 worksheet2.hide_row(6)?;
44 worksheet2.set_row_level(7, 2)?;
45 worksheet2.hide_row(7)?;
46 worksheet2.set_row_level(8, 2)?;
47 worksheet2.hide_row(8)?;
48 worksheet2.set_row_level(9, 2)?;
49 worksheet2.hide_row(9)?;
50 worksheet2.set_row_level(10, 2)?;
51 worksheet2.hide_row(10)?;
52 worksheet2.set_row_level(11, 1)?;
53 worksheet2.hide_row(11)?;
54
55 worksheet2.collapse_row(12)?;
56
57 // Write the sub-total data that is common to the row examples.
58 create_sub_totals(worksheet2)?;
59
60
61 //
62 // Example 3: Create a worksheet with collapsed outlined rows.
63 // Same as the example 1 except that the two sub-totals are collapsed.
64 //
65 let worksheet3 = workbook.add_worksheet_by_name("Collapsed Rows 2")?;
66 worksheet3.set_row_level(2, 2)?;
67 worksheet3.hide_row(2)?;
68 worksheet3.set_row_level(3, 2)?;
69 worksheet3.hide_row(3)?;
70 worksheet3.set_row_level(4, 2)?;
71 worksheet3.hide_row(4)?;
72 worksheet3.set_row_level(5, 2)?;
73 worksheet3.hide_row(5)?;
74 worksheet3.set_row_level(6, 1)?;
75 worksheet3.collapse_row(6)?;
76 worksheet3.set_row_level(7, 2)?;
77 worksheet3.hide_row(7)?;
78 worksheet3.set_row_level(8, 2)?;
79 worksheet3.hide_row(8)?;
80 worksheet3.set_row_level(9, 2)?;
81 worksheet3.hide_row(9)?;
82 worksheet3.set_row_level(10, 2)?;
83 worksheet3.hide_row(10)?;
84 worksheet3.set_row_level(11, 1)?;
85 worksheet3.collapse_row(11)?;
86 // Write the sub-total data that is common to the row examples.
87 create_sub_totals(worksheet3)?;
88
89
90 //
91 // Example 4: Create a worksheet with outlined rows.
92 // Same as the example 1 except that the two sub-totals are collapsed.
93 //
94 let worksheet4 = workbook.add_worksheet_by_name("Collapsed Rows 3")?;
95 worksheet4.set_row_level(2, 2)?;
96 worksheet4.hide_row(2)?;
97 worksheet4.set_row_level(3, 2)?;
98 worksheet4.hide_row(3)?;
99 worksheet4.set_row_level(4, 2)?;
100 worksheet4.hide_row(4)?;
101 worksheet4.set_row_level(5, 2)?;
102 worksheet4.hide_row(5)?;
103 worksheet4.set_row_level(6, 1)?;
104 worksheet4.hide_row(6)?;
105 worksheet4.collapse_row(6)?;
106 worksheet4.set_row_level(7, 2)?;
107 worksheet4.hide_row(7)?;
108 worksheet4.set_row_level(8, 2)?;
109 worksheet4.hide_row(8)?;
110 worksheet4.set_row_level(9, 2)?;
111 worksheet4.hide_row(9)?;
112 worksheet4.set_row_level(10, 2)?;
113 worksheet4.hide_row(10)?;
114 worksheet4.set_row_level(11, 1)?;
115 worksheet4.hide_row(11)?;
116 worksheet4.collapse_row(11)?;
117 worksheet4.collapse_row(12)?;
118 // Write the sub-total data that is common to the row examples.
119 create_sub_totals(worksheet4)?;
120
121
122
123 //
124 // Example 5: Create a worksheet with outlined columns.
125 //
126 let worksheet5 = workbook.add_worksheet_by_name("Outline Columns")?;
127 worksheet5.set_row_height_with_format(1, 15.0, &bold)?;
128 worksheet5.write_row("A1", &["Month", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Total"])?;
129 worksheet5.set_columns_width_with_format("A:A", 10.0, &bold)?;
130 worksheet5.set_columns_width("B:G", 5.0)?;
131 worksheet5.set_columns_level("B:G", 1)?;
132 worksheet5.set_columns_width("H:H", 10.0)?;
133 worksheet5.write_column("A2", &["North", "South", "East", "East"])?;
134 worksheet5.write_row("B2", &[50, 20, 15, 25, 65, 80])?;
135 worksheet5.write_row("B3", &[10, 20, 30, 50, 50, 50])?;
136 worksheet5.write_row("B4", &[45, 75, 50, 15, 75, 100])?;
137 worksheet5.write_row("B5", &[15, 15, 55, 35, 20, 50])?;
138 worksheet5.write_formula("H2", "=SUM(B2:G2)")?;
139 worksheet5.write_formula("H3", "=SUM(B3:G3)")?;
140 worksheet5.write_formula("H4", "=SUM(B4:G4)")?;
141 worksheet5.write_formula("H5", "=SUM(B5:G5)")?;
142 worksheet5.write_formula_with_format("H6", "=SUM(H2:H5)", &bold)?;
143
144
145
146 //
147 // Example 6: Create a worksheet with collapsed outlined columns.
148 // This is the same as the previous example except with collapsed columns.
149 //
150 let worksheet6 = workbook.add_worksheet_by_name("Collapsed Columns")?;
151 worksheet6.set_row_height_with_format(1, 15.0, &bold)?;
152 worksheet6.write_row("A1", &["Month", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Total"])?;
153 worksheet6.set_columns_width_with_format("A:H", 10.0, &bold)?;
154 worksheet6.set_columns_level("B:G", 1)?;
155 worksheet6.set_columns_level("C:F", 2)?;
156 worksheet6.set_columns_level("D:E", 3)?;
157 worksheet6.hide_columns("D:E")?;
158 worksheet6.collapse_columns("D:E")?;
159 worksheet6.write_column("A2", &["North", "South", "East", "East"])?;
160 worksheet6.write_row("B2", &[50, 20, 15, 25, 65, 80])?;
161 worksheet6.write_row("B3", &[10, 20, 30, 50, 50, 50])?;
162 worksheet6.write_row("B4", &[45, 75, 50, 15, 75, 100])?;
163 worksheet6.write_row("B5", &[15, 15, 55, 35, 20, 50])?;
164 worksheet6.write_formula("H2", "=SUM(B2:G2)")?;
165 worksheet6.write_formula("H3", "=SUM(B3:G3)")?;
166 worksheet6.write_formula("H4", "=SUM(B4:G4)")?;
167 worksheet6.write_formula("H5", "=SUM(B5:G5)")?;
168 worksheet6.write_formula_with_format("H6", "=SUM(H2:H5)", &bold)?;
169
170
171 workbook.save_as("examples/outline_collapsed.xlsx")?;
172 Ok(())
173}
174
175fn create_sub_totals(worksheet: &mut WorkSheet) -> WorkbookResult<()> {
176 // Add a general format
177 let bold = Format::default().set_bold();
178
179 // Add the data, labels and formulas
180 worksheet.write_with_format("A1", "Region", &bold)?;
181 worksheet.write("A2", "North")?;
182 worksheet.write("A3", "North")?;
183 worksheet.write("A4", "North")?;
184 worksheet.write("A5", "North")?;
185 worksheet.write_with_format("A6", "North Total", &bold)?;
186
187 worksheet.write_with_format("B1", "Sales", &bold)?;
188 worksheet.write("B2", 1000)?;
189 worksheet.write("B3", 1200)?;
190 worksheet.write("B4", 900)?;
191 worksheet.write("B5", 1200)?;
192 worksheet.write_formula_with_format("B6", "=SUBTOTAL(9,B2:B5)", &bold)?;
193 worksheet.write("A7", "South")?;
194 worksheet.write("A8", "South")?;
195 worksheet.write("A9", "South")?;
196 worksheet.write("A10", "South")?;
197 worksheet.write_with_format("A11", "South Total", &bold)?;
198 worksheet.write("B7", 400)?;
199 worksheet.write("B8", 600)?;
200 worksheet.write("B9", 500)?;
201 worksheet.write("B10", 600)?;
202 worksheet.write_formula_with_format("B11", "=SUBTOTAL(9,B7:B10)", &bold)?;
203 worksheet.write_with_format("A12", "Grand Total", &bold)?;
204 worksheet.write_formula_with_format("B12", "=SUBTOTAL(9,B2:B10)", &bold)?;
205 Ok(())
206}
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet1 = workbook.add_worksheet_by_name("Outlined Rows")?;
7 // Add a general format
8 let bold = Format::default().set_bold();
9
10 //
11 // Example 1: A worksheet with outlined rows. It also includes SUBTOTAL()?; // functions so that it looks like the type of automatic outlines that are
12 // generated when you use the Excel Data->SubTotals menu item.
13 //
14 // For outlines the important parameters are 'level' and 'hidden'. Rows with
15 // the same 'level' are grouped together. The group will be collapsed if
16 // 'hidden' is enabled. The parameters 'height' and 'cell_format' are assigned
17 // default values if they are None.
18 //
19 worksheet1.set_row_level(2, 2)?;
20 worksheet1.set_row_level(3, 2)?;
21 worksheet1.set_row_level(4, 2)?;
22 worksheet1.set_row_level(5, 2)?;
23 worksheet1.set_row_level(6, 1)?;
24 worksheet1.set_row_level(7, 2)?;
25 worksheet1.set_row_level(8, 2)?;
26 worksheet1.set_row_level(9, 2)?;
27 worksheet1.set_row_level(10, 2)?;
28 worksheet1.set_row_level(11, 1)?;
29
30 // Adjust the column width for clarity
31 worksheet1.set_columns_width("A:A", 20.0)?;
32
33 // Add the data, labels and formulas
34 worksheet1.write_with_format("A1", "Region", &bold)?;
35 worksheet1.write("A2", "North")?;
36 worksheet1.write("A3", "North")?;
37 worksheet1.write("A4", "North")?;
38 worksheet1.write("A5", "North")?;
39 worksheet1.write_with_format("A6", "North Total", &bold)?;
40
41 worksheet1.write_with_format("B1", "Sales", &bold)?;
42 worksheet1.write("B2", 1000)?;
43 worksheet1.write("B3", 1200)?;
44 worksheet1.write("B4", 900)?;
45 worksheet1.write("B5", 1200)?;
46 worksheet1.write_formula_with_format("B6", "=SUBTOTAL(9,B2:B5)", &bold)?;
47 worksheet1.write("A7", "South")?;
48 worksheet1.write("A8", "South")?;
49 worksheet1.write("A9", "South")?;
50 worksheet1.write("A10", "South")?;
51 worksheet1.write_with_format("A11", "South Total", &bold)?;
52 worksheet1.write("B7", 400)?;
53 worksheet1.write("B8", 600)?;
54 worksheet1.write("B9", 500)?;
55 worksheet1.write("B10", 600)?;
56 worksheet1.write_formula_with_format("B11", "=SUBTOTAL(9,B7:B10)", &bold)?;
57 worksheet1.write_with_format("A12", "Grand Total", &bold)?;
58 worksheet1.write_formula_with_format("B12", "=SUBTOTAL(9,B2:B10)", &bold)?;
59
60
61 //
62 // Example 2: A worksheet with outlined rows. This is the same as the
63 // previous example except that the rows are collapsed.
64 // Note: We need to indicate the rows that contains the collapsed symbol '+'
65 // with the optional parameter, 'collapsed'. The group will be then be
66 // collapsed if 'hidden' is True.
67 //
68 let worksheet2 = workbook.add_worksheet_by_name("Collapsed Rows")?;
69 worksheet2.set_row_level(2, 2)?;
70 worksheet2.hide_row(2)?;
71 worksheet2.set_row_level(3, 2)?;
72 worksheet2.hide_row(3)?;
73 worksheet2.set_row_level(4, 2)?;
74 worksheet2.hide_row(4)?;
75 worksheet2.set_row_level(5, 2)?;
76 worksheet2.hide_row(5)?;
77 worksheet2.set_row_level(6, 1)?;
78 worksheet2.hide_row(6)?;
79 worksheet2.set_row_level(7, 2)?;
80 worksheet2.hide_row(7)?;
81 worksheet2.set_row_level(8, 2)?;
82 worksheet2.hide_row(8)?;
83 worksheet2.set_row_level(9, 2)?;
84 worksheet2.hide_row(9)?;
85 worksheet2.set_row_level(10, 2)?;
86 worksheet2.hide_row(10)?;
87 worksheet2.set_row_level(11, 1)?;
88 worksheet2.hide_row(11)?;
89 worksheet2.collapse_row(12)?;
90
91 // Adjust the column width for clarity
92 worksheet2.set_columns_width("A:A", 20.0)?;
93
94 // Add the data, labels and formulas
95 worksheet2.write_with_format("A1", "Region", &bold)?;
96 worksheet2.write("A2", "North")?;
97 worksheet2.write("A3", "North")?;
98 worksheet2.write("A4", "North")?;
99 worksheet2.write("A5", "North")?;
100 worksheet2.write_with_format("A6", "North Total", &bold)?;
101
102 worksheet2.write_with_format("B1", "Sales", &bold)?;
103 worksheet2.write("B2", 1000)?;
104 worksheet2.write("B3", 1200)?;
105 worksheet2.write("B4", 900)?;
106 worksheet2.write("B5", 1200)?;
107 worksheet2.write_formula_with_format("B6", "=SUBTOTAL(9,B2:B5)", &bold)?;
108 worksheet2.write("A7", "South")?;
109 worksheet2.write("A8", "South")?;
110 worksheet2.write("A9", "South")?;
111 worksheet2.write("A10", "South")?;
112 worksheet2.write_with_format("A11", "South Total", &bold)?;
113 worksheet2.write("B7", 400)?;
114 worksheet2.write("B8", 600)?;
115 worksheet2.write("B9", 500)?;
116 worksheet2.write("B10", 600)?;
117 worksheet2.write_formula_with_format("B11", "=SUBTOTAL(9,B7:B10)", &bold)?;
118 worksheet2.write_with_format("A12", "Grand Total", &bold)?;
119 worksheet2.write_formula_with_format("B12", "=SUBTOTAL(9,B2:B10)", &bold)?;
120
121 //
122 // Example 3: Create a worksheet with outlined columns.
123 //
124 let worksheet3 = workbook.add_worksheet_by_name("Outline Columns")?;
125 // Add bold format to the first row.
126 worksheet3.set_row_height_with_format(1, 15.0, &bold)?;
127 worksheet3.write_row("A1", &["Month", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Total"])?;
128 worksheet3.set_columns_width_with_format("A:A", 10.0, &bold)?;
129 worksheet3.set_columns_width("B:G", 10.0)?;
130 worksheet3.set_columns_level("B:G", 1)?;
131 worksheet3.set_columns_width("H:H", 10.0)?;
132 worksheet3.write_column("A2", &["North", "South", "East", "East"])?;
133 worksheet3.write_row("B2", &[50, 20, 15, 25, 65, 80])?;
134 worksheet3.write_row("B3", &[10, 20, 30, 50, 50, 50])?;
135 worksheet3.write_row("B4", &[45, 75, 50, 15, 75, 100])?;
136 worksheet3.write_row("B5", &[15, 15, 55, 35, 20, 50])?;
137 worksheet3.write_formula("H2", "=SUM(B2:G2)")?;
138 worksheet3.write_formula("H3", "=SUM(B3:G3)")?;
139 worksheet3.write_formula("H4", "=SUM(B4:G4)")?;
140 worksheet3.write_formula("H5", "=SUM(B5:G5)")?;
141 worksheet3.write_formula_with_format("H6", "=SUM(H2:H5)", &bold)?;
142
143 //
144 // Example 4: Show all possible outline levels.
145 //
146 let levels = [
147 "Level 1",
148 "Level 2",
149 "Level 3",
150 "Level 4",
151 "Level 5",
152 "Level 6",
153 "Level 7",
154 "Level 6",
155 "Level 5",
156 "Level 4",
157 "Level 3",
158 "Level 2",
159 "Level 1",
160 ];
161 let worksheet4 = workbook.add_worksheet_by_name("Outline levels")?;
162 worksheet4.write_column("A1", &levels)?;
163
164 worksheet4.set_row_level(1, 1)?;
165 worksheet4.set_row_level(2, 2)?;
166 worksheet4.set_row_level(3, 3)?;
167 worksheet4.set_row_level(4, 4)?;
168 worksheet4.set_row_level(5, 5)?;
169 worksheet4.set_row_level(6, 6)?;
170 worksheet4.set_row_level(7, 7)?;
171 worksheet4.set_row_level(8, 6)?;
172 worksheet4.set_row_level(9, 5)?;
173 worksheet4.set_row_level(10, 4)?;
174 worksheet4.set_row_level(11, 3)?;
175 worksheet4.set_row_level(12, 2)?;
176 worksheet4.set_row_level(13, 1)?;
177
178 workbook.save_as("examples/outline.xlsx")?;
179 Ok(())
180}
4fn main() -> WorkbookResult<()> {
5 // Prepare autofilter data
6 let text = fs::read_to_string("examples/autofilter_data.txt").unwrap();
7 let mut text = text.split("\n");
8 let headers: Vec<&str> = text.next().unwrap().split_whitespace().collect();
9 let mut data: Vec<Vec<&str>> = vec![];
10 for text in text { data.push(text.split_whitespace().collect()) }
11
12 // Create a new workbook
13 let mut workbook = Workbook::new();
14 // Add some worksheets
15 workbook.add_worksheet()?;
16 workbook.add_worksheet()?;
17 workbook.add_worksheet()?;
18 workbook.add_worksheet()?;
19 workbook.add_worksheet()?;
20 workbook.add_worksheet()?;
21
22 // Set up several sheets with the same data.
23 for worksheet in workbook.worksheets_mut() {
24 // Make the columns wider.
25 worksheet.set_columns_width("A:D", 12.0)?;
26 // // Make the header row larger.
27 worksheet.set_row_height_with_format(1, 20.0, &Format::default().set_bold())?;
28 // Make the headers bold.
29 worksheet.write_row("A1", &headers)?;
30 }
31
32 //
33 // Example 1. Autofilter without conditions.
34 //
35
36 let worksheet1 = workbook.get_worksheet_mut(1)?;
37 // Set the autofilter.
38 worksheet1.autofilter("A1:D51");
39 let mut row = 2;
40 for row_data in &data {
41 let mut col = 1;
42 for data in row_data {
43 if let Ok(num) = data.parse::<i32>() {
44 worksheet1.write((row, col), num)?;
45 } else {
46 worksheet1.write((row, col), *data)?;
47 }
48 col += 1;
49 }
50 // Move on to the next worksheet row.
51 row += 1;
52 }
53
54
55 //
56 // Example 2. Autofilter with a filter condition in the first column.
57 //
58 let worksheet2 = workbook.get_worksheet_mut(2)?;
59 // Set the autofilter.
60 worksheet2.autofilter("A1:D51");
61 // Add filter criteria.
62 let mut filters = Filters::new();
63 filters.and(Filter::eq("East"));
64 worksheet2.filter_column("A", &filters);
65 // Hide the rows that don't match the filter criteria.
66 let mut row = 2;
67 for row_data in &data {
68 let mut col = 1;
69 let data = row_data.get(0);
70 // Check for rows that match the filter.
71 if data != Some(&"East") {
72 // We need to hide rows that don't match the filter.
73 worksheet2.hide_row(row)?;
74 }
75 for data in row_data {
76 if let Ok(num) = data.parse::<i32>() {
77 worksheet2.write((row, col), num)?;
78 } else {
79 worksheet2.write((row, col), *data)?;
80 }
81 col += 1;
82 }
83 // Move on to the next worksheet row.
84 row += 1;
85 }
86
87 //
88 // Example 3. Autofilter with a filter condition in the first column.
89 //
90 let worksheet3 = workbook.get_worksheet_mut(3)?;
91 // Set the autofilter.
92 worksheet3.autofilter("A1:D51");
93 // Add filter criteria.
94 let mut filters = Filters::new();
95 filters.and(Filter::eq("East")).or(Filter::eq("South"));
96 worksheet3.filter_column("A", &filters);
97 // Hide the rows that don't match the filter criteria.
98 let mut row = 2;
99 for row_data in &data {
100 let mut col = 1;
101 let data = row_data.get(0);
102 // Check for rows that match the filter.
103 if data != Some(&"East") && data != Some(&"South") {
104 // We need to hide rows that don't match the filter.
105 worksheet3.hide_row(row)?;
106 }
107 for data in row_data {
108 if let Ok(num) = data.parse::<i32>() {
109 worksheet3.write((row, col), num)?;
110 } else {
111 worksheet3.write((row, col), *data)?;
112 }
113 col += 1;
114 }
115 // Move on to the next worksheet row.
116 row += 1;
117 }
118
119
120 //
121 // Example 4. Autofilter with filter conditions in two columns.
122 //
123 let worksheet4 = workbook.get_worksheet_mut(4)?;
124 // Set the autofilter.
125 worksheet4.autofilter("A1:D51");
126 // Add filter criteria.
127 let mut filters_a = Filters::new();
128 filters_a.and(Filter::eq("East"));
129 worksheet4.filter_column("A", &filters_a);
130 let mut filters_c = Filters::new();
131 filters_c.and(Filter::gt("3000")).and(Filter::lt("8000"));
132 worksheet4.filter_column("C", &filters_c);
133 // Hide the rows that don't match the filter criteria.
134 let mut row = 2;
135 for row_data in &data {
136 let mut col = 1;
137 let data = row_data.get(0);
138 // Check for rows that match the filter.
139 if data != Some(&"East") {
140 // We need to hide rows that don't match the filter.
141 worksheet4.hide_row(row)?;
142 }
143 for data in row_data {
144 if let Ok(num) = data.parse::<i32>() {
145 if num <= 3000 || num >= 8000 {
146 worksheet4.hide_row(row)?;
147 }
148 worksheet4.write((row, col), num)?;
149 } else {
150 worksheet4.write((row, col), *data)?;
151 }
152 col += 1;
153 }
154 // Move on to the next worksheet row.
155 row += 1;
156 }
157
158
159 //
160 // Example 5. Autofilter with a filter list condition in one of the columns.
161 //
162 let worksheet5 = workbook.get_worksheet_mut(5)?;
163 // Set the autofilter.
164 worksheet5.autofilter("A1:D51");
165 // Add filter criteria.
166 let filters_list = Filters::eq(vec!["East", "North", "South"]);
167 worksheet5.filter_column("A", &filters_list);
168 // Hide the rows that don't match the filter criteria.
169 let mut row = 2;
170 for row_data in &data {
171 let mut col = 1;
172 let data = row_data.get(0);
173 // Check for rows that match the filter.
174 if data != Some(&"East") && data != Some(&"North") && data != Some(&"South") {
175 // We need to hide rows that don't match the filter.
176 worksheet5.hide_row(row)?;
177 }
178 for data in row_data {
179 if let Ok(num) = data.parse::<i32>() {
180 worksheet5.write((row, col), num)?;
181 } else {
182 worksheet5.write((row, col), *data)?;
183 }
184 col += 1;
185 }
186 // Move on to the next worksheet row.
187 row += 1;
188 }
189
190 //
191 // Example 6. Autofilter with filter for blanks.
192 //
193 let worksheet6 = workbook.get_worksheet_mut(6)?;
194 // Set the autofilter.
195 worksheet6.autofilter("A1:D51");
196 // Add filter criteria.
197 let filters = Filters::blank();
198 worksheet6.filter_column("A", &filters);
199 // Hide the rows that don't match the filter criteria.
200 let mut row = 2;
201 // Simulate a blank cell in the data.
202 data[5][0] = "";
203
204 for row_data in &data {
205 let mut col = 1;
206 let data = row_data.get(0);
207 // Check for rows that match the filter.
208 if data != Some(&"") {
209 // We need to hide rows that don't match the filter.
210 worksheet6.hide_row(row)?;
211 }
212 for data in row_data {
213 if let Ok(num) = data.parse::<i32>() {
214 worksheet6.write((row, col), num)?;
215 } else {
216 worksheet6.write((row, col), *data)?;
217 }
218 col += 1;
219 }
220 // Move on to the next worksheet row.
221 row += 1;
222 }
223
224 //
225 // Example 7. Autofilter with filter for non-blanks.
226 //
227 let worksheet7 = workbook.get_worksheet_mut(7)?;
228 // Set the autofilter.
229 worksheet7.autofilter("A1:D51");
230 // Add filter criteria.
231 let filters = Filters::not_blank();
232 worksheet7.filter_column("A", &filters);
233 // Hide the rows that don't match the filter criteria.
234 let mut row = 2;
235 // Simulate a blank cell in the data.
236
237 for row_data in &data {
238 let mut col = 1;
239 let data = row_data.get(0);
240 // Check for rows that match the filter.
241 if data == Some(&"") {
242 // We need to hide rows that don't match the filter.
243 worksheet7.hide_row(row)?;
244 }
245 for data in row_data {
246 if let Ok(num) = data.parse::<i32>() {
247 worksheet7.write((row, col), num)?;
248 } else {
249 worksheet7.write((row, col), *data)?;
250 }
251 col += 1;
252 }
253 // Move on to the next worksheet row.
254 row += 1;
255 }
256
257
258 workbook.save_as("examples/autofilter.xlsx")?;
259
260 Ok(())
261}
Sourcepub fn set_italic(self) -> Self
pub fn set_italic(self) -> Self
Set the font to italic, learn more about it in FormatFont
.
§Returns
Returns the modified format with font italic set to true.
Sourcepub fn set_underline(self) -> Self
pub fn set_underline(self) -> Self
Add an underline for the font, learn more about it in FormatFont
.
§Returns
Returns the modified format with font underline set to true.
Sourcepub fn set_size(self, size: u8) -> Self
pub fn set_size(self, size: u8) -> Self
Set the size of the font, learn more about it in FormatFont
.
§Arguments
arg | type | meaning |
---|---|---|
size | u8 | The new size of the font. |
§Returns
Returns the modified Format with the size set to the provided value.
Examples found in repository?
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7 // write some text
8 WorkSheet::write(worksheet, "A1", "Hello")?;
9 worksheet.write("B1", "World")?;
10 worksheet.write("C1", "Rust")?;
11 // Adjust font size
12 let big = Format::default().set_size(32);
13 worksheet.write_with_format("B1", "big text", &big)?;
14 // Change font color
15 let red = Format::default().set_color(FormatColor::RGB(255, 119, 119));
16 worksheet.write_with_format("C1", "red text", &red)?;
17 // Change the font style
18 let bold = red.set_bold();
19 worksheet.write_with_format("D1", "red bold text", &bold)?;
20 // Change font
21 let font = Format::default().set_font("华文行楷");
22 worksheet.write_with_format("E1", "你好", &font)?;
23 // adjust the text align
24 let left_top = Format::default().set_align(FormatAlignType::Left).set_align(FormatAlignType::Top);
25 worksheet.write_with_format("A2", "left top", &left_top)?;
26 // add borders
27 let thin_border = Format::default().set_border(FormatBorderType::Thin);
28 worksheet.write_with_format("B2", "bordered text", &thin_border)?;
29 // add background
30 let red_background = Format::default().set_background_color(FormatColor::RGB(255, 119, 119));
31 worksheet.write_with_format("C2", "red", &red_background)?;
32 // add a number
33 worksheet.write("D2", std::f64::consts::PI)?;
34 // add a new worksheet and set a tab color
35 let worksheet = workbook.add_worksheet_by_name("Other examples")?;
36 worksheet.set_tab_color(&FormatColor::RGB(255, 153, 0)); // Orange
37 // Set a background.
38 worksheet.set_background("examples/pics/ferris.png")?;
39 // Create a format to use in the merged range.
40 let merge_format = Format::default()
41 .set_bold()
42 .set_border(FormatBorderType::Double)
43 .set_align(FormatAlignType::Center)
44 .set_align(FormatAlignType::VerticalCenter)
45 .set_background_color(FormatColor::RGB(255, 255, 0));
46 // Merge cells.
47 worksheet.merge_range_with_format("A1:C3", "Merged Range", &merge_format)?;
48 // Add an image
49 worksheet.insert_image("A4:C10", &"./examples/pics/rust.png")?;
50 workbook.save_as("examples/hello_world.xlsx")?;
51 Ok(())
52}
Sourcepub fn set_size_f64(self, size: f64) -> Self
pub fn set_size_f64(self, size: f64) -> Self
Set the size of the font, learn more about it in FormatFont
.
§Arguments
arg | type | meaning |
---|---|---|
size | f64 | The new size of the font. |
§Returns
Returns the modified Format with the size set to the provided value.
Sourcepub fn set_color(self, format_color: FormatColor) -> Self
pub fn set_color(self, format_color: FormatColor) -> Self
Set the color of the font, learn more about it in FormatFont
.
§Arguments
arg | type | meaning |
---|---|---|
format_color | FormatColor | The new color of the font. |
§Returns
Returns the modified Format with the color set to the provided value.
Examples found in repository?
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7 // write some text
8 WorkSheet::write(worksheet, "A1", "Hello")?;
9 worksheet.write("B1", "World")?;
10 worksheet.write("C1", "Rust")?;
11 // Adjust font size
12 let big = Format::default().set_size(32);
13 worksheet.write_with_format("B1", "big text", &big)?;
14 // Change font color
15 let red = Format::default().set_color(FormatColor::RGB(255, 119, 119));
16 worksheet.write_with_format("C1", "red text", &red)?;
17 // Change the font style
18 let bold = red.set_bold();
19 worksheet.write_with_format("D1", "red bold text", &bold)?;
20 // Change font
21 let font = Format::default().set_font("华文行楷");
22 worksheet.write_with_format("E1", "你好", &font)?;
23 // adjust the text align
24 let left_top = Format::default().set_align(FormatAlignType::Left).set_align(FormatAlignType::Top);
25 worksheet.write_with_format("A2", "left top", &left_top)?;
26 // add borders
27 let thin_border = Format::default().set_border(FormatBorderType::Thin);
28 worksheet.write_with_format("B2", "bordered text", &thin_border)?;
29 // add background
30 let red_background = Format::default().set_background_color(FormatColor::RGB(255, 119, 119));
31 worksheet.write_with_format("C2", "red", &red_background)?;
32 // add a number
33 worksheet.write("D2", std::f64::consts::PI)?;
34 // add a new worksheet and set a tab color
35 let worksheet = workbook.add_worksheet_by_name("Other examples")?;
36 worksheet.set_tab_color(&FormatColor::RGB(255, 153, 0)); // Orange
37 // Set a background.
38 worksheet.set_background("examples/pics/ferris.png")?;
39 // Create a format to use in the merged range.
40 let merge_format = Format::default()
41 .set_bold()
42 .set_border(FormatBorderType::Double)
43 .set_align(FormatAlignType::Center)
44 .set_align(FormatAlignType::VerticalCenter)
45 .set_background_color(FormatColor::RGB(255, 255, 0));
46 // Merge cells.
47 worksheet.merge_range_with_format("A1:C3", "Merged Range", &merge_format)?;
48 // Add an image
49 worksheet.insert_image("A4:C10", &"./examples/pics/rust.png")?;
50 workbook.save_as("examples/hello_world.xlsx")?;
51 Ok(())
52}
More examples
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook called simple.xls and add some worksheets.
5 let mut workbook = Workbook::new();
6 let header1 = Format::default()
7 .set_color(FormatColor::RGB(255, 255, 255))
8 .set_background_color(FormatColor::RGB(74, 196, 12));
9 let header2 = Format::default()
10 .set_color(FormatColor::RGB(255, 255, 255))
11 .set_background_color(FormatColor::RGB(40, 253, 3));
12
13 //
14 // Example of using the FILTER() function.
15 //
16 let worksheet1 = workbook.add_worksheet_by_name("Filter")?;
17 worksheet1.write_formula("F2", "_xlfn.FILTER(A1:D17,C1:C17=K2)")?;
18
19 // Write the data the function will work on.
20 worksheet1.write_with_format("K1", "Product", &header2)?;
21 worksheet1.write("K2", "Apple")?;
22 worksheet1.write_with_format("F1", "Region", &header2)?;
23 worksheet1.write_with_format("G1", "Sales Rep", &header2)?;
24 worksheet1.write_with_format("H1", "Product", &header2)?;
25 worksheet1.write_with_format("I1", "Units", &header2)?;
26
27 write_worksheet_data(worksheet1, &header1)?;
28 worksheet1.set_columns_width_pixels("E:E", 10.0)?;
29 worksheet1.set_columns_width_pixels("J:J", 10.0)?;
30
31 //
32 // Example of using the UNIQUE() function.
33 //
34 let worksheet2 = workbook.add_worksheet_by_name("Unique")?;
35 worksheet2.write_formula("F2", "_xlfn.UNIQUE(B2:B17)")?;
36
37 // A more complex example combining SORT and UNIQUE.
38 worksheet2.write_formula("H2", "_xlfn.SORT(_xlfn.UNIQUE(B2:B17))")?;
39
40 // Write the data the function will work on.
41 worksheet2.write_with_format("F1", "Sales Rep", &header2)?;
42 worksheet2.write_with_format("H1", "Sales Rep", &header2)?;
43
44 write_worksheet_data(worksheet2, &header1)?;
45 worksheet2.set_columns_width_pixels("E:E", 10.0)?;
46 worksheet2.set_columns_width_pixels("G:G", 10.0)?;
47
48 //
49 // Example of using the SORT() function.
50 //
51 let worksheet3 = workbook.add_worksheet_by_name("Sort")?;
52 worksheet3.write_formula("F2", "_xlfn.SORT(B2:B17)")?;
53
54 // A more complex example combining SORT and FILTER.
55 worksheet3.write_formula("H2", "_xlfn.SORT(_xlfn.FILTER(C2: D17, D2: D17 > 5000, \"\"), 2, 1)")?;
56
57 // Write the data the function will work on.
58 worksheet3.write_with_format("F1", "Sales Rep", &header2)?;
59 worksheet3.write_with_format("H1", "Product", &header2)?;
60 worksheet3.write_with_format("I1", "Units", &header2)?;
61
62 write_worksheet_data(worksheet3, &header1)?;
63 worksheet3.set_columns_width_pixels("E:E", 10.0)?;
64 worksheet3.set_columns_width_pixels("G:G", 10.0)?;
65
66
67 //
68 // Example of using the SORTBY() function.
69 //
70 let worksheet4 = workbook.add_worksheet_by_name("Sortby")?;
71 worksheet4.write_formula("D2", "_xlfn.SORTBY(A2:B9,B2:B9)")?;
72
73 // Write the data the function will work on.
74 worksheet4.write_with_format("A1", "Name", &header1)?;
75 worksheet4.write_with_format("B1", "Age", &header1)?;
76
77 worksheet4.write("A2", "Tom")?;
78 worksheet4.write("A3", "Fred")?;
79 worksheet4.write("A4", "Amy")?;
80 worksheet4.write("A5", "Sal")?;
81 worksheet4.write("A6", "Fritz")?;
82 worksheet4.write("A7", "Srivan")?;
83 worksheet4.write("A8", "Xi")?;
84 worksheet4.write("A9", "Hector")?;
85
86 worksheet4.write("B2", 52)?;
87 worksheet4.write("B3", 65)?;
88 worksheet4.write("B4", 22)?;
89 worksheet4.write("B5", 73)?;
90 worksheet4.write("B6", 19)?;
91 worksheet4.write("B7", 39)?;
92 worksheet4.write("B8", 19)?;
93 worksheet4.write("B9", 66)?;
94
95 worksheet4.write_with_format("D1", "Name", &header2)?;
96 worksheet4.write_with_format("E1", "Age", &header2)?;
97
98 worksheet4.set_columns_width_pixels("C:C", 10.0)?;
99
100 //
101 // Example of using the XLOOKUP() function.
102 //
103 let worksheet5 = workbook.add_worksheet_by_name("Xlookup")?;
104 worksheet5.write_formula("F1", "_xlfn.XLOOKUP(E1,A2:A9,C2:C9)")?;
105
106 // Write the data the function will work on.
107 worksheet5.write_with_format("A1", "Country", &header1)?;
108 worksheet5.write_with_format("B1", "Abr", &header1)?;
109 worksheet5.write_with_format("C1", "Prefix", &header1)?;
110
111 worksheet5.write("A2", "China")?;
112 worksheet5.write("A3", "India")?;
113 worksheet5.write("A4", "United States")?;
114 worksheet5.write("A5", "Indonesia")?;
115 worksheet5.write("A6", "Brazil")?;
116 worksheet5.write("A7", "Pakistan")?;
117 worksheet5.write("A8", "Nigeria")?;
118 worksheet5.write("A9", "Bangladesh")?;
119
120 worksheet5.write("B2", "CN")?;
121 worksheet5.write("B3", "IN")?;
122 worksheet5.write("B4", "US")?;
123 worksheet5.write("B5", "ID")?;
124 worksheet5.write("B6", "BR")?;
125 worksheet5.write("B7", "PK")?;
126 worksheet5.write("B8", "NG")?;
127 worksheet5.write("B9", "BD")?;
128
129 worksheet5.write("C2", 86)?;
130 worksheet5.write("C3", 91)?;
131 worksheet5.write("C4", 1)?;
132 worksheet5.write("C5", 62)?;
133 worksheet5.write("C6", 55)?;
134 worksheet5.write("C7", 92)?;
135 worksheet5.write("C8", 234)?;
136 worksheet5.write("C9", 880)?;
137
138 worksheet5.write_with_format("E1", "Brazil", &header2)?;
139
140 worksheet5.set_columns_width_pixels("A:A", 100.0)?;
141 worksheet5.set_columns_width_pixels("D:D", 10.0)?;
142
143 //
144 // Example of using the XMATCH() function.
145 //
146 let worksheet6 = workbook.add_worksheet_by_name("Xmatch")?;
147 worksheet6.write_formula("D2", "_xlfn.XMATCH(C2,A2:A6)")?;
148
149 // Write the data the function will work on.
150 worksheet6.write_with_format("A1", "Product", &header1)?;
151
152 worksheet6.write("A2", "Apple")?;
153 worksheet6.write("A3", "Grape")?;
154 worksheet6.write("A4", "Pear")?;
155 worksheet6.write("A5", "Banana")?;
156 worksheet6.write("A6", "Cherry")?;
157
158 worksheet6.write_with_format("C1", "Product", &header2)?;
159 worksheet6.write_with_format("D1", "Position", &header2)?;
160 worksheet6.write("C2", "Grape")?;
161
162 worksheet6.set_columns_width_pixels("B:B", 10.0)?;
163
164 //
165 // Example of using the RANDARRAY() function.
166 //
167 let worksheet7 = workbook.add_worksheet_by_name("Randarray")?;
168 worksheet7.write_dynamic_array_formula("A1", "_xlfn.RANDARRAY(5,3,1,100, TRUE)")?;
169
170 //
171 // Example of using the SEQUENCE() function.
172 //
173 let worksheet8 = workbook.add_worksheet_by_name("Sequence")?;
174 worksheet8.write_dynamic_array_formula("A1", "_xlfn.SEQUENCE(4,5)")?;
175
176 //
177 // Example of using the Spill range operator.
178 //
179 let worksheet9 = workbook.add_worksheet_by_name("Spill ranges")?;
180 worksheet9.write_dynamic_array_formula("H2", "_xlfn.ANCHORARRAY(F2)")?;
181
182 worksheet9.write_dynamic_array_formula("J2", "_xlfn.COUNTA(_xlfn.ANCHORARRAY(F2))")?;
183
184 // Write the data the to work on.
185 worksheet9.write_dynamic_array_formula("F2", "_xlfn.UNIQUE(B2:B17)")?;
186 worksheet9.write_with_format("F1", "Unique", &header2)?;
187 worksheet9.write_with_format("H1", "Spill", &header2)?;
188 worksheet9.write_with_format("J1", "Spill", &header2)?;
189
190 write_worksheet_data(worksheet9, &header1)?;
191 worksheet9.set_columns_width_pixels("E:E", 10.0)?;
192 worksheet9.set_columns_width_pixels("G:G", 10.0)?;
193 worksheet9.set_columns_width_pixels("I:I", 10.0)?;
194
195 //
196 // Example of using dynamic ranges with older Excel functions.
197 //
198 let worksheet10 = workbook.add_worksheet_by_name("Older functions")?;
199 worksheet10.write_dynamic_array_formula("B1", "=LEN(A1:A3)")?;
200
201 // Write the data the to work on.
202 worksheet10.write("A1", "Foo")?;
203 worksheet10.write("A2", "Food")?;
204 worksheet10.write("A3", "Frood")?;
205
206
207 workbook.save_as("examples/dynamic_arrays.xlsx")?;
208 Ok(())
209}
Sourcepub fn set_font(self, font_name: &str) -> Self
pub fn set_font(self, font_name: &str) -> Self
Set the font name, learn more about it in FormatFont
.
§Arguments
arg | type | meaning |
---|---|---|
size | &str | The new font name. |
§Returns
Returns the modified Format with the name set to the provided value.
Examples found in repository?
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7 // write some text
8 WorkSheet::write(worksheet, "A1", "Hello")?;
9 worksheet.write("B1", "World")?;
10 worksheet.write("C1", "Rust")?;
11 // Adjust font size
12 let big = Format::default().set_size(32);
13 worksheet.write_with_format("B1", "big text", &big)?;
14 // Change font color
15 let red = Format::default().set_color(FormatColor::RGB(255, 119, 119));
16 worksheet.write_with_format("C1", "red text", &red)?;
17 // Change the font style
18 let bold = red.set_bold();
19 worksheet.write_with_format("D1", "red bold text", &bold)?;
20 // Change font
21 let font = Format::default().set_font("华文行楷");
22 worksheet.write_with_format("E1", "你好", &font)?;
23 // adjust the text align
24 let left_top = Format::default().set_align(FormatAlignType::Left).set_align(FormatAlignType::Top);
25 worksheet.write_with_format("A2", "left top", &left_top)?;
26 // add borders
27 let thin_border = Format::default().set_border(FormatBorderType::Thin);
28 worksheet.write_with_format("B2", "bordered text", &thin_border)?;
29 // add background
30 let red_background = Format::default().set_background_color(FormatColor::RGB(255, 119, 119));
31 worksheet.write_with_format("C2", "red", &red_background)?;
32 // add a number
33 worksheet.write("D2", std::f64::consts::PI)?;
34 // add a new worksheet and set a tab color
35 let worksheet = workbook.add_worksheet_by_name("Other examples")?;
36 worksheet.set_tab_color(&FormatColor::RGB(255, 153, 0)); // Orange
37 // Set a background.
38 worksheet.set_background("examples/pics/ferris.png")?;
39 // Create a format to use in the merged range.
40 let merge_format = Format::default()
41 .set_bold()
42 .set_border(FormatBorderType::Double)
43 .set_align(FormatAlignType::Center)
44 .set_align(FormatAlignType::VerticalCenter)
45 .set_background_color(FormatColor::RGB(255, 255, 0));
46 // Merge cells.
47 worksheet.merge_range_with_format("A1:C3", "Merged Range", &merge_format)?;
48 // Add an image
49 worksheet.insert_image("A4:C10", &"./examples/pics/rust.png")?;
50 workbook.save_as("examples/hello_world.xlsx")?;
51 Ok(())
52}
Sourcepub fn set_border(self, format_border_type: FormatBorderType) -> Self
pub fn set_border(self, format_border_type: FormatBorderType) -> Self
Set all borders of the format, learn more about it in FormatBorder
.
§Arguments
arg | type | meaning |
---|---|---|
format_border_type | FormatBorderType | The type of border to apply to borders of the format. |
§Returns
Returns the modified Format with borders of the format set to the provided type.
Examples found in repository?
3fn main() -> WorkbookResult<()> {
4 let mut workbook = Workbook::new();
5 let worksheet = workbook.get_worksheet_mut(1)?;
6
7 // Increase the cell size of the merged cells to highlight the formatting.
8 worksheet.set_columns_width("B:D", 18.0)?;
9 worksheet.set_row_height(4, 40.0)?;
10 worksheet.set_row_height(7, 30.0)?;
11 worksheet.set_row_height(8, 30.0)?;
12
13 // Create a format to use in the merged range.
14 let merge_format = Format::default()
15 .set_bold()
16 .set_border(FormatBorderType::Double)
17 .set_align(FormatAlignType::Center)
18 .set_align(FormatAlignType::VerticalCenter)
19 .set_background_color(FormatColor::RGB(255, 255, 0));
20
21 // Merge 3 cells.
22 worksheet.merge_range_with_format("B4:D4", "Merged Range", &merge_format)?;
23 // Merge 3 cells over two rows.
24 worksheet.merge_range_with_format("B7:D8", "Merged Range", &merge_format)?;
25
26 workbook.save_as("examples/merge.xlsx")?;
27 Ok(())
28}
More examples
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7 // write some text
8 WorkSheet::write(worksheet, "A1", "Hello")?;
9 worksheet.write("B1", "World")?;
10 worksheet.write("C1", "Rust")?;
11 // Adjust font size
12 let big = Format::default().set_size(32);
13 worksheet.write_with_format("B1", "big text", &big)?;
14 // Change font color
15 let red = Format::default().set_color(FormatColor::RGB(255, 119, 119));
16 worksheet.write_with_format("C1", "red text", &red)?;
17 // Change the font style
18 let bold = red.set_bold();
19 worksheet.write_with_format("D1", "red bold text", &bold)?;
20 // Change font
21 let font = Format::default().set_font("华文行楷");
22 worksheet.write_with_format("E1", "你好", &font)?;
23 // adjust the text align
24 let left_top = Format::default().set_align(FormatAlignType::Left).set_align(FormatAlignType::Top);
25 worksheet.write_with_format("A2", "left top", &left_top)?;
26 // add borders
27 let thin_border = Format::default().set_border(FormatBorderType::Thin);
28 worksheet.write_with_format("B2", "bordered text", &thin_border)?;
29 // add background
30 let red_background = Format::default().set_background_color(FormatColor::RGB(255, 119, 119));
31 worksheet.write_with_format("C2", "red", &red_background)?;
32 // add a number
33 worksheet.write("D2", std::f64::consts::PI)?;
34 // add a new worksheet and set a tab color
35 let worksheet = workbook.add_worksheet_by_name("Other examples")?;
36 worksheet.set_tab_color(&FormatColor::RGB(255, 153, 0)); // Orange
37 // Set a background.
38 worksheet.set_background("examples/pics/ferris.png")?;
39 // Create a format to use in the merged range.
40 let merge_format = Format::default()
41 .set_bold()
42 .set_border(FormatBorderType::Double)
43 .set_align(FormatAlignType::Center)
44 .set_align(FormatAlignType::VerticalCenter)
45 .set_background_color(FormatColor::RGB(255, 255, 0));
46 // Merge cells.
47 worksheet.merge_range_with_format("A1:C3", "Merged Range", &merge_format)?;
48 // Add an image
49 worksheet.insert_image("A4:C10", &"./examples/pics/rust.png")?;
50 workbook.save_as("examples/hello_world.xlsx")?;
51 Ok(())
52}
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, ¢er_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, ¢er_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, ¢er_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", ¢er_format)?;
93 }
94 for row in 1..=50 {
95 worksheet4.write_with_format((row, 1), "Scroll", ¢er_format)?;
96 for col in 1..=26 {
97 worksheet4.write_with_format((row, col), col, ¢er_format)?;
98 }
99 }
100
101 workbook.save_as("examples/panes.xlsx")?;
102 Ok(())
103}
Sourcepub fn set_border_left(self, format_border_type: FormatBorderType) -> Self
pub fn set_border_left(self, format_border_type: FormatBorderType) -> Self
Set the left border of the format, learn more about it in FormatBorder
.
§Arguments
arg | type | meaning |
---|---|---|
format_border_type | FormatBorderType | The type of border to apply to the left side of the format. |
§Returns
Returns the modified Format with the left border of the format set to the provided type.
Sourcepub fn set_border_right(self, format_border_type: FormatBorderType) -> Self
pub fn set_border_right(self, format_border_type: FormatBorderType) -> Self
Set the right border of the format, learn more about it in FormatBorder
.
§Arguments
arg | type | meaning |
---|---|---|
format_border_type | FormatBorderType | The type of border to apply to the right side of the format. |
§Returns
Returns the modified Format with the right border of the format set to the provided type.
Sourcepub fn set_border_top(self, format_border_type: FormatBorderType) -> Self
pub fn set_border_top(self, format_border_type: FormatBorderType) -> Self
Set the top border of the format, learn more about it in FormatBorder
.
§Arguments
arg | type | meaning |
---|---|---|
format_border_type | FormatBorderType | The type of border to apply to the top side of the format. |
§Returns
Returns the modified Format with the top border of the format set to the provided type.
Sourcepub fn set_border_bottom(self, format_border_type: FormatBorderType) -> Self
pub fn set_border_bottom(self, format_border_type: FormatBorderType) -> Self
Set the bottom border of the format, learn more about it in FormatBorder
.
§Arguments
arg | type | meaning |
---|---|---|
format_border_type | FormatBorderType | The type of border to apply to the bottom side of the format. |
§Returns
Returns the modified Format with the bottom border of the format set to the provided type.
Sourcepub fn set_background_color(self, format_color: FormatColor) -> Self
pub fn set_background_color(self, format_color: FormatColor) -> Self
Set the background color of the format.
§Arguments
arg | type | meaning |
---|---|---|
format_color | FormatColor | The color to set as the background of the format. |
§Returns
Returns the modified Format with the background color of the format set to the provided color.
Examples found in repository?
3fn main() -> WorkbookResult<()> {
4 let mut workbook = Workbook::new();
5 let worksheet = workbook.get_worksheet_mut(1)?;
6
7 // Increase the cell size of the merged cells to highlight the formatting.
8 worksheet.set_columns_width("B:D", 18.0)?;
9 worksheet.set_row_height(4, 40.0)?;
10 worksheet.set_row_height(7, 30.0)?;
11 worksheet.set_row_height(8, 30.0)?;
12
13 // Create a format to use in the merged range.
14 let merge_format = Format::default()
15 .set_bold()
16 .set_border(FormatBorderType::Double)
17 .set_align(FormatAlignType::Center)
18 .set_align(FormatAlignType::VerticalCenter)
19 .set_background_color(FormatColor::RGB(255, 255, 0));
20
21 // Merge 3 cells.
22 worksheet.merge_range_with_format("B4:D4", "Merged Range", &merge_format)?;
23 // Merge 3 cells over two rows.
24 worksheet.merge_range_with_format("B7:D8", "Merged Range", &merge_format)?;
25
26 workbook.save_as("examples/merge.xlsx")?;
27 Ok(())
28}
More examples
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7 // write some text
8 WorkSheet::write(worksheet, "A1", "Hello")?;
9 worksheet.write("B1", "World")?;
10 worksheet.write("C1", "Rust")?;
11 // Adjust font size
12 let big = Format::default().set_size(32);
13 worksheet.write_with_format("B1", "big text", &big)?;
14 // Change font color
15 let red = Format::default().set_color(FormatColor::RGB(255, 119, 119));
16 worksheet.write_with_format("C1", "red text", &red)?;
17 // Change the font style
18 let bold = red.set_bold();
19 worksheet.write_with_format("D1", "red bold text", &bold)?;
20 // Change font
21 let font = Format::default().set_font("华文行楷");
22 worksheet.write_with_format("E1", "你好", &font)?;
23 // adjust the text align
24 let left_top = Format::default().set_align(FormatAlignType::Left).set_align(FormatAlignType::Top);
25 worksheet.write_with_format("A2", "left top", &left_top)?;
26 // add borders
27 let thin_border = Format::default().set_border(FormatBorderType::Thin);
28 worksheet.write_with_format("B2", "bordered text", &thin_border)?;
29 // add background
30 let red_background = Format::default().set_background_color(FormatColor::RGB(255, 119, 119));
31 worksheet.write_with_format("C2", "red", &red_background)?;
32 // add a number
33 worksheet.write("D2", std::f64::consts::PI)?;
34 // add a new worksheet and set a tab color
35 let worksheet = workbook.add_worksheet_by_name("Other examples")?;
36 worksheet.set_tab_color(&FormatColor::RGB(255, 153, 0)); // Orange
37 // Set a background.
38 worksheet.set_background("examples/pics/ferris.png")?;
39 // Create a format to use in the merged range.
40 let merge_format = Format::default()
41 .set_bold()
42 .set_border(FormatBorderType::Double)
43 .set_align(FormatAlignType::Center)
44 .set_align(FormatAlignType::VerticalCenter)
45 .set_background_color(FormatColor::RGB(255, 255, 0));
46 // Merge cells.
47 worksheet.merge_range_with_format("A1:C3", "Merged Range", &merge_format)?;
48 // Add an image
49 worksheet.insert_image("A4:C10", &"./examples/pics/rust.png")?;
50 workbook.save_as("examples/hello_world.xlsx")?;
51 Ok(())
52}
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, ¢er_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, ¢er_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, ¢er_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", ¢er_format)?;
93 }
94 for row in 1..=50 {
95 worksheet4.write_with_format((row, 1), "Scroll", ¢er_format)?;
96 for col in 1..=26 {
97 worksheet4.write_with_format((row, col), col, ¢er_format)?;
98 }
99 }
100
101 workbook.save_as("examples/panes.xlsx")?;
102 Ok(())
103}
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook called simple.xls and add some worksheets.
5 let mut workbook = Workbook::new();
6 let header1 = Format::default()
7 .set_color(FormatColor::RGB(255, 255, 255))
8 .set_background_color(FormatColor::RGB(74, 196, 12));
9 let header2 = Format::default()
10 .set_color(FormatColor::RGB(255, 255, 255))
11 .set_background_color(FormatColor::RGB(40, 253, 3));
12
13 //
14 // Example of using the FILTER() function.
15 //
16 let worksheet1 = workbook.add_worksheet_by_name("Filter")?;
17 worksheet1.write_formula("F2", "_xlfn.FILTER(A1:D17,C1:C17=K2)")?;
18
19 // Write the data the function will work on.
20 worksheet1.write_with_format("K1", "Product", &header2)?;
21 worksheet1.write("K2", "Apple")?;
22 worksheet1.write_with_format("F1", "Region", &header2)?;
23 worksheet1.write_with_format("G1", "Sales Rep", &header2)?;
24 worksheet1.write_with_format("H1", "Product", &header2)?;
25 worksheet1.write_with_format("I1", "Units", &header2)?;
26
27 write_worksheet_data(worksheet1, &header1)?;
28 worksheet1.set_columns_width_pixels("E:E", 10.0)?;
29 worksheet1.set_columns_width_pixels("J:J", 10.0)?;
30
31 //
32 // Example of using the UNIQUE() function.
33 //
34 let worksheet2 = workbook.add_worksheet_by_name("Unique")?;
35 worksheet2.write_formula("F2", "_xlfn.UNIQUE(B2:B17)")?;
36
37 // A more complex example combining SORT and UNIQUE.
38 worksheet2.write_formula("H2", "_xlfn.SORT(_xlfn.UNIQUE(B2:B17))")?;
39
40 // Write the data the function will work on.
41 worksheet2.write_with_format("F1", "Sales Rep", &header2)?;
42 worksheet2.write_with_format("H1", "Sales Rep", &header2)?;
43
44 write_worksheet_data(worksheet2, &header1)?;
45 worksheet2.set_columns_width_pixels("E:E", 10.0)?;
46 worksheet2.set_columns_width_pixels("G:G", 10.0)?;
47
48 //
49 // Example of using the SORT() function.
50 //
51 let worksheet3 = workbook.add_worksheet_by_name("Sort")?;
52 worksheet3.write_formula("F2", "_xlfn.SORT(B2:B17)")?;
53
54 // A more complex example combining SORT and FILTER.
55 worksheet3.write_formula("H2", "_xlfn.SORT(_xlfn.FILTER(C2: D17, D2: D17 > 5000, \"\"), 2, 1)")?;
56
57 // Write the data the function will work on.
58 worksheet3.write_with_format("F1", "Sales Rep", &header2)?;
59 worksheet3.write_with_format("H1", "Product", &header2)?;
60 worksheet3.write_with_format("I1", "Units", &header2)?;
61
62 write_worksheet_data(worksheet3, &header1)?;
63 worksheet3.set_columns_width_pixels("E:E", 10.0)?;
64 worksheet3.set_columns_width_pixels("G:G", 10.0)?;
65
66
67 //
68 // Example of using the SORTBY() function.
69 //
70 let worksheet4 = workbook.add_worksheet_by_name("Sortby")?;
71 worksheet4.write_formula("D2", "_xlfn.SORTBY(A2:B9,B2:B9)")?;
72
73 // Write the data the function will work on.
74 worksheet4.write_with_format("A1", "Name", &header1)?;
75 worksheet4.write_with_format("B1", "Age", &header1)?;
76
77 worksheet4.write("A2", "Tom")?;
78 worksheet4.write("A3", "Fred")?;
79 worksheet4.write("A4", "Amy")?;
80 worksheet4.write("A5", "Sal")?;
81 worksheet4.write("A6", "Fritz")?;
82 worksheet4.write("A7", "Srivan")?;
83 worksheet4.write("A8", "Xi")?;
84 worksheet4.write("A9", "Hector")?;
85
86 worksheet4.write("B2", 52)?;
87 worksheet4.write("B3", 65)?;
88 worksheet4.write("B4", 22)?;
89 worksheet4.write("B5", 73)?;
90 worksheet4.write("B6", 19)?;
91 worksheet4.write("B7", 39)?;
92 worksheet4.write("B8", 19)?;
93 worksheet4.write("B9", 66)?;
94
95 worksheet4.write_with_format("D1", "Name", &header2)?;
96 worksheet4.write_with_format("E1", "Age", &header2)?;
97
98 worksheet4.set_columns_width_pixels("C:C", 10.0)?;
99
100 //
101 // Example of using the XLOOKUP() function.
102 //
103 let worksheet5 = workbook.add_worksheet_by_name("Xlookup")?;
104 worksheet5.write_formula("F1", "_xlfn.XLOOKUP(E1,A2:A9,C2:C9)")?;
105
106 // Write the data the function will work on.
107 worksheet5.write_with_format("A1", "Country", &header1)?;
108 worksheet5.write_with_format("B1", "Abr", &header1)?;
109 worksheet5.write_with_format("C1", "Prefix", &header1)?;
110
111 worksheet5.write("A2", "China")?;
112 worksheet5.write("A3", "India")?;
113 worksheet5.write("A4", "United States")?;
114 worksheet5.write("A5", "Indonesia")?;
115 worksheet5.write("A6", "Brazil")?;
116 worksheet5.write("A7", "Pakistan")?;
117 worksheet5.write("A8", "Nigeria")?;
118 worksheet5.write("A9", "Bangladesh")?;
119
120 worksheet5.write("B2", "CN")?;
121 worksheet5.write("B3", "IN")?;
122 worksheet5.write("B4", "US")?;
123 worksheet5.write("B5", "ID")?;
124 worksheet5.write("B6", "BR")?;
125 worksheet5.write("B7", "PK")?;
126 worksheet5.write("B8", "NG")?;
127 worksheet5.write("B9", "BD")?;
128
129 worksheet5.write("C2", 86)?;
130 worksheet5.write("C3", 91)?;
131 worksheet5.write("C4", 1)?;
132 worksheet5.write("C5", 62)?;
133 worksheet5.write("C6", 55)?;
134 worksheet5.write("C7", 92)?;
135 worksheet5.write("C8", 234)?;
136 worksheet5.write("C9", 880)?;
137
138 worksheet5.write_with_format("E1", "Brazil", &header2)?;
139
140 worksheet5.set_columns_width_pixels("A:A", 100.0)?;
141 worksheet5.set_columns_width_pixels("D:D", 10.0)?;
142
143 //
144 // Example of using the XMATCH() function.
145 //
146 let worksheet6 = workbook.add_worksheet_by_name("Xmatch")?;
147 worksheet6.write_formula("D2", "_xlfn.XMATCH(C2,A2:A6)")?;
148
149 // Write the data the function will work on.
150 worksheet6.write_with_format("A1", "Product", &header1)?;
151
152 worksheet6.write("A2", "Apple")?;
153 worksheet6.write("A3", "Grape")?;
154 worksheet6.write("A4", "Pear")?;
155 worksheet6.write("A5", "Banana")?;
156 worksheet6.write("A6", "Cherry")?;
157
158 worksheet6.write_with_format("C1", "Product", &header2)?;
159 worksheet6.write_with_format("D1", "Position", &header2)?;
160 worksheet6.write("C2", "Grape")?;
161
162 worksheet6.set_columns_width_pixels("B:B", 10.0)?;
163
164 //
165 // Example of using the RANDARRAY() function.
166 //
167 let worksheet7 = workbook.add_worksheet_by_name("Randarray")?;
168 worksheet7.write_dynamic_array_formula("A1", "_xlfn.RANDARRAY(5,3,1,100, TRUE)")?;
169
170 //
171 // Example of using the SEQUENCE() function.
172 //
173 let worksheet8 = workbook.add_worksheet_by_name("Sequence")?;
174 worksheet8.write_dynamic_array_formula("A1", "_xlfn.SEQUENCE(4,5)")?;
175
176 //
177 // Example of using the Spill range operator.
178 //
179 let worksheet9 = workbook.add_worksheet_by_name("Spill ranges")?;
180 worksheet9.write_dynamic_array_formula("H2", "_xlfn.ANCHORARRAY(F2)")?;
181
182 worksheet9.write_dynamic_array_formula("J2", "_xlfn.COUNTA(_xlfn.ANCHORARRAY(F2))")?;
183
184 // Write the data the to work on.
185 worksheet9.write_dynamic_array_formula("F2", "_xlfn.UNIQUE(B2:B17)")?;
186 worksheet9.write_with_format("F1", "Unique", &header2)?;
187 worksheet9.write_with_format("H1", "Spill", &header2)?;
188 worksheet9.write_with_format("J1", "Spill", &header2)?;
189
190 write_worksheet_data(worksheet9, &header1)?;
191 worksheet9.set_columns_width_pixels("E:E", 10.0)?;
192 worksheet9.set_columns_width_pixels("G:G", 10.0)?;
193 worksheet9.set_columns_width_pixels("I:I", 10.0)?;
194
195 //
196 // Example of using dynamic ranges with older Excel functions.
197 //
198 let worksheet10 = workbook.add_worksheet_by_name("Older functions")?;
199 worksheet10.write_dynamic_array_formula("B1", "=LEN(A1:A3)")?;
200
201 // Write the data the to work on.
202 worksheet10.write("A1", "Foo")?;
203 worksheet10.write("A2", "Food")?;
204 worksheet10.write("A3", "Frood")?;
205
206
207 workbook.save_as("examples/dynamic_arrays.xlsx")?;
208 Ok(())
209}
Sourcepub fn set_align(self, format_align_type: FormatAlignType) -> Self
pub fn set_align(self, format_align_type: FormatAlignType) -> Self
Set the alignment of the format, learn more about it in FormatAlign
.
§Arguments
arg | type | meaning |
---|---|---|
format_align_type | FormatAlignType | The type of alignment to apply to the format. |
§Returns
Returns the modified Format with the alignment of the format set to the provided type.
Examples found in repository?
3fn main() -> WorkbookResult<()> {
4 let mut workbook = Workbook::new();
5 let worksheet = workbook.get_worksheet_mut(1)?;
6
7 // Increase the cell size of the merged cells to highlight the formatting.
8 worksheet.set_columns_width("B:D", 18.0)?;
9 worksheet.set_row_height(4, 40.0)?;
10 worksheet.set_row_height(7, 30.0)?;
11 worksheet.set_row_height(8, 30.0)?;
12
13 // Create a format to use in the merged range.
14 let merge_format = Format::default()
15 .set_bold()
16 .set_border(FormatBorderType::Double)
17 .set_align(FormatAlignType::Center)
18 .set_align(FormatAlignType::VerticalCenter)
19 .set_background_color(FormatColor::RGB(255, 255, 0));
20
21 // Merge 3 cells.
22 worksheet.merge_range_with_format("B4:D4", "Merged Range", &merge_format)?;
23 // Merge 3 cells over two rows.
24 worksheet.merge_range_with_format("B7:D8", "Merged Range", &merge_format)?;
25
26 workbook.save_as("examples/merge.xlsx")?;
27 Ok(())
28}
More examples
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7 // write some text
8 WorkSheet::write(worksheet, "A1", "Hello")?;
9 worksheet.write("B1", "World")?;
10 worksheet.write("C1", "Rust")?;
11 // Adjust font size
12 let big = Format::default().set_size(32);
13 worksheet.write_with_format("B1", "big text", &big)?;
14 // Change font color
15 let red = Format::default().set_color(FormatColor::RGB(255, 119, 119));
16 worksheet.write_with_format("C1", "red text", &red)?;
17 // Change the font style
18 let bold = red.set_bold();
19 worksheet.write_with_format("D1", "red bold text", &bold)?;
20 // Change font
21 let font = Format::default().set_font("华文行楷");
22 worksheet.write_with_format("E1", "你好", &font)?;
23 // adjust the text align
24 let left_top = Format::default().set_align(FormatAlignType::Left).set_align(FormatAlignType::Top);
25 worksheet.write_with_format("A2", "left top", &left_top)?;
26 // add borders
27 let thin_border = Format::default().set_border(FormatBorderType::Thin);
28 worksheet.write_with_format("B2", "bordered text", &thin_border)?;
29 // add background
30 let red_background = Format::default().set_background_color(FormatColor::RGB(255, 119, 119));
31 worksheet.write_with_format("C2", "red", &red_background)?;
32 // add a number
33 worksheet.write("D2", std::f64::consts::PI)?;
34 // add a new worksheet and set a tab color
35 let worksheet = workbook.add_worksheet_by_name("Other examples")?;
36 worksheet.set_tab_color(&FormatColor::RGB(255, 153, 0)); // Orange
37 // Set a background.
38 worksheet.set_background("examples/pics/ferris.png")?;
39 // Create a format to use in the merged range.
40 let merge_format = Format::default()
41 .set_bold()
42 .set_border(FormatBorderType::Double)
43 .set_align(FormatAlignType::Center)
44 .set_align(FormatAlignType::VerticalCenter)
45 .set_background_color(FormatColor::RGB(255, 255, 0));
46 // Merge cells.
47 worksheet.merge_range_with_format("A1:C3", "Merged Range", &merge_format)?;
48 // Add an image
49 worksheet.insert_image("A4:C10", &"./examples/pics/rust.png")?;
50 workbook.save_as("examples/hello_world.xlsx")?;
51 Ok(())
52}
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, ¢er_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, ¢er_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, ¢er_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", ¢er_format)?;
93 }
94 for row in 1..=50 {
95 worksheet4.write_with_format((row, 1), "Scroll", ¢er_format)?;
96 for col in 1..=26 {
97 worksheet4.write_with_format((row, col), col, ¢er_format)?;
98 }
99 }
100
101 workbook.save_as("examples/panes.xlsx")?;
102 Ok(())
103}
Sourcepub fn set_reading_order(self, reading_order: u8) -> Self
pub fn set_reading_order(self, reading_order: u8) -> Self
Set the indent of the text alignment, learn more about it in FormatAlign
.
§Arguments
arg | type | meaning |
---|---|---|
indent | u8 | The new reading order of the text alignment. |
§Returns
Returns the modified Format with the reading order of the text alignment set to the provided value.
Examples found in repository?
3fn main() -> WorkbookResult<()> {
4 // Add the cell formats.
5 let format_left_to_right = Format::default()
6 .set_reading_order(1);
7 let format_right_to_left = Format::default()
8 .set_reading_order(2);
9
10 // Create a new workbook
11 let mut workbook = Workbook::new();
12 // Set up some worksheets and set tab colors
13 let worksheet1 = workbook.get_worksheet_mut(1)?;
14
15 // Make the columns wider for clarity.
16 worksheet1.set_columns_width("A:A", 25.0)?;
17
18 // Standard direction: | A1 | B1 | C1 | ...
19 worksheet1.write("A1", "نص عربي / English text")?; // Default direction.
20 worksheet1.write_with_format("A2", "نص عربي / English text", &format_left_to_right)?;
21 worksheet1.write_with_format("A3", "نص عربي / English text", &format_right_to_left)?;
22
23 let worksheet2 = workbook.add_worksheet()?;
24 worksheet2.set_columns_width("A:A", 25.0)?;
25 worksheet2.right_to_left();
26
27 // Right to left direction: ... | C1 | B1 | A1 |
28 worksheet2.write("A1", "نص عربي / English text")?; // Default direction.
29 worksheet2.write_with_format("A2", "نص عربي / English text", &format_left_to_right)?;
30 worksheet2.write_with_format("A3", "نص عربي / English text", &format_right_to_left)?;
31
32 workbook.save_as("examples/right_to_left.xlsx")?;
33
34 Ok(())
35}
Sourcepub fn set_indent(self, indent: u8) -> Self
pub fn set_indent(self, indent: u8) -> Self
Set the indent of the text alignment, learn more about it in FormatAlign
.
§Arguments
arg | type | meaning |
---|---|---|
indent | u8 | The new indent of the text alignment. |
§Returns
Returns the modified Format with the indent of the text alignment set to the provided value.
Examples found in repository?
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7
8 let indent1 = Format::default().set_indent(1);
9 let indent2 = Format::default().set_indent(2);
10
11 worksheet.set_columns_width("A:A", 40.0)?;
12
13 worksheet.write_with_format("A1", "This text is indented 1 level", &indent1)?;
14 worksheet.write_with_format("A2", "This text is indented 2 levels", &indent2)?;
15
16 // Note: Alignment is not applied correctly when changing the reading order, this bug will be fixed in the future!
17 // let indent = Format::default().set_reading_order(2).set_align(FormatAlignType::Right).set_indent(2);
18 // worksheet.right_to_left();
19
20 workbook.save_as("examples/text_indent.xlsx")?;
21 Ok(())
22}