由网络副手--寻路人于2022.08.26 16:09:00发布在Go语言 GO-XLS文件导出模块功能编写 阅读1386 评论0 喜欢0 ##一、前言 比较简单的xls 代码导出功能记录下 ##二、项目树 ``` ├── controller | ├── music_sale_controller.go ├── service | ├── music_sale_service.go | ├── export_excel_service.go ``` ### 2.1 music_sale_controller.go ``` func MusicSaleList(c *gin.Context) { var ( req typespec.MusicSaleListRequest resp typespec.MusicSaleListResponse ) page := NewPage(c) req.PageSize = page.PageSize req.Offset = page.Offset if err := c.ShouldBind(&req); err != nil { c.JSON(http.StatusBadRequest, WriteResponse(BadRequest, err, nil)) return } err := service.MusicSaleList(c, &req, &resp) if err != nil { c.JSON(http.StatusInternalServerError, WriteResponse(LoginFail, err)) return } c.JSON(http.StatusOK, WriteResponse(Success, nil, &resp)) } ``` ###2.2 music_sale_service.go ``` func MusicSaleList(c *gin.Context, res *typespec.MusicSaleListRequest, resp *typespec.MusicSaleListResponse) error { totalNum := &typespec.MusicSaleExportTotal{ SalePrice: float64(salePriceTotal) / 100, ServicePrice: ServicePriceTotal / 100, SharingPrice: SharingPriceTotal / 100, } var excelData []interface{} for _, excelDataInfo := range resp.List { excelData = append(excelData, &typespec.MusicSaleExport{ OrderNo: excelDataInfo.OrderNo, BuyUser: excelDataInfo.BuyUser, BuyUserName: excelDataInfo.BuyUserName, BuyPhone: excelDataInfo.BuyPhone, BuyAddress: excelDataInfo.BuyAddress, SellUser: excelDataInfo.SellUser, SellUserName: excelDataInfo.SellUserName, SellPhone: excelDataInfo.SellPhone, SellAddress: excelDataInfo.SellAddress, MusicName: excelDataInfo.MusicName, CodeNum: excelDataInfo.CodeNum, SalePrice: float64(excelDataInfo.SalePrice) / 100, ServicePrice: excelDataInfo.ServicePrice / 100, SharingPrice: excelDataInfo.SharingPrice / 100, PaymentValue: models.SaleOrderPayment[excelDataInfo.Payment], CreatedAt: excelDataInfo.CreatedAt, DealTimeAt: excelDataInfo.DealTimeAt, StatusValue: excelDataInfo.StatusValue, }) } excelData = append(excelData, totalNum) content := ToExcel([]string{`订单编号`, `买方用户ID`, `买方姓名`, `买方手机号`, `买方区块链地址`, `卖方用户ID`, `卖方姓名`, `卖方手机号`, `卖方区块链地址`, `藏品名称`, `藏品编号`, `售卖价格(元)`, `服务费(元)`, `音乐人分成(元)`, `支付类型`, `上架时间`, `成交时间`, `状态`}, excelData) ResponseXls(c, content, "集市交易数据"+startTimeTitle+"-"+endTimeTitle) return nil } ``` ### 2.2.3 export_excel_service.go ``` package service import ( "bytes" "fmt" "github.com/gin-gonic/gin" "github.com/tealeg/xlsx" "io" "net/http" "time" ) // ToExcel 生成io.ReadSeeker 参数 titleList 为Excel表头,dataList 为数据 func ToExcel(titleList []string, dataList []interface{}) (content io.ReadSeeker) { // 生成一个新的文件 file := xlsx.NewFile() // 添加sheet页 sheet, _ := file.AddSheet("Sheet1") // 插入表头 titleRow := sheet.AddRow() for _, v := range titleList { cell := titleRow.AddCell() cell.Value = v } // 插入内容 for _, v := range dataList { row := sheet.AddRow() row.WriteStruct(v, 20) } var buffer bytes.Buffer _ = file.Write(&buffer) content = bytes.NewReader(buffer.Bytes()) return } //ResponseXls 参数 content 为上面生成的io.ReadSeeker, fileTag 为返回前端的文件名 func ResponseXls(c *gin.Context, content io.ReadSeeker, fileTag string) { fileName := fmt.Sprintf("%s.xlsx", fileTag) //fileName := fmt.Sprintf("%s%s%s.xlsx", time.Now(), `-`, fileTag) c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName)) c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content) } ``` 赞 0 分享 赏 您可以选择一种方式赞助本站 支付宝扫码赞助 BraveDu 署名: 网络副手~寻路人