pub trait Write: _Write {
Show 31 methods
// Provided methods
fn write_cell<L: Location, T: Clone + CellDisplay + CellValue>(
&mut self,
loc: L,
cell: &Cell<T>,
) -> WorkSheetResult<()> { ... }
fn write<L: Location, T: Default + Clone + CellDisplay + CellValue>(
&mut self,
loc: L,
data: T,
) -> WorkSheetResult<()> { ... }
fn write_string<L: Location>(
&mut self,
loc: L,
data: String,
) -> WorkSheetResult<()> { ... }
fn write_rich_string<L: Location>(
&mut self,
loc: L,
data: &RichText,
) -> WorkSheetResult<()> { ... }
fn write_number<L: Location>(
&mut self,
loc: L,
data: i32,
) -> WorkSheetResult<()> { ... }
fn write_double<L: Location>(
&mut self,
loc: L,
data: f64,
) -> WorkSheetResult<()> { ... }
fn write_boolean<L: Location>(
&mut self,
loc: L,
data: bool,
) -> WorkSheetResult<()> { ... }
fn write_row<L: Location, T: CellDisplay + CellValue>(
&mut self,
loc: L,
data: &[T],
) -> WorkSheetResult<()> { ... }
fn write_row_cells<L: Location, T: CellDisplay + CellValue + Clone>(
&mut self,
loc: L,
cells: &[Cell<T>],
) -> WorkSheetResult<()> { ... }
fn write_column<L: Location, T: CellDisplay + CellValue>(
&mut self,
loc: L,
data: &[T],
) -> WorkSheetResult<()> { ... }
fn write_column_cells<L: Location, T: CellDisplay + CellValue + Clone>(
&mut self,
loc: L,
cells: &[Cell<T>],
) -> WorkSheetResult<()> { ... }
fn write_url<L: Location>(
&mut self,
loc: L,
url: &str,
) -> WorkSheetResult<()> { ... }
fn write_url_text<L: Location, T: CellDisplay + CellValue>(
&mut self,
loc: L,
url: &str,
data: &str,
) -> WorkSheetResult<()> { ... }
fn merge_range<L: LocationRange, T: CellDisplay + CellValue>(
&mut self,
loc: L,
data: T,
) -> WorkSheetResult<()> { ... }
fn write_formula<L: Location>(
&mut self,
loc: L,
data: &str,
) -> WorkSheetResult<()> { ... }
fn write_old_formula<L: Location>(
&mut self,
loc: L,
data: &str,
) -> WorkSheetResult<()> { ... }
fn write_array_formula<L: Location>(
&mut self,
loc: L,
data: &str,
) -> WorkSheetResult<()> { ... }
fn write_dynamic_array_formula<L: Location>(
&mut self,
loc: L,
data: &str,
) -> WorkSheetResult<()> { ... }
fn write_with_format<L: Location, T: Default + Clone + CellDisplay + CellValue>(
&mut self,
loc: L,
data: T,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_string_with_format<L: Location>(
&mut self,
loc: L,
data: String,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_number_with_format<L: Location>(
&mut self,
loc: L,
data: i32,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_double_with_format<L: Location>(
&mut self,
loc: L,
data: f64,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_boolean_with_format<L: Location>(
&mut self,
loc: L,
data: bool,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_row_with_format<L: Location, T: CellDisplay + CellValue>(
&mut self,
loc: L,
data: Iter<'_, T>,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_column_with_format<L: Location, T: CellDisplay + CellValue>(
&mut self,
loc: L,
data: Iter<'_, T>,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_url_with_format<L: Location>(
&mut self,
loc: L,
url: &str,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_url_text_with_format<L: Location>(
&mut self,
loc: L,
url: &str,
data: &str,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_formula_with_format<L: Location>(
&mut self,
loc: L,
data: &str,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_array_formula_with_format<L: Location>(
&mut self,
loc: L,
data: &str,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn write_dynamic_array_formula_with_format<L: LocationRange>(
&mut self,
loc_range: L,
data: &str,
format: &Format,
) -> WorkSheetResult<()> { ... }
fn merge_range_with_format<L: LocationRange, T: CellDisplay + CellValue>(
&mut self,
loc: L,
data: T,
format: &Format,
) -> WorkSheetResult<()> { ... }
}Provided Methods§
Sourcefn write_cell<L: Location, T: Clone + CellDisplay + CellValue>(
&mut self,
loc: L,
cell: &Cell<T>,
) -> WorkSheetResult<()>
fn write_cell<L: Location, T: Clone + CellDisplay + CellValue>( &mut self, loc: L, cell: &Cell<T>, ) -> WorkSheetResult<()>
Examples found in repository?
examples/read_and_copy.rs (line 46)
7fn main() -> WorkbookResult<()> {
8 // Read an existed workbook
9 let reading_book = Workbook::from_path("./tests/xlsx/accounting.xlsx")?;
10 let reading_sheet = reading_book.get_worksheet_by_name("worksheet")?;
11 // Create a new workbook to write
12 let mut writing_book = Workbook::new();
13 let writing_sheet = writing_book.get_worksheet_mut(1)?;
14
15 // Synchronous column width and format
16 let columns_map = reading_sheet.get_columns_with_format("A:XFD")?;
17 match reading_sheet.get_default_column() {
18 None => writing_sheet.set_default_column_adaptive(),
19 Some(width) => writing_sheet.set_default_column(width),
20 }
21 columns_map.iter().for_each(|(col_range, (column, format))| {
22 if let Some(format) = format {
23 // if col format exists, write it to writing_sheet
24 writing_sheet.set_columns_with_format(col_range, column, format).unwrap()
25 } else {
26 writing_sheet.set_columns(col_range, column).unwrap()
27 }
28 });
29
30 // Synchronous row height and format
31 writing_sheet.set_default_row(reading_sheet.get_default_row());
32 for row_number in 1..=reading_sheet.max_row() {
33 let (row, format) = reading_sheet.get_row_with_format(row_number)?;
34 if let Some(format) = format {
35 // if col format exists, write it to writing_sheet
36 writing_sheet.set_row_with_format(row_number, &row, &format)?;
37 } else {
38 writing_sheet.set_row(row_number, &row)?;
39 }
40 }
41
42 // Read then write text and format
43 for row in 1..=reading_sheet.max_row() {
44 for col in 1..=reading_sheet.max_column() {
45 if let Ok(cell) = reading_sheet.read_cell((row, col)) {
46 writing_sheet.write_cell((row, col), &cell)?;
47 }
48 }
49 }
50
51 writing_book.save_as("./examples/read_and_copy.xlsx")?;
52 Ok(())
53}Sourcefn write<L: Location, T: Default + Clone + CellDisplay + CellValue>(
&mut self,
loc: L,
data: T,
) -> WorkSheetResult<()>
fn write<L: Location, T: Default + Clone + CellDisplay + CellValue>( &mut self, loc: L, data: T, ) -> WorkSheetResult<()>
Examples found in repository?
examples/hide_row_col.rs (line 10)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6
7 let worksheet = workbook.get_worksheet_mut(1)?;
8
9 // Write some data.
10 worksheet.write("D1", "Some hidden columns.")?;
11 worksheet.write("A8", "Some hidden rows.")?;
12
13 // Hide all rows without data.
14 worksheet.hide_unused_rows(true);
15
16 // Set the height of empty rows that we do want to display even if it is
17 // the default height.
18 for row in 2..=7 {
19 worksheet.set_row_height(row, 15.0)?;
20 }
21
22 // Columns can be hidden explicitly. This doesn't increase the file size..
23 worksheet.hide_columns("G:XFD")?;
24
25 workbook.save_as("examples/hide_row_col.xlsx")?;
26 Ok(())
27}More examples
examples/doc_properties.rs (line 20)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let mut properties = Properties::default();
7 properties.set_title("This is an example spreadsheet")
8 .set_subject("With document properties")
9 .set_author("pt")
10 .set_manager("example manager")
11 .set_company("example company")
12 .set_category("Example spreadsheets")
13 .set_keywords("Sample, Example, Properties")
14 .set_comments("Created with Rust")
15 .set_status("example status");
16 workbook.set_properties(&properties)?;
17 // Use the default worksheet
18 let worksheet = workbook.get_worksheet_mut(1)?;
19 worksheet.set_columns_width("A:A", 70.0)?;
20 worksheet.write("A1", "Select 'Workbook Properties' to see properties.")?;
21 workbook.save_as("examples/doc_properties.xlsx")?;
22 Ok(())
23}examples/array_formula.rs (line 9)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 // Use the default worksheet
7 let worksheet = workbook.get_worksheet_mut(1)?;
8 // Write some test data.
9 worksheet.write("B1", 500)?;
10 worksheet.write("B2", 10)?;
11 worksheet.write("B5", 1)?;
12 worksheet.write("B6", 2)?;
13 worksheet.write("B7", 3)?;
14 worksheet.write("C1", 300)?;
15 worksheet.write("C2", 15)?;
16 worksheet.write("C5", 20234)?;
17 worksheet.write("C6", 21003)?;
18 worksheet.write("C7", 10000)?;
19 // Write an array formula that returns a single value
20 worksheet.write_formula("A1", "_xlfn.SUM(B1:C1*B2:C2)")?;
21 // Same as above but more verbose.
22 worksheet.write_array_formula("A2", "_xlfn.SUM(B1:C1*B2:C2)")?;
23 // Write an array formula that returns a range of values
24 worksheet.write_array_formula("A5", "_xlfn.TREND(C5:C7,B5:B7)")?;
25
26 workbook.save_as("examples/array_formula.xlsx")?;
27 Ok(())
28}examples/hide_sheet.rs (line 9)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet1 = workbook.get_worksheet_mut(1)?;
7
8 worksheet1.set_columns_width("A:A", 30.0)?;
9 worksheet1.write("A1", "Sheet2 is hidden")?;
10
11 // Hide Sheet2. It won't be visible until it is unhidden in Excel.
12 let worksheet2 = workbook.add_worksheet()?;
13 worksheet2.set_columns_width("A:A", 30.0)?;
14 // worksheet2.activate();
15 worksheet2.hide();
16 worksheet2.write("A1", "Now it's my turn to find you!")?;
17 // Note, you can't hide the "active" worksheet, which generally is the
18 // first worksheet, since this would cause an Excel error. So, in order to hide
19 // the first sheet you will need to activate another worksheet:
20 //
21 // worksheet2.activate();
22 // worksheet1.hide();
23
24 let worksheet3 = workbook.add_worksheet()?;
25 worksheet3.set_columns_width("A:A", 30.0)?;
26 worksheet3.write("A1", "Sheet2 is hidden")?;
27
28 workbook.save_as("examples/hide_sheet.xlsx")?;
29 Ok(())
30}examples/old_array_formula.rs (line 14)
3fn main() -> WorkbookResult<()> {
4 /// In Office 365, many new functions have emerged.
5 /// However, these functions may conflict with older versions of Excel.
6 /// You can use the 'write_old_formula' method,
7 /// which will minimize potential conflicts as much as possible.
8
9 // Create a new workbook
10 let mut workbook = Workbook::new();
11 // Use the default worksheet
12 let worksheet = workbook.get_worksheet_mut(1)?;
13 // Write some test data.
14 worksheet.write("B1", 500)?;
15 worksheet.write("B2", 10)?;
16 worksheet.write("B5", 1)?;
17 worksheet.write("B6", 2)?;
18 worksheet.write("B7", 3)?;
19 worksheet.write("C1", 300)?;
20 worksheet.write("C2", 15)?;
21 worksheet.write("C5", 20234)?;
22 worksheet.write("C6", 21003)?;
23 worksheet.write("C7", 10000)?;
24 // Write an array formula that returns a single value
25 worksheet.write_old_formula("A1", "_xlfn.SUM(B1:C1*B2:C2)")?;
26 // Same as above but more verbose.
27 worksheet.write_old_formula("A2", "_xlfn.SUM(B1:C1*B2:C2)")?;
28 // Write an array formula that returns a range of values
29 worksheet.write_old_formula("A5", "_xlfn.TREND(C5:C7,B5:B7)")?;
30
31 workbook.save_as("examples/old_array_formula.xlsx")?;
32 Ok(())
33}examples/ignore_errors.rs (line 27)
4fn main() -> WorkbookResult<()> {
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7
8 // Write strings that looks like numbers. This will cause an Excel warning.
9 worksheet.write_string("C2", "123".to_string())?;
10 worksheet.write_string("C3", "123".to_string())?;
11
12 // Write a divide by zero formula. This will also cause an Excel warning.
13 worksheet.write_formula("C5", "=1/0")?;
14 worksheet.write_formula("C6", "=1/0")?;
15 // In older versions of Excel, you could use the write_old_formula method:
16 // worksheet.write_old_formula("C5", "=1/0")?;
17 // worksheet.write_old_formula("C6", "=1/0")?;
18
19 // Turn off some of the warnings:
20 let mut error_map = HashMap::new();
21 error_map.insert("number_stored_as_text", "C3");
22 error_map.insert("eval_error", "C6");
23 worksheet.ignore_errors(error_map);
24
25 // Write some descriptions for the cells and make the column wider for clarity.
26 worksheet.set_columns_width("B:B", 16.0)?;
27 worksheet.write("B2", "Warning:")?;
28 worksheet.write("B3", "Warning turned off:")?;
29 worksheet.write("B5", "Warning:")?;
30 worksheet.write("B6", "Warning turned off:")?;
31
32 workbook.save_as("examples/ignore_errors.xlsx")?;
33 Ok(())
34}Sourcefn write_string<L: Location>(
&mut self,
loc: L,
data: String,
) -> WorkSheetResult<()>
fn write_string<L: Location>( &mut self, loc: L, data: String, ) -> WorkSheetResult<()>
Examples found in repository?
examples/ignore_errors.rs (line 9)
4fn main() -> WorkbookResult<()> {
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7
8 // Write strings that looks like numbers. This will cause an Excel warning.
9 worksheet.write_string("C2", "123".to_string())?;
10 worksheet.write_string("C3", "123".to_string())?;
11
12 // Write a divide by zero formula. This will also cause an Excel warning.
13 worksheet.write_formula("C5", "=1/0")?;
14 worksheet.write_formula("C6", "=1/0")?;
15 // In older versions of Excel, you could use the write_old_formula method:
16 // worksheet.write_old_formula("C5", "=1/0")?;
17 // worksheet.write_old_formula("C6", "=1/0")?;
18
19 // Turn off some of the warnings:
20 let mut error_map = HashMap::new();
21 error_map.insert("number_stored_as_text", "C3");
22 error_map.insert("eval_error", "C6");
23 worksheet.ignore_errors(error_map);
24
25 // Write some descriptions for the cells and make the column wider for clarity.
26 worksheet.set_columns_width("B:B", 16.0)?;
27 worksheet.write("B2", "Warning:")?;
28 worksheet.write("B3", "Warning turned off:")?;
29 worksheet.write("B5", "Warning:")?;
30 worksheet.write("B6", "Warning turned off:")?;
31
32 workbook.save_as("examples/ignore_errors.xlsx")?;
33 Ok(())
34}fn write_rich_string<L: Location>( &mut self, loc: L, data: &RichText, ) -> WorkSheetResult<()>
fn write_number<L: Location>( &mut self, loc: L, data: i32, ) -> WorkSheetResult<()>
fn write_double<L: Location>( &mut self, loc: L, data: f64, ) -> WorkSheetResult<()>
fn write_boolean<L: Location>( &mut self, loc: L, data: bool, ) -> WorkSheetResult<()>
Sourcefn write_row<L: Location, T: CellDisplay + CellValue>(
&mut self,
loc: L,
data: &[T],
) -> WorkSheetResult<()>
fn write_row<L: Location, T: CellDisplay + CellValue>( &mut self, loc: L, data: &[T], ) -> WorkSheetResult<()>
Examples found in repository?
examples/dynamic_arrays.rs (line 236)
211fn write_worksheet_data(worksheet: &mut WorkSheet, header: &Format) -> WorkSheetResult<()> {
212 worksheet.write_with_format("A1", "Region", &header)?;
213 worksheet.write_with_format("B1", "Sales Rep", &header)?;
214 worksheet.write_with_format("C1", "Product", &header)?;
215 worksheet.write_with_format("D1", "Units", &header)?;
216 let data = [
217 ["East", "Tom", "Apple"],
218 ["West", "Fred", "Grape"],
219 ["North", "Amy", "Pear"],
220 ["South", "Sal", "Banana"],
221 ["East", "Fritz", "Apple"],
222 ["West", "Sravan", "Grape"],
223 ["North", "Xi", "Pear"],
224 ["South", "Hector", "Banana"],
225 ["East", "Tom", "Banana"],
226 ["West", "Fred", "Pear"],
227 ["North", "Amy", "Grape"],
228 ["South", "Sal", "Apple"],
229 ["East", "Fritz", "Banana"],
230 ["West", "Sravan", "Pear"],
231 ["North", "Xi", "Grape"],
232 ["South", "Hector", "Apple"],
233 ];
234 let mut row_num = 2;
235 for data in data {
236 worksheet.write_row((row_num, 1), &data)?;
237 row_num += 1;
238 }
239 let units = [6380, 5619, 4565, 5323, 4394, 7195, 5231, 2427, 4213, 3239, 6520, 1310, 6274, 4894, 7580, 9814];
240 worksheet.write_column("D2", &units)?;
241 Ok(())
242}More examples
examples/duplicate_sheet.rs (line 20)
3fn main() -> WorkbookResult<()> {
4 // from an existed workbook
5 let mut workbook = Workbook::from_path("examples/xlsx/accounting.xlsx")?;
6 // Use the first worksheet as a template
7 let template = workbook.get_worksheet_mut(1)?;
8 template.insert_image("I1:L3", &"./examples/pics/ferris.png");
9 template.set_name("template")?;
10 // Deselect and hide the template.
11 template.deselect();
12 template.hide();
13 //
14 // Example of using the duplicate_worksheet() function.
15 //
16 let jan = workbook.duplicate_worksheet(1)?;
17 jan.write("A1", "Accounting Journal in Jan.")?;
18 jan.set_name("Jan.")?;
19 for row in 6..=15 {
20 jan.write_row((row, 3), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
21 }
22 let feb = workbook.duplicate_worksheet(1)?;
23 feb.write("A1", "Accounting Journal in Feb.")?;
24 feb.set_name("Feb.")?;
25 for row in 6..=15 {
26 feb.write_row((row, 3), &[2, 4, 6, 8, 10, 12, 14, 16, 18, 20])?;
27 }
28 // activate the Feb. sheet.
29 feb.activate();
30 // duplicate worksheet by sheet name
31 let mar = workbook.duplicate_worksheet_by_name("template")?;
32 mar.write("A1", "Accounting Journal in Mar.")?;
33 mar.set_name("Mar.")?;
34 for col in 'C'..='L' {
35 mar.write_column(&format!("{col}6"), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
36 }
37 workbook.save_as("examples/duplicate_sheet.xlsx")?;
38 Ok(())
39}examples/outline_collapsed.rs (line 128)
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}examples/outline.rs (line 127)
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}examples/autofilter.rs (line 29)
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}fn write_row_cells<L: Location, T: CellDisplay + CellValue + Clone>( &mut self, loc: L, cells: &[Cell<T>], ) -> WorkSheetResult<()>
Sourcefn write_column<L: Location, T: CellDisplay + CellValue>(
&mut self,
loc: L,
data: &[T],
) -> WorkSheetResult<()>
fn write_column<L: Location, T: CellDisplay + CellValue>( &mut self, loc: L, data: &[T], ) -> WorkSheetResult<()>
Examples found in repository?
examples/dynamic_arrays.rs (line 240)
211fn write_worksheet_data(worksheet: &mut WorkSheet, header: &Format) -> WorkSheetResult<()> {
212 worksheet.write_with_format("A1", "Region", &header)?;
213 worksheet.write_with_format("B1", "Sales Rep", &header)?;
214 worksheet.write_with_format("C1", "Product", &header)?;
215 worksheet.write_with_format("D1", "Units", &header)?;
216 let data = [
217 ["East", "Tom", "Apple"],
218 ["West", "Fred", "Grape"],
219 ["North", "Amy", "Pear"],
220 ["South", "Sal", "Banana"],
221 ["East", "Fritz", "Apple"],
222 ["West", "Sravan", "Grape"],
223 ["North", "Xi", "Pear"],
224 ["South", "Hector", "Banana"],
225 ["East", "Tom", "Banana"],
226 ["West", "Fred", "Pear"],
227 ["North", "Amy", "Grape"],
228 ["South", "Sal", "Apple"],
229 ["East", "Fritz", "Banana"],
230 ["West", "Sravan", "Pear"],
231 ["North", "Xi", "Grape"],
232 ["South", "Hector", "Apple"],
233 ];
234 let mut row_num = 2;
235 for data in data {
236 worksheet.write_row((row_num, 1), &data)?;
237 row_num += 1;
238 }
239 let units = [6380, 5619, 4565, 5323, 4394, 7195, 5231, 2427, 4213, 3239, 6520, 1310, 6274, 4894, 7580, 9814];
240 worksheet.write_column("D2", &units)?;
241 Ok(())
242}More examples
examples/duplicate_sheet.rs (line 35)
3fn main() -> WorkbookResult<()> {
4 // from an existed workbook
5 let mut workbook = Workbook::from_path("examples/xlsx/accounting.xlsx")?;
6 // Use the first worksheet as a template
7 let template = workbook.get_worksheet_mut(1)?;
8 template.insert_image("I1:L3", &"./examples/pics/ferris.png");
9 template.set_name("template")?;
10 // Deselect and hide the template.
11 template.deselect();
12 template.hide();
13 //
14 // Example of using the duplicate_worksheet() function.
15 //
16 let jan = workbook.duplicate_worksheet(1)?;
17 jan.write("A1", "Accounting Journal in Jan.")?;
18 jan.set_name("Jan.")?;
19 for row in 6..=15 {
20 jan.write_row((row, 3), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
21 }
22 let feb = workbook.duplicate_worksheet(1)?;
23 feb.write("A1", "Accounting Journal in Feb.")?;
24 feb.set_name("Feb.")?;
25 for row in 6..=15 {
26 feb.write_row((row, 3), &[2, 4, 6, 8, 10, 12, 14, 16, 18, 20])?;
27 }
28 // activate the Feb. sheet.
29 feb.activate();
30 // duplicate worksheet by sheet name
31 let mar = workbook.duplicate_worksheet_by_name("template")?;
32 mar.write("A1", "Accounting Journal in Mar.")?;
33 mar.set_name("Mar.")?;
34 for col in 'C'..='L' {
35 mar.write_column(&format!("{col}6"), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
36 }
37 workbook.save_as("examples/duplicate_sheet.xlsx")?;
38 Ok(())
39}examples/defined_name.rs (line 31)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 workbook.add_worksheet()?;
7
8 // Define some global/workbook names.
9 workbook.define_name("Exchange_rate", "0.96")?;
10 workbook.define_name("Sales", "Sheet1!$G$1:$H$10")?;
11 // Define a local/worksheet name. Over-rides the "Sales" name above.
12 workbook.define_local_name("Sales", "Sheet2!$G$1:$G$10", 2)?;
13
14 // Write some text in the file and one of the defined names in a formula.
15 let sales = ["Apple", "Grape", "Pear", "Banana", "Apple", "Grape", "Pear", "Banana", "Banana", "Pear"];
16 let units = [10, 12, 32, 16, 13, 50, 25, 8, 33, 95];
17 for worksheet in workbook.worksheets_mut() {
18 worksheet.set_columns_width("A:B", 40.0)?;
19 worksheet.set_columns_width("F:F", 40.0)?;
20 worksheet.write("A1", "This worksheet contains some defined names.")?;
21 worksheet.write("B1", "Show defined name Sales on the right->")?;
22 worksheet.write_formula("C1", "=Sales")?;
23 // In older versions of Excel, you could use the write_old_formula method:
24 // worksheet.write_old_formula("C1", "=Sales")?;
25 worksheet.write("A2", "See Formulas -> Name Manager above.")?;
26 worksheet.write("A3", "Example formula in cell B3 ->")?;
27 worksheet.write_formula("B3", "=Exchange_rate")?;
28 // In older versions of Excel, you could use the write_old_formula method:
29 // worksheet.write_old_formula("B3", "=Exchange_rate")?;
30 worksheet.write("F1", "Fill in some arrays on the right->")?;
31 worksheet.write_column("G1", &sales)?;
32 worksheet.write_column("H1", &units)?;
33 }
34
35 workbook.save_as("examples/defined_name.xlsx")?;
36 Ok(())
37}examples/outline_collapsed.rs (line 133)
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}examples/outline.rs (line 132)
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}fn write_column_cells<L: Location, T: CellDisplay + CellValue + Clone>( &mut self, loc: L, cells: &[Cell<T>], ) -> WorkSheetResult<()>
fn write_url<L: Location>(&mut self, loc: L, url: &str) -> WorkSheetResult<()>
fn write_url_text<L: Location, T: CellDisplay + CellValue>( &mut self, loc: L, url: &str, data: &str, ) -> WorkSheetResult<()>
fn merge_range<L: LocationRange, T: CellDisplay + CellValue>( &mut self, loc: L, data: T, ) -> WorkSheetResult<()>
Sourcefn write_formula<L: Location>(
&mut self,
loc: L,
data: &str,
) -> WorkSheetResult<()>
fn write_formula<L: Location>( &mut self, loc: L, data: &str, ) -> WorkSheetResult<()>
Examples found in repository?
examples/array_formula.rs (line 20)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 // Use the default worksheet
7 let worksheet = workbook.get_worksheet_mut(1)?;
8 // Write some test data.
9 worksheet.write("B1", 500)?;
10 worksheet.write("B2", 10)?;
11 worksheet.write("B5", 1)?;
12 worksheet.write("B6", 2)?;
13 worksheet.write("B7", 3)?;
14 worksheet.write("C1", 300)?;
15 worksheet.write("C2", 15)?;
16 worksheet.write("C5", 20234)?;
17 worksheet.write("C6", 21003)?;
18 worksheet.write("C7", 10000)?;
19 // Write an array formula that returns a single value
20 worksheet.write_formula("A1", "_xlfn.SUM(B1:C1*B2:C2)")?;
21 // Same as above but more verbose.
22 worksheet.write_array_formula("A2", "_xlfn.SUM(B1:C1*B2:C2)")?;
23 // Write an array formula that returns a range of values
24 worksheet.write_array_formula("A5", "_xlfn.TREND(C5:C7,B5:B7)")?;
25
26 workbook.save_as("examples/array_formula.xlsx")?;
27 Ok(())
28}More examples
examples/lambda.rs (line 13)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7
8
9 // Write a Lambda function to convert Fahrenheit to Celsius to a cell.
10 //
11 // Note that the lambda function parameters must be prefixed with
12 // "_xlpm.". These prefixes won't show up in Excel.
13 worksheet.write_formula("A1", "_xlfn.LAMBDA(_xlpm.a, _xlpm.b, SQRT((_xlpm.a^2+_xlpm.b^2)))(3, 4)")?;
14
15
16 // The user defined name needs to be written explicitly as a dynamic array
17 // formula.
18 let a = 6;
19 let b = 8;
20 worksheet.write_formula("A2", &format!("=HYPOTENUSE({a}, {b})"))?;
21
22 // Create the same formula (without an argument) as a defined name and use that
23 // to calculate a value.
24 //
25 // Note that the formula name is prefixed with "_xlfn." (this is normally
26 // converted automatically by write_formula() but isn't for defined names)
27 // and note that the lambda function parameters are prefixed with
28 // "_xlpm.". These prefixes won't show up in Excel.
29 workbook.define_name("HYPOTENUSE", "_xlfn.LAMBDA(_xlpm.a, _xlpm.b, SQRT((_xlpm.a^2+_xlpm.b^2)))")?;
30
31
32 workbook.save_as("examples/lambda.xlsx")?;
33 Ok(())
34}examples/ignore_errors.rs (line 13)
4fn main() -> WorkbookResult<()> {
5 let mut workbook = Workbook::new();
6 let worksheet = workbook.get_worksheet_mut(1)?;
7
8 // Write strings that looks like numbers. This will cause an Excel warning.
9 worksheet.write_string("C2", "123".to_string())?;
10 worksheet.write_string("C3", "123".to_string())?;
11
12 // Write a divide by zero formula. This will also cause an Excel warning.
13 worksheet.write_formula("C5", "=1/0")?;
14 worksheet.write_formula("C6", "=1/0")?;
15 // In older versions of Excel, you could use the write_old_formula method:
16 // worksheet.write_old_formula("C5", "=1/0")?;
17 // worksheet.write_old_formula("C6", "=1/0")?;
18
19 // Turn off some of the warnings:
20 let mut error_map = HashMap::new();
21 error_map.insert("number_stored_as_text", "C3");
22 error_map.insert("eval_error", "C6");
23 worksheet.ignore_errors(error_map);
24
25 // Write some descriptions for the cells and make the column wider for clarity.
26 worksheet.set_columns_width("B:B", 16.0)?;
27 worksheet.write("B2", "Warning:")?;
28 worksheet.write("B3", "Warning turned off:")?;
29 worksheet.write("B5", "Warning:")?;
30 worksheet.write("B6", "Warning turned off:")?;
31
32 workbook.save_as("examples/ignore_errors.xlsx")?;
33 Ok(())
34}examples/defined_name.rs (line 22)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 workbook.add_worksheet()?;
7
8 // Define some global/workbook names.
9 workbook.define_name("Exchange_rate", "0.96")?;
10 workbook.define_name("Sales", "Sheet1!$G$1:$H$10")?;
11 // Define a local/worksheet name. Over-rides the "Sales" name above.
12 workbook.define_local_name("Sales", "Sheet2!$G$1:$G$10", 2)?;
13
14 // Write some text in the file and one of the defined names in a formula.
15 let sales = ["Apple", "Grape", "Pear", "Banana", "Apple", "Grape", "Pear", "Banana", "Banana", "Pear"];
16 let units = [10, 12, 32, 16, 13, 50, 25, 8, 33, 95];
17 for worksheet in workbook.worksheets_mut() {
18 worksheet.set_columns_width("A:B", 40.0)?;
19 worksheet.set_columns_width("F:F", 40.0)?;
20 worksheet.write("A1", "This worksheet contains some defined names.")?;
21 worksheet.write("B1", "Show defined name Sales on the right->")?;
22 worksheet.write_formula("C1", "=Sales")?;
23 // In older versions of Excel, you could use the write_old_formula method:
24 // worksheet.write_old_formula("C1", "=Sales")?;
25 worksheet.write("A2", "See Formulas -> Name Manager above.")?;
26 worksheet.write("A3", "Example formula in cell B3 ->")?;
27 worksheet.write_formula("B3", "=Exchange_rate")?;
28 // In older versions of Excel, you could use the write_old_formula method:
29 // worksheet.write_old_formula("B3", "=Exchange_rate")?;
30 worksheet.write("F1", "Fill in some arrays on the right->")?;
31 worksheet.write_column("G1", &sales)?;
32 worksheet.write_column("H1", &units)?;
33 }
34
35 workbook.save_as("examples/defined_name.xlsx")?;
36 Ok(())
37}examples/outline_collapsed.rs (line 138)
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}examples/outline.rs (line 137)
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}Additional examples can be found in:
Sourcefn write_old_formula<L: Location>(
&mut self,
loc: L,
data: &str,
) -> WorkSheetResult<()>
fn write_old_formula<L: Location>( &mut self, loc: L, data: &str, ) -> WorkSheetResult<()>
Examples found in repository?
examples/old_array_formula.rs (line 25)
3fn main() -> WorkbookResult<()> {
4 /// In Office 365, many new functions have emerged.
5 /// However, these functions may conflict with older versions of Excel.
6 /// You can use the 'write_old_formula' method,
7 /// which will minimize potential conflicts as much as possible.
8
9 // Create a new workbook
10 let mut workbook = Workbook::new();
11 // Use the default worksheet
12 let worksheet = workbook.get_worksheet_mut(1)?;
13 // Write some test data.
14 worksheet.write("B1", 500)?;
15 worksheet.write("B2", 10)?;
16 worksheet.write("B5", 1)?;
17 worksheet.write("B6", 2)?;
18 worksheet.write("B7", 3)?;
19 worksheet.write("C1", 300)?;
20 worksheet.write("C2", 15)?;
21 worksheet.write("C5", 20234)?;
22 worksheet.write("C6", 21003)?;
23 worksheet.write("C7", 10000)?;
24 // Write an array formula that returns a single value
25 worksheet.write_old_formula("A1", "_xlfn.SUM(B1:C1*B2:C2)")?;
26 // Same as above but more verbose.
27 worksheet.write_old_formula("A2", "_xlfn.SUM(B1:C1*B2:C2)")?;
28 // Write an array formula that returns a range of values
29 worksheet.write_old_formula("A5", "_xlfn.TREND(C5:C7,B5:B7)")?;
30
31 workbook.save_as("examples/old_array_formula.xlsx")?;
32 Ok(())
33}Sourcefn write_array_formula<L: Location>(
&mut self,
loc: L,
data: &str,
) -> WorkSheetResult<()>
fn write_array_formula<L: Location>( &mut self, loc: L, data: &str, ) -> WorkSheetResult<()>
Examples found in repository?
examples/array_formula.rs (line 22)
3fn main() -> WorkbookResult<()> {
4 // Create a new workbook
5 let mut workbook = Workbook::new();
6 // Use the default worksheet
7 let worksheet = workbook.get_worksheet_mut(1)?;
8 // Write some test data.
9 worksheet.write("B1", 500)?;
10 worksheet.write("B2", 10)?;
11 worksheet.write("B5", 1)?;
12 worksheet.write("B6", 2)?;
13 worksheet.write("B7", 3)?;
14 worksheet.write("C1", 300)?;
15 worksheet.write("C2", 15)?;
16 worksheet.write("C5", 20234)?;
17 worksheet.write("C6", 21003)?;
18 worksheet.write("C7", 10000)?;
19 // Write an array formula that returns a single value
20 worksheet.write_formula("A1", "_xlfn.SUM(B1:C1*B2:C2)")?;
21 // Same as above but more verbose.
22 worksheet.write_array_formula("A2", "_xlfn.SUM(B1:C1*B2:C2)")?;
23 // Write an array formula that returns a range of values
24 worksheet.write_array_formula("A5", "_xlfn.TREND(C5:C7,B5:B7)")?;
25
26 workbook.save_as("examples/array_formula.xlsx")?;
27 Ok(())
28}Sourcefn write_dynamic_array_formula<L: Location>(
&mut self,
loc: L,
data: &str,
) -> WorkSheetResult<()>
fn write_dynamic_array_formula<L: Location>( &mut self, loc: L, data: &str, ) -> WorkSheetResult<()>
Examples found in repository?
examples/dynamic_arrays.rs (line 168)
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}Sourcefn write_with_format<L: Location, T: Default + Clone + CellDisplay + CellValue>(
&mut self,
loc: L,
data: T,
format: &Format,
) -> WorkSheetResult<()>
fn write_with_format<L: Location, T: Default + Clone + CellDisplay + CellValue>( &mut self, loc: L, data: T, format: &Format, ) -> WorkSheetResult<()>
Examples found in repository?
examples/text_indent.rs (line 13)
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}More examples
examples/outline_collapsed.rs (line 180)
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}examples/right_to_left.rs (line 20)
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}examples/hello_world.rs (line 13)
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}examples/panes.rs (line 27)
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}examples/outline.rs (line 34)
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}Additional examples can be found in:
fn write_string_with_format<L: Location>( &mut self, loc: L, data: String, format: &Format, ) -> WorkSheetResult<()>
fn write_number_with_format<L: Location>( &mut self, loc: L, data: i32, format: &Format, ) -> WorkSheetResult<()>
fn write_double_with_format<L: Location>( &mut self, loc: L, data: f64, format: &Format, ) -> WorkSheetResult<()>
fn write_boolean_with_format<L: Location>( &mut self, loc: L, data: bool, format: &Format, ) -> WorkSheetResult<()>
fn write_row_with_format<L: Location, T: CellDisplay + CellValue>( &mut self, loc: L, data: Iter<'_, T>, format: &Format, ) -> WorkSheetResult<()>
fn write_column_with_format<L: Location, T: CellDisplay + CellValue>( &mut self, loc: L, data: Iter<'_, T>, format: &Format, ) -> WorkSheetResult<()>
fn write_url_with_format<L: Location>( &mut self, loc: L, url: &str, format: &Format, ) -> WorkSheetResult<()>
fn write_url_text_with_format<L: Location>( &mut self, loc: L, url: &str, data: &str, format: &Format, ) -> WorkSheetResult<()>
Sourcefn write_formula_with_format<L: Location>(
&mut self,
loc: L,
data: &str,
format: &Format,
) -> WorkSheetResult<()>
fn write_formula_with_format<L: Location>( &mut self, loc: L, data: &str, format: &Format, ) -> WorkSheetResult<()>
Examples found in repository?
examples/outline_collapsed.rs (line 142)
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}More examples
examples/outline.rs (line 46)
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}fn write_array_formula_with_format<L: Location>( &mut self, loc: L, data: &str, format: &Format, ) -> WorkSheetResult<()>
fn write_dynamic_array_formula_with_format<L: LocationRange>( &mut self, loc_range: L, data: &str, format: &Format, ) -> WorkSheetResult<()>
Sourcefn merge_range_with_format<L: LocationRange, T: CellDisplay + CellValue>(
&mut self,
loc: L,
data: T,
format: &Format,
) -> WorkSheetResult<()>
fn merge_range_with_format<L: LocationRange, T: CellDisplay + CellValue>( &mut self, loc: L, data: T, format: &Format, ) -> WorkSheetResult<()>
Examples found in repository?
examples/merge.rs (line 22)
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
examples/hello_world.rs (line 47)
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}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.