2个不稳定版本
0.2.0 | 2024年1月23日 |
---|---|
0.1.0 | 2024年1月13日 |
#41 in #xlsx
611 个月下载量
在 3 个crate中使用 (通过 rust_xlsxwriter)
47KB
426 代码行
rust_xlsxwriter_derive
rust_xlsxwriter_derive
crate提供XlsxSerialize
派生特质,该特质与rust_xlsxwriter
序列化结合使用。
XlsxSerialize
可以用于设置结构体的容器和字段属性,以在将结构体序列化为Excel时定义Excel格式和其他选项,使用rust_xlsxwriter
和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(())
}
以下为输出文件。注意A列中的列宽变化、重命名的标题和列B中的货币格式。

有关更多信息,请参阅rust_xlsxwriter
文档中关于使用Serde的文档。
另请参阅
依赖关系
~265–710KB
~17K SLoC