use rust_xlsxwriter::{
CustomSerializeField, Format, FormatBorder, SerializeFieldOptions, Workbook, XlsxError,
};
use serde::{Deserialize, Serialize};
fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
let header_format = Format::new()
.set_bold()
.set_border(FormatBorder::Thin)
.set_background_color("C6EFCE");
let currency_format = Format::new().set_num_format("$0.00");
#[derive(Deserialize, Serialize)]
struct Produce {
#[serde(rename = "Item")]
fruit: &'static str,
#[serde(rename = "Price")]
cost: f64,
}
let item1 = Produce {
fruit: "Peach",
cost: 1.05,
};
let item2 = Produce {
fruit: "Plum",
cost: 0.15,
};
let item3 = Produce {
fruit: "Pear",
cost: 0.75,
};
let custom_headers = [CustomSerializeField::new("Price").set_value_format(currency_format)];
let header_options = SerializeFieldOptions::new()
.set_header_format(header_format)
.set_custom_headers(&custom_headers);
worksheet.deserialize_headers_with_options::<Produce>(1, 1, &header_options)?;
worksheet.serialize(&item1)?;
worksheet.serialize(&item2)?;
worksheet.serialize(&item3)?;
workbook.save("serialize.xlsx")?;
Ok(())
}