1. 樣式設(shè)置
1.1 創(chuàng)建樣式
func (f *File) NewStyle(style interface{}) (int, error)
1.2 Style 結(jié)構(gòu)體
type Style struct {
Border []Border `json:"border"`
Fill Fill `json:"fill"`
Font *Font `json:"font"`
Alignment *Alignment `json:"alignment"`
Protection *Protection `json:"protection"`
NumFmt int `json:"number_format"`
DecimalPlaces int `json:"decimal_places"`
CustomNumFmt *string `json:"custom_number_format"`
Lang string `json:"lang"`
NegRed bool `json:"negred"`
}
- 成員說(shuō)明
-
Border:邊界 -
Fill:填充色 -
Font:字體 -
Alignment:對(duì)齊 -
Protection: ? -
NumFmt:自定義格式 -
DecimalPlaces:小數(shù)點(diǎn)位置 -
CustomNumFmt:自定義數(shù)字格式 -
Lang: 誰(shuí)的長(zhǎng)度 -
NegRed:是否粗體?
-
幾個(gè)常用成員(如
Border、Fill等)使用的結(jié)構(gòu)體我們接下來(lái)將做說(shuō)明:
1.2.1 Border結(jié)構(gòu)體(邊框設(shè)置)
結(jié)構(gòu)體語(yǔ)法
type Border struct {
Type string `json:"type"`
Color string `json:"color"`
Style int `json:"style"`
}
- 成員說(shuō)明
- Type:邊線方向
- left
- right
- top
- bottom
- diagonalDown:左上到右下
- diagonalUP:左下到右上
- Color:顏色
- Style:邊線類(lèi)型
- Type:邊線方向
Style邊線類(lèi)型效果如下:

image.png
完整示例
- 代碼
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
styleId, err := f.NewStyle(&excelize.Style{
Border: []excelize.Border{
{Type: "left", Color: "000000", Style: 1},
{Type: "top", Color: "000000", Style: 2},
{Type: "bottom", Color: "000000", Style: 3},
{Type: "right", Color: "000000", Style: 4},
{Type: "diagonalDown", Color: "000000", Style: 5},
{Type: "diagonalUp", Color: "A020F0", Style: 6},
},
})
if err != nil {
fmt.Println(err)
}
err = f.SetCellStyle("Sheet1", "B4", "D2", styleId)
if err = f.SaveAs("sanGuo.xlsx"); err != nil {
fmt.Println(err)
}
}
- 結(jié)果顯示

image.png
注意:
diagonalDown和diagonalUp同時(shí)設(shè)置,如果二者樣式不同,則diagonalDown會(huì)被diagonalUp的樣式覆蓋。
1.2.2 Fill結(jié)構(gòu)體(填充設(shè)置)
結(jié)構(gòu)體語(yǔ)法
type Fill struct {
Type string `json:"type"`
Pattern int `json:"pattern"`
Color []string `json:"color"`
Shading int `json:"shading"`
}
- 說(shuō)明:
- Type
-
gradient:漸變 -
pattern:填充圖
-
- Shading(Type為
gradient時(shí)生效)- 1:橫向填充
- 2:縱向填充
- 3:對(duì)角線向下填充
- 4:對(duì)角線向上填充
- 5:從內(nèi)向外填充
- Pattern(Type為
pattern時(shí)生效)- 值從1~18(圖片“Pattern值”)。
- 1 表示純色填充
- Color
- Type為
gradient時(shí),Color 有兩個(gè)值,Pattern不生效 - 切片只有一個(gè)成員時(shí),Shading 不生效。
- Type為
- Type
Pattern值:

image.png
完整示例(漸變填充)
- 代碼
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
styleId, err := f.NewStyle(&excelize.Style{
Border: []excelize.Border{
{Type: "left", Color: "000000", Style: 2},
{Type: "top", Color: "000000", Style: 2},
{Type: "bottom", Color: "000000", Style: 2},
{Type: "right", Color: "000000", Style: 2},
},
Fill: excelize.Fill{
Type: "gradient",
Color: []string{"FFFF00", "00FF00"},
Shading: 1,
},
})
if err != nil {
fmt.Println(err)
}
err = f.SetCellStyle("Sheet1", "B4", "D2", styleId)
if err = f.SaveAs("sanGuo.xlsx"); err != nil {
fmt.Println(err)
}
}
- 結(jié)果顯示

