From 5f9860ccd434e5fd95fbfbd1ddd0b987a33fe451 Mon Sep 17 00:00:00 2001 From: wangwei Date: Fri, 13 Aug 2021 11:46:47 +0800 Subject: [PATCH] add xlsx --- controller/register.go | 1 + controller/usercontroller.go | 29 +++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ handler/userhandler.go | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+) diff --git a/controller/register.go b/controller/register.go index 4883f51..c228122 100644 --- a/controller/register.go +++ b/controller/register.go @@ -20,5 +20,6 @@ func Register(app *iris.Application) { { userController.Get("/getUserInfo", GetUserInfo) userController.Get("/addUser", addUser) + userController.Get("/upload", upload) } } diff --git a/controller/usercontroller.go b/controller/usercontroller.go index 5963fd8..76f4a0f 100644 --- a/controller/usercontroller.go +++ b/controller/usercontroller.go @@ -28,3 +28,32 @@ func addUser(ctx iris.Context) { userName := ctx.URLParam("userName") handler.AddUserInfo(userId, userName) } + +type St struct { + Name string + Age int + Addr string +} + +func upload(ctx iris.Context) { + title := []string{"name", "age", "addr"} + // data := make([]interface{}, 0) + // slice := make([]St, 0) + // slice = append(slice, St{ + // Name: "ww", + // Age: 12, + // Addr: "上海", + // }) + b := St{ + Name: "ww", + Age: 12, + Addr: "上海", + } + // var data []interface{} = make([]interface{}, len(slice)) + // for i, d := range slice { + // data[i] = &d + // } + data := make([]interface{}, 0) + data = append(data, &b) + handler.DataToExcel(ctx.ResponseWriter(), ctx.Request(), title, data, "123") +} diff --git a/go.mod b/go.mod index cd22b66..c2bf21e 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/smartystreets/goconvey v1.6.4 // indirect github.com/spf13/viper v1.8.1 // indirect github.com/stretchr/testify v1.7.0 // indirect + github.com/tealeg/xlsx v1.0.5 // indirect github.com/valyala/fasthttp v1.28.0 // indirect github.com/xeipuuv/gojsonschema v1.2.0 // indirect github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect diff --git a/go.sum b/go.sum index 2d87503..3e52db0 100644 --- a/go.sum +++ b/go.sum @@ -350,6 +350,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= +github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= diff --git a/handler/userhandler.go b/handler/userhandler.go index b6ed6d0..063e046 100644 --- a/handler/userhandler.go +++ b/handler/userhandler.go @@ -12,8 +12,14 @@ package handler import ( + "bytes" + "fmt" data "go-iris-1/Data" dataentity "go-iris-1/DataEntity" + "net/http" + "time" + + "github.com/tealeg/xlsx" ) func GetUserInfo(userId string) *dataentity.Tbl_user { @@ -24,3 +30,32 @@ func GetUserInfo(userId string) *dataentity.Tbl_user { func AddUserInfo(userId string, userName string) { data.AddUserInfo(userId, userName) } + +// DataToExcel 数据导出excel, dataList里面的对象为指针 +func DataToExcel(w http.ResponseWriter, r *http.Request, titleList []string, dataList []interface{}, 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 _, v := range dataList { + row := sheet.AddRow() + row.WriteStruct(v, -1) + } + 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) +} -- Gitee