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