-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (67 loc) · 1.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"ExcelText/img"
"bytes"
"fmt"
"github.com/xuri/excelize/v2"
"image"
_ "image/color"
_ "image/jpeg"
"io/ioutil"
"strconv"
)
func main() {
//这里填写你的图片名字
//目前只支持500*500像素的图片
var imgName = "HaiLunAndPBoos.jpg"
exclFile := excelize.NewFile()
sheet := "sheet2"
exclFile.NewSheet(sheet)
err := exclFile.SetColWidth(sheet,"A","XFD",2.2)
if err != nil {
fmt.Println("@",err)
return
}
imgfile,err := ioutil.ReadFile(imgName)
if err != nil {
fmt.Println("@",err)
return
}
byteImgFile := bytes.NewBuffer(imgfile)
imgD ,_,err := image.Decode(byteImgFile)
if err != nil {
fmt.Println("@",err)
return
}
bounds := imgD.Bounds()
image.NewRGBA(bounds)
dx := bounds.Dx()
dy := bounds.Dy()
for i := 1; i <= dx; i++ {
for j := 1; j <= dy; j++ {
colorRGB := imgD.At(i,j)
r,g,b,_ := colorRGB.RGBA()
r_uint8 := uint8(r >>8)
g_uint8 := uint8(g >>8)
b_uint8 := uint8(b >>8)
style ,err := exclFile.NewStyle(&excelize.Style{
Fill: excelize.Fill{
Type: "pattern",
Color:[]string{"#"+img.ChangToHEX(int(r_uint8),int(g_uint8),int(b_uint8))},
Pattern: 1,
},
})
if err != nil {
fmt.Println(err)
return
}
strj :=strconv.Itoa(j)
exclFile.SetCellStyle(sheet,img.ChangToLetter(i)+strj,img.ChangToLetter(i)+strj,style)
fmt.Println(img.ChangToLetter(i)+strj)
}
}
err = exclFile.SaveAs("img.xlsx")
if err != nil {
fmt.Println(err)
}
}