订单下载。jd下单。。
CI / changes (push) Successful in 37s
CI / ad (push) Successful in 29s
CI / admin (push) Successful in 17s
CI / bff (push) Successful in 13s
CI / chore (push) Successful in 15s
CI / equipment (push) Successful in 14s
CI / express (push) Successful in 14s
CI / product (push) Successful in 13s
CI / record (push) Successful in 13s
CI / sale (push) Successful in 13s
CI / task (push) Successful in 13s
CI / user (push) Successful in 14s
CI / wecom (push) Successful in 13s
CI / changes (push) Successful in 37s
CI / ad (push) Successful in 29s
CI / admin (push) Successful in 17s
CI / bff (push) Successful in 13s
CI / chore (push) Successful in 15s
CI / equipment (push) Successful in 14s
CI / express (push) Successful in 14s
CI / product (push) Successful in 13s
CI / record (push) Successful in 13s
CI / sale (push) Successful in 13s
CI / task (push) Successful in 13s
CI / user (push) Successful in 14s
CI / wecom (push) Successful in 13s
This commit is contained in:
+10
-15
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
@@ -16,7 +17,6 @@ import (
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
@@ -348,7 +348,8 @@ type ExcelColumn struct {
|
||||
IsImage bool //是否是图片
|
||||
}
|
||||
|
||||
func ExcelExport(c *gin.Context, fileName string, columns []ExcelColumn, dataList interface{}) error {
|
||||
// 【改造后】只生成excel,返回二进制,不处理http
|
||||
func ExcelExport(columns []ExcelColumn, dataList interface{}) ([]byte, error) {
|
||||
f := excelize.NewFile()
|
||||
sheet := "Sheet1"
|
||||
f.SetSheetName("Sheet1", sheet)
|
||||
@@ -369,7 +370,7 @@ func ExcelExport(c *gin.Context, fileName string, columns []ExcelColumn, dataLis
|
||||
|
||||
items := reflect.ValueOf(dataList)
|
||||
if items.Kind() != reflect.Slice {
|
||||
return f.Write(c.Writer)
|
||||
return nil, errors.New("data must be slice")
|
||||
}
|
||||
|
||||
// 遍历行
|
||||
@@ -392,23 +393,16 @@ func ExcelExport(c *gin.Context, fileName string, columns []ExcelColumn, dataLis
|
||||
cellName, _ := excelize.ColumnNumberToName(colIdx + 1)
|
||||
cellPos := cellName + strconv.Itoa(rowNum)
|
||||
|
||||
// ======================================
|
||||
// 图片URL → 改为【点击查看图片】链接
|
||||
// ======================================
|
||||
if col.IsImage {
|
||||
imgURL := toStr(fieldVal.Interface())
|
||||
if imgURL != "" {
|
||||
// 设置超链接(显示文字:点击查看图片,真正链接:imgURL)
|
||||
_ = f.SetCellHyperLink(sheet, cellPos, imgURL, "External")
|
||||
// 单元格显示文字
|
||||
_ = f.SetCellValue(sheet, cellPos, "点击查看")
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// ======================================
|
||||
// 文本/数组 自动换行
|
||||
// ======================================
|
||||
var cellValue interface{}
|
||||
kind := fieldVal.Kind()
|
||||
if kind == reflect.Slice || kind == reflect.Array {
|
||||
@@ -424,7 +418,6 @@ func ExcelExport(c *gin.Context, fileName string, columns []ExcelColumn, dataLis
|
||||
_ = f.SetCellValue(sheet, cellPos, cellValue)
|
||||
_ = f.SetCellStyle(sheet, cellPos, cellPos, wrapStyle)
|
||||
}
|
||||
|
||||
_ = f.SetRowHeight(sheet, rowNum, 30)
|
||||
}
|
||||
|
||||
@@ -437,10 +430,12 @@ func ExcelExport(c *gin.Context, fileName string, columns []ExcelColumn, dataLis
|
||||
_ = f.SetColWidth(sheet, colName, colName, col.Width)
|
||||
}
|
||||
|
||||
// 下载
|
||||
c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||||
c.Header("Content-Disposition", "attachment; filename="+fileName+".xlsx")
|
||||
return f.Write(c.Writer)
|
||||
// 写入buffer,不要直接写http
|
||||
buf := new(bytes.Buffer)
|
||||
if err := f.Write(buf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// 嵌套字段取值
|
||||
|
||||
Reference in New Issue
Block a user