From d7a49bf56d0e5f26d59445d3092f04396d491bdf Mon Sep 17 00:00:00 2001 From: zhoupengwei Date: Tue, 10 May 2022 19:45:00 +0800 Subject: [PATCH] =?UTF-8?q?vip=E5=BD=93=E6=97=A5=E8=B4=AD=E4=B9=B0?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=AF=BC=E5=87=BAexcel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/app/func.go | 44 ++++++++++++++++++++++++++++++++++++++++ example/app/vip_order.go | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 example/app/func.go diff --git a/example/app/func.go b/example/app/func.go new file mode 100644 index 0000000..25d08f2 --- /dev/null +++ b/example/app/func.go @@ -0,0 +1,44 @@ +package app + +import ( + "bytes" + "code.hoteas.com/golang/hotime/common" + "fmt" + "github.com/tealeg/xlsx" + "net/http" + "time" +) + +func DataToExcel(w http.ResponseWriter, r *http.Request, titleList []string, dataList []common.Map, fileName string) { + // 生成一个新的文件 + file := xlsx.NewFile() + // 添加sheet页 + sheet, _ := file.AddSheet("Sheet1") + // 插入表头 + titleRow := sheet.AddRow() + for _, v := range titleList { + cell := titleRow.AddCell() + cell.Value = v + cell.GetStyle().Font.Color = "00FF0000" + } + for _, v1 := range dataList { + row := sheet.AddRow() + // 插入内容 + for _, v := range titleList { + cell := row.AddCell() + cell.SetValue(v1.Get(v)) + } + + } + file.Save("D:\\temp\\vip_excel") + + fileName = fmt.Sprintf("%s.xlsx", fileName) + //_ = file.Save(fileName) + w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName)) + w.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") + + var buffer bytes.Buffer + _ = file.Write(&buffer) + content := bytes.NewReader(buffer.Bytes()) + http.ServeContent(w, r, fileName, time.Now(), content) +} diff --git a/example/app/vip_order.go b/example/app/vip_order.go index c41a5fb..163eccd 100644 --- a/example/app/vip_order.go +++ b/example/app/vip_order.go @@ -157,4 +157,47 @@ var VipOrderCtr = Ctr{ return }, + "export": func(that *Context) { + if that.Session("user_id").Data == nil { + that.Display(2, "没有登录") + return + } + + buy_date := ObjToStr(that.Req.FormValue("buy_date")) + if buy_date == "" { + that.Display(3, "请求参数异常") + return + } + + data := that.Db.Query("SELECT vo.sn 订单号, vo.`name` 订单名, vo.user_id 购买用户ID,usr.`name` 购买用户名, usr.phone 购买用户电话, cp.id 企业ID, cp.`name` 企业名,\nvo.expiration_time 过期时间, vo.old_expiration_time 订购前到期时间, vo.amount `订单金额_单位(分)`,\nvo.salesman_id 业务员ID,sm.`name` 业务员名, sm.phone 业务员电话, pv.`name` 服务商名 \nFROM vip_order vo\nLEFT JOIN salesman sm ON sm.id = vo.salesman_id\nLEFT JOIN provider pv ON pv.id = vo.provider_id\nLEFT JOIN `user` usr ON usr.id = vo.user_id\nLEFT JOIN company cp ON cp.id = vo.company_id\nWHERE vo.`status` = 1 AND vo.create_time >= '" + buy_date + "' \nORDER BY vo.create_time") + + if len(data) == 0 { + that.Display(0, "今日没有vip购买信息数据") + return + } + + var titleList []string + if data != nil { + m := data[0] + for k, _ := range m { + titleList = append(titleList, k) + } + } + + var dataList []Map + for _, v := range data { + dataList = append(dataList, v) + } + + //打印最后一次sql语句 + //that.Db.LastQuery + //请求的数据 + //that.Db.LastData + //that.Db.LastErr + //appIns.Db.Select("company",common.Map{"[<>]user":"company.id=user.company_id"},"company.id as id",common.Map{"AND":common.Map{"id[!]":nil},"ORDER":"id DESC"}) + //appIns.Db.Query("select * from user where id = ? and name = ?",common.Slice{1,"nn"}) + + //titleList:= []string{"aaaa", "vvvvv", "dddd", "eeeee", "gfgggg"} + DataToExcel(that.Resp, that.Req, titleList, dataList, buy_date+"vip购买信息分析数据") + }, }