//导出
func (c *UserManageController) Export() {
var file *xlsx.File
var sheet *xlsx.Sheet
var row *xlsx.Row
var cell *xlsx.Cell
file = xlsx.NewFile()
sheet, _ = file.AddSheet("sheet")
//设置表格头
headers := []string{"姓名(必填)", "人员编号(必填,唯一,不能重复)", "照片(与导入照片名对应,照片大小不大于2M)", "卡号"}
row = sheet.AddRow()
for _, header := range headers {
cell = row.AddCell()
cell.Value = header
}
//读取config数据库信息
userManageData := make([]*models.UserManage, 0)
orm.NewOrm().QueryTable(models.UserManageTBName()).All(&userManageData)
//写入数据
//datas := [][]string {{"data1","data2","data3"},{"data1","data2","data3"},}
for _, data := range userManageData { //row
row = sheet.AddRow()
//col
cell = row.AddCell()
cell.Value = data.Name
cell = row.AddCell()
cell.Value = data.Pin
cell = row.AddCell()
//路径
wd, _ := os.Getwd()
cell.Value = wd + "/static/users/images/" + data.Photo
cell = row.AddCell()
cell.Value = data.Cardid
}
//保存路径
if !FileExists("static/excel") {
os.MkdirAll("static/excel", os.ModePerm)
}
filename := "static/excel/" + cast.ToString(time.Now().Unix()) + ".xlsx"
err := file.Save(filename)
defer os.Remove(filename)
if err != nil {
fmt.Println(err)
c.jsonResult(enums.JRCodeFailed, "文件打开失败!", 0)
}
//读取execl文件to浏览器下载
servername := "users_" + time.Now().Format("20060102150405") + ".xlsx"
c.Ctx.Output.Download(filename, servername)
}
//导入
func (c *UserManageController) Import() {
if c.Ctx.Request.Method == "POST" {
c.ImportSave()
}
c.setTpl("usermanage/import.html", "shared/layout_pullbox.html")
c.LayoutSections = make(map[string]string)
c.LayoutSections["footerjs"] = "usermanage/import_footerjs.html"
}
func (c *UserManageController) ImportSave() {
//获取上传url
str := c.GetString("Url")
fmt.Println("Url:", str)
//read excel
excelFileName := "static/excel/" + str
//判断文件后缀 .xlsx
//fmt.Println("文件后缀:",path.Ext(excelFileName))
if path.Ext(excelFileName) != ".xlsx" {
c.jsonResult(enums.JRCodeFailed, "文件类型错误,请选择 *.xlsx 文件 !", 0)
}
xlFile, err := xlsx.OpenFile(excelFileName)
//defer os.Remove(excelFileName)
if err != nil {
fmt.Printf("open failed: %s\n", err)
c.jsonResult(enums.JRCodeFailed, "文件打开失败!", 0)
}
qs := orm.NewOrm().QueryTable(models.UserManageTBName())
i, _ := qs.PrepareInsert()
defer i.Close();
os.Remove(excelFileName)
succNum := 0
failNum := 0
fail := ""
for _, sheet := range xlFile.Sheets {
fmt.Printf("Sheet Name: %s\n", sheet.Name)
if sheet.Name == "sheet" {
fmt.Printf("Sheet len: %v\n", len(sheet.Rows))
for j := 1; j < len(sheet.Rows); j++ { //忽略标题行
fmt.Println("row Cells :", sheet.Rows[j].Cells)
//fmt.Println("row Cells 0 :", sheet.Rows[j].Cells[0])
data := models.UserManage{}
data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
data.Pin = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
data.Photo = "/static/users/images/" + fmt.Sprintf("%s", sheet.Rows[j].Cells[2])
data.Cardid = fmt.Sprintf("%s", sheet.Rows[j].Cells[3])
data.Creator = &c.curUser
if data.Name == "" || data.Pin == "" {
failNum += 1
fail += "姓名或人员编号不能为空."
continue
} else {
userData := models.UserManage{Pin: data.Pin}
hasUser := orm.NewOrm().Read(&userData, "pin")
if hasUser != nil {
/******/
succNum += 1
//fmt.Println("查询不到")//插入表
id, _ := i.Insert(&data)
fmt.Printf("id :%v \n", id)
} else {
failNum += 1
fail += "人员编号:" + data.Pin + "已存在."
}
}
}
} else {
c.jsonResult(enums.JRCodeFailed, "excel工作表须为 'sheet'", 0)
}
}
if succNum > 0 {
c.jsonResult(enums.JRCodeSucc, "成功数量:"+strconv.Itoa(succNum)+",失败数量:"+strconv.Itoa(failNum)+";原因:"+fail, 0)
} else {
c.jsonResult(enums.JRCodeFailed, "失败数量:"+strconv.Itoa(failNum)+";原因:"+fail, 0)
}
}