Creating and using a Format object
Formats are created by calling the Format::new()
method and properties as
set using the various methods shown is this section of the document. Once
the Format has been created it can be passed to one of the worksheet
write_*()
methods. Multiple properties can be set by chaining them
together, for example:
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, John McNamara, jmcnamara@cpan.org
//! The following example demonstrates create a new format and setting the
//! properties.
use rust_xlsxwriter::{Color, Format, Workbook, XlsxError};
fn main() -> Result<(), XlsxError> {
// Create a new Excel file object.
let mut workbook = Workbook::new();
// Add a worksheet.
let worksheet = workbook.add_worksheet();
// Create a new format and set some properties.
let format = Format::new()
.set_bold()
.set_italic()
.set_font_color(Color::Red);
worksheet.write_string_with_format(0, 0, "Hello", &format)?;
workbook.save("formats.xlsx")?;
Ok(())
}
Output:
Formats can be cloned in the usual way:
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, John McNamara, jmcnamara@cpan.org
//! The following example demonstrates cloning a format and setting the
//! properties.
use rust_xlsxwriter::{Color, Format, Workbook, XlsxError};
fn main() -> Result<(), XlsxError> {
// Create a new Excel file object.
let mut workbook = Workbook::new();
// Add a worksheet.
let worksheet = workbook.add_worksheet();
// Create a new format and set some properties.
let format1 = Format::new().set_bold();
// Clone a new format and set some properties.
let format2 = format1.clone().set_font_color(Color::Blue);
worksheet.write_string_with_format(0, 0, "Hello", &format1)?;
worksheet.write_string_with_format(1, 0, "Hello", &format2)?;
workbook.save("formats.xlsx")?;
Ok(())
}
Output: