Crates.io | rust_xlsxwriter_derive |
lib.rs | rust_xlsxwriter_derive |
version | 0.2.0 |
source | src |
created_at | 2024-01-13 01:09:18.151921 |
updated_at | 2024-01-23 23:36:14.001416 |
description | Proc macros to support rust_xlsxwriter serialization |
homepage | https://rustxlsxwriter.github.io |
repository | https://github.com/jmcnamara/rust_xlsxwriter |
max_upload_size | |
id | 1098202 |
size | 46,790 |
The rust_xlsxwriter_derive
crate provides the XlsxSerialize
derived
trait which is used in conjunction with rust_xlsxwriter
serialization.
XlsxSerialize
can be used to set container and field attributes for structs to
define Excel formatting and other options when serializing them to Excel using
rust_xlsxwriter
and Serde
.
use rust_xlsxwriter::{Workbook, XlsxError, XlsxSerialize};
use serde::Serialize;
fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
// Add a worksheet to the workbook.
let worksheet = workbook.add_worksheet();
// Create a serializable struct.
#[derive(XlsxSerialize, Serialize)]
#[xlsx(header_format = Format::new().set_bold())]
struct Produce {
#[xlsx(rename = "Item", column_width = 12.0)]
fruit: &'static str,
#[xlsx(rename = "Price", num_format = "$0.00")]
cost: f64,
}
// Create some data instances.
let items = [
Produce {
fruit: "Peach",
cost: 1.05,
},
Produce {
fruit: "Plum",
cost: 0.15,
},
Produce {
fruit: "Pear",
cost: 0.75,
},
];
// Set the serialization location and headers.
worksheet.set_serialize_headers::<Produce>(0, 0)?;
// Serialize the data.
worksheet.serialize(&items)?;
// Save the file to disk.
workbook.save("serialize.xlsx")?;
Ok(())
}
The output file is shown below. Note the change or column width in Column A, the renamed headers and the currency format in Column B numbers.
For more information see the documentation on Working with Serde in the
rust_xlsxwriter
docs.