io-excel

Crates.ioio-excel
lib.rsio-excel
version0.1.4
sourcesrc
created_at2024-10-22 13:32:38.972346
updated_at2024-10-23 23:27:02.79086
descriptionThis is a package that wraps around Calamine and rust_xlsxwriter, allowing for simple Excel read and write operations through procedural macros.
homepage
repositoryhttps://github.com/yipen9/io-excel
max_upload_size
id1418664
size13,622
(yipen9)

documentation

README

Cargo.toml 引入

io-excel = "0.1.4"
serde = { version = "1.0", features = ["derive"] }
calamine = "0.26.1"
rust_xlsxwriter = "0.79.0"

Code demo

Read Excel For Vec

Record::read_excel("中文名称.xlsx", "Sheet1"),分别为 file_path 和 sheet name

use io_excel::IOExcel;
#[derive(IOExcel, Debug)]
pub struct Record {
    #[column(name = "省份")]
    pub province: String,

    #[column(name = "城市")]
    pub city: String,
}

fn main() {
    let record_list = Record::read_excel("中文名称.xlsx", "Sheet1").unwrap();
    for record in &record_list {
        eprintln!("{:#?}", record);
    }
}

输出结果

Record {
    province: "湖北",
    city: "武汉",
}
Record {
    province: "湖北",
    city: "孝感",
}

Write Excel With Vec

Record::write_excel("中文名称.xlsx", "Sheet1", record_list).unwrap();分别为 file_path 和 sheet name and record_list

use io_excel::IOExcel;
#[derive(IOExcel, Debug)]
pub struct Record {
    #[column(name = "省份")]
    pub province: String,

    #[column(name = "城市")]
    pub city: String,
}

fn main() {
    let record_list = Record::read_excel("中文名称.xlsx", "Sheet1").unwrap();
    Record::write_excel("中文名称2.xlsx", "第二个中文名称", &record_list).unwrap();
}

Write Excel Which Some Column Is None

use io_excel::IOExcel;
#[derive(IOExcel, Debug)]
pub struct Record {
    #[column(name = "省份")]
    pub province: String,

    #[column(name = "城市")]
    pub city: Option<String>,

    #[column(name = "版本号")]
    pub name: u32,
}

fn main() {
    let record_list = Record::read_excel("中文名称.xlsx", "Sheet1").unwrap();
    for record in &record_list {
        eprintln!("{:#?}", record);
    }
    Record::write_excel("中文名称2.xlsx", "第二个中文名称", &record_list).unwrap();
}
Commit count: 12

cargo fmt