导出excel功能是很常见的功能,读者可根据我的代码,自行修改成需要的内容。
引入依赖
[dependencies]
# 用来生成新的文件名
uuid = {version= "0.7",features=["serde","v4"]}
# 用于生成存储时的路径名
chrono = "*"
# 导出excel关键依赖
simple_excel_writer = "0.1.7"
代码实现
返回url,前端通过请求获取到的url,来下载excel
use uuid::Uuid;
use chrono::prelude::*;
use simple_excel_writer::*;
pub fn export() -> String {
//本地存储路径
let save_path= "E:/";
//文件代理路径
let url_path= "http://127.0.0.1/";
// 当前时间
let date = Local::now().format("%Y-%m-%d").to_string();
std::fs::create_dir(format!("{}{}/",save_path, date.clone()));
let uid = Uuid::new_v4().to_string()[0..8].to_string();
let filename =format!("{}{}/{}.xlsx",save_path,date,uid) ;
let url_name =format!("{}{}/{}.xlsx",url_path,date,uid) ;
let mut wb = Workbook::create(&filename);
let mut sheet = wb.create_sheet("第一页");
// 设置行宽
sheet.add_column(Column { width: 30.0 });
sheet.add_column(Column { width: 30.0 });
sheet.add_column(Column { width: 30.0 });
sheet.add_column(Column { width: 30.0 });
wb.write_sheet(&mut sheet, |sheet_writer| {
let sw = sheet_writer;
sw.append_row(row!["姓名", "标题"])?;
sw.append_row(row!["张三","你好"])
}).expect("写入excel错误!");
let mut sheet = wb.create_sheet("第二页");
wb.write_sheet(&mut sheet, |sheet_writer| {
let sw = sheet_writer;
sw.append_row(row!["姓名", "标题"])?;
sw.append_row(row!["张三","你好"])
}).expect("写入excel错误!");
wb.close().expect("关闭excel错误!");
url_name
}