image.png
示例(純色填充)
style, err := f.NewStyle(&excelize.Style{
Fill: excelize.Fill{Type: "pattern", Color: []string{"FF0000"}, Pattern: 1},
})
1.2.3 Font結(jié)構(gòu)體(字體設(shè)置)
結(jié)構(gòu)體語(yǔ)法
type Font struct {
Bold bool `json:"bold"`
Italic bool `json:"italic"`
Underline string `json:"underline"`
Family string `json:"family"`
Size float64 `json:"size"`
Strike bool `json:"strike"`
Color string `json:"color"`
VertAlign string `json:"vertAlign"`
}
- 說(shuō)明:
-
Bold:是否粗體 -
Italic:是否斜體 -
Underline: 下劃線- single :?jiǎn)尉€
- double:雙線
-
Family:字體樣式 -
Size:字體大小 -
Color:字體顏色
-
完整示例
- 代碼
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
styleId, err := f.NewStyle(&excelize.Style{
Font: &excelize.Font{
Bold: true,
Italic: true,
Family: "Times New Roman",
Size: 36,
Color: "微軟雅黑",
},
})
if err != nil {
fmt.Println(err)
}
f.SetCellStyle("Sheet1", "B4", "B4", styleId)
f.SetCellValue("Sheet1","B4","LiuBei")
if err = f.SaveAs("sanGuo.xlsx"); err != nil {
fmt.Println(err)
}
}
- 結(jié)果顯示

image.png
1.2.4 Alignment結(jié)構(gòu)體(對(duì)齊方式)
結(jié)構(gòu)體語(yǔ)法
type Alignment struct {
Horizontal string `json:"horizontal"`
Indent int `json:"indent"`
JustifyLastLine bool `json:"justify_last_line"`
ReadingOrder uint64 `json:"reading_order"`
RelativeIndent int `json:"relative_indent"`
ShrinkToFit bool `json:"shrink_to_fit"`
TextRotation int `json:"text_rotation"`
Vertical string `json:"vertical"`
WrapText bool `json:"wrap_text"`
}
- 說(shuō)明
-
Horizontal:水平對(duì)齊- right
- left
- center
-
Indent:縮進(jìn) -
JustifyLastLine:兩端對(duì)齊 -
ReadingOrder:文字方向 -
RelativeIndent:相對(duì)縮進(jìn) -
ShrinkToFit:縮小字體 -
TextRotation:文字旋轉(zhuǎn) -
Vertical:垂直對(duì)齊- top
- bottom
- center
-
WrapText:自動(dòng)換行
-
完整示例
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
styleId, err := f.NewStyle(&excelize.Style{
Alignment: &excelize.Alignment{
Horizontal: "center",
Indent: 1,
JustifyLastLine: true,
ReadingOrder: 2,
RelativeIndent: 1,
ShrinkToFit: true,
TextRotation: 30,
Vertical: "top",
WrapText: true,
},
})
if err != nil {
fmt.Println(err)
}
f.SetCellStyle("Sheet1", "B4", "B4", styleId)
f.SetCellValue("Sheet1","B4","LiuBei")
if err = f.SaveAs("sanGuo.xlsx"); err != nil {
fmt.Println(err)
}
}
結(jié)果顯示

