98 lines
3.3 KiB
Go
98 lines
3.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/360EntSecGroup-Skylar/excelize"
|
|
"github.com/exhibition-main/internal/model"
|
|
"github.com/exhibition-main/pkg/utils"
|
|
"image"
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// DealListExcelImg 处理Excel图片
|
|
func DealListExcelImg(filePath string) (err error) {
|
|
f, err := excelize.OpenFile(filePath)
|
|
filename := filepath.Base(filePath)
|
|
var cellHeight, cellWidth float64 = 150, 40
|
|
|
|
//minColumn := int([]byte("A")[0])
|
|
// string(rune(i))
|
|
rows := f.GetRows("Sheet1")
|
|
var columnFullName string
|
|
var lineNo, columnNo int32
|
|
var cellNo string
|
|
for k, row := range rows {
|
|
lineNo = int32(k + 1)
|
|
if lineNo != 1 {
|
|
f.SetRowHeight("Sheet1", int(lineNo), cellHeight)
|
|
}
|
|
var firstCell int32
|
|
var suffix string
|
|
var columnName, firstCellName string
|
|
columnFullName = ""
|
|
for kk, colCell := range row {
|
|
cellNo = ""
|
|
columnNo = int32(int([]byte("A")[0]) + kk%26)
|
|
columnName = string(columnNo)
|
|
if kk >= 26 {
|
|
firstCell = int32(int([]byte("A")[0]) + (int(math.Floor(float64(kk)/float64(26))) - 1))
|
|
firstCellName = string(firstCell)
|
|
}
|
|
columnFullName = fmt.Sprintf("%s%s", firstCellName, columnName)
|
|
if strings.Contains(colCell, "cdns.fontree.cn") {
|
|
suffix = colCell[strings.LastIndex(colCell, "."):]
|
|
if strings.Contains(".jpg,.png,.gif,.bmp,.jpeg,", suffix) {
|
|
cellNo = fmt.Sprintf("%s%v", columnFullName, lineNo)
|
|
// 需要转化图片 ,先下载图片
|
|
fullPath, _ := utils.SaveUrlFileDisk(colCell, model.TmpArtworkDir, "")
|
|
reader, _ := os.Open(fullPath)
|
|
defer reader.Close()
|
|
im, _, _ := image.DecodeConfig(reader)
|
|
rateX := cellWidth * 2.0 / float64(im.Width)
|
|
rateY := cellHeight * 1.33 / float64(im.Height)
|
|
f.SetCellValue("Sheet1", cellNo, "")
|
|
err = f.AddPicture("Sheet1", cellNo, fullPath, fmt.Sprintf(`{"x_scale": %v, "y_scale": %v, "hyperlink": "%s", "hyperlink_type": "External"}`, rateX, rateY, colCell))
|
|
_ = os.Remove(fullPath)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
f.SetColWidth("Sheet1", "A", columnFullName, cellWidth)
|
|
//err = f.AddPicture("Sheet1", "A2", "./runtime/small.png", `{"x_scale": 1, "y_scale": 1, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`)
|
|
//err = f.AddPicture("Sheet1", "A2", "./runtime/small.png", `{"x_with": 30, "y_hright": 50, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`)
|
|
|
|
//err = f.AddPicture("Sheet1", "H2", "./runtime/field_3.jpg", `{"x_offset": 15, "y_offset": 10, "hyperlink": "https://www.baidu.com", "hyperlink_type": "External", "print_obj": true, "lock_aspect_ratio": false, "locked": false, "positioning": "oneCell"}`)
|
|
|
|
//if err := f.AddPicture("Sheet1", "D2", "./runtime/field_3.jpg",
|
|
// `{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
|
|
// fmt.Println(err)
|
|
//}
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
err = f.SaveAs(fmt.Sprintf("%s%s", model.MediaPath, filename))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
// Insert a picture offset in the cell with printing support.
|
|
//err = xlsx.AddPicture("Sheet1", "C2", "./runtime/field_3.jpg", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`)
|
|
//if err != nil {
|
|
// fmt.Println(err)
|
|
//}
|
|
// Save the xlsx file with the origin path.
|
|
//err = xlsx.Save()
|
|
//if err != nil {
|
|
// fmt.Println(err)
|
|
//}
|
|
//ResponseQuickMsg(c, e.Ok, "成功", nil)
|
|
return
|
|
}
|