use std::fs::{create_dir_all, remove_dir_all};
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
use calamine::{open_workbook, DataType, Reader, Xlsx, Data};
use rbatis::rbdc::{Error};
use salvo::{Request, Response};
pub async fn import_excel_data(req:&mut Request){
//创建临时文件
create_dir_all("temp").unwrap();
//创建时间戳
let time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis();
//从req中获得上传的文件
let file = req.file("file").await;
let mut dest = "".to_string();
if let Some(file) = file {
dest = format!("temp/{}", time.to_string() + "_" + file.name().unwrap_or("file"));
match std::fs::copy(file.path(), Path::new(&dest)) {
Ok(_) => { println!("文件上传拷贝成功"); }
Err(_) => { println!("文件上传拷贝失败"); }
}
}
//导入到具体的实体类
let mut vec_user_import_data: Vec
let mut workbook: Xlsx<_> = open_workbook(dest).expect("无法读取文件");
match workbook.worksheet_range("员工") {
Ok(worksheet) => {
//跳过第一行表头数据
for row in worksheet.rows().skip(1) {
// 将每一列的数据存入员工实体
if let (Some(Data::String(user_name)),
Some(Data::String(nick_name)),
Some(Data::String(company)),
Some(Data::String(department)),
Some(Data::String(gender)),
Some(Data::String(email)),
Some(Data::String(telephone))) =
(row.get(0), row.get(1), row.get(2), row.get(3), row.get(4), row.get(5), row.get(6)) {
let mut user_entity = SysUserEntity{
user_id: None,
dept_id:Some(dept_id),
user_name: Some(user_name.to_string()),
nick_name: Some(nick_name.to_string()),
user_type: None,
email:Some(email.to_string()),
phone_number :Some(te