image.png
1.2.4 NumFmt編號(hào)(自定義格式)
參數(shù)
| 索引 | 類(lèi)型 |
|---|---|
| 27 | yyyy"年"m"月" |
| 28 | m"月"d"日" |
| 29 | m"月"d"日" |
| 30 | m-d-yy |
| 31 | yyyy"年"m"月"d"日" |
| 32 | h"時(shí)"mm"分" |
| 33 | h"時(shí)"mm"分"ss"秒" |
| 34 | 上午/下午 h"時(shí)"mm"分" |
| 35 | 上午/下午 h"時(shí)"mm"分"ss"秒 |
| 36 | yyyy"年"m"月 |
| 50 | yyyy"年"m"月 |
| 51 | m"月"d"日 |
| 52 | yyyy"年"m"月 |
| 53 | m"月"d"日 |
| 54 | m"月"d"日 |
| 55 | 上午/下午 h"時(shí)"mm"分 |
| 56 | 上午/下午 h"時(shí)"mm"分"ss"秒 |
| 57 | yyyy"年"m"月 |
| 58 | m"月"d"日" |
完整示例
import (
"fmt"
"github.com/xuri/excelize/v2"
"time"
)
func main() {
f := excelize.NewFile()
numFmt := "yyyy\"年\"m\"月\"d\"日\(chéng)""
styleId, err := f.NewStyle(&excelize.Style{
CustomNumFmt: &numFmt,
})
if err != nil {
fmt.Println(err)
}
f.SetCellStyle("Sheet1", "B4", "B4", styleId)
f.SetCellValue("Sheet1","B4",time.Now())
if err = f.SaveAs("sanGuo.xlsx"); err != nil {
fmt.Println(err)
}
}
- 效果

image.png
1.2.5 CustomNumFmt編號(hào)(自定義數(shù)字)
參數(shù)
| 索引 | 類(lèi)型 |
|---|---|
| 0 | General |
| 1 | 0 |
| 2 | 0.00 |
| 3 | #,##0 |
| 4 | #,##0.00 |
| 5 | ( |
| 6 | ( |
| 7 | ( |
| 8 | ( |
| 9 | 0% |
| 10 | 0.00% |
| 11 | 0.00E+00 |
| 12 | # ?/? |
| 13 | # ??/?? |
| 14 | m/d/yy |
| 15 | d-mmm-yy |
| 16 | d-mmm |
| 17 | mmm-yy |
| 18 | h:mm AM/PM |
| 19 | h:mm:ss AM/PM |
| 20 | h:mm |
| 21 | h:mm:ss |
| 22 | m/d/yy h:mm |
| .. | ... |
| 37 | (#,##0_) ; (#,##0) |
| 38 | (#,##0_);Red |
| 39 | (#,##0.00_); (#,##0.00) |
| 40 | (#,##0.00_);Red |
| 41 | (* #,##0);(* (#,##0);(* "-");(@_) |
| 42 |
( |
| 43 | (* #,##0.00);(* (#,##0.00);(* "-"??);(@_) |
| 44 |
( |
| 45 | mm:ss |
| 46 | [h]:mm:ss |
| 47 | mm:ss.0 |
| 48 | ##0.0E+0 |
| 49 | @ |
完整示例(指定小數(shù)位)
顯示為五位小數(shù)
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
customNumFmt := "0.00000"
styleId, err := f.NewStyle(&excelize.Style{
CustomNumFmt: &customNumFmt,
})
if err != nil {
fmt.Println(err)
}
f.SetCellStyle("Sheet1", "B4", "B4", styleId)
f.SetCellValue("Sheet1","B4",1)
if err = f.SaveAs("sanGuo.xlsx"); err != nil {
fmt.Println(err)
}
}
- 效果

image.png
完整示例(顯示節(jié))
- 代碼
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
customNumFmt := "#,##0"
styleId, err := f.NewStyle(&excelize.Style{
CustomNumFmt: &customNumFmt,
})
if err != nil {
fmt.Println(err)
}
f.SetCellStyle("Sheet1", "B4", "B4", styleId)
f.SetCellValue("Sheet1","B4",12345)
if err = f.SaveAs("sanGuo.xlsx"); err != nil {
fmt.Println(err)
}
}
- 效果

image.png
2. 樣式使用
2.1 單元格使用樣式
- 語(yǔ)法
func (f *File) SetCellStyle(sheet string, hCell string, vCell string, styleID int) error
2.2 列使用樣式
- 語(yǔ)法
func (f *File) SetColStyle(sheet, columns string, styleID int) error
- 語(yǔ)法示例
err = f.SetColStyle("Sheet1", "H", style)
err = f.SetColStyle("Sheet1", "C:F", style)
2.3 行使用樣式
- 語(yǔ)法
func (f *File) SetRowStyle(sheet string, start int, end int, styleID int) error
- 語(yǔ)法示例
err = f.SetRowStyle("Sheet1", 1,3,style)
