Go語(yǔ)言excelize包-06-樣式設(shè)置(樣式設(shè)置、區(qū)間使用樣式、行使用樣式、列使用樣式)

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)型

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

注意:
diagonalDowndiagonalUp同時(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 不生效。

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 (#,##0_);(#,##0)
6 (#,##0_);[Red](#,##0)
7 (#,##0.00_);(#,##0.00)
8 (#,##0.00_);[Red](#,##0.00)
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 (* #,##0_);_(* (#,##0);($* "-");(@_)
43 (* #,##0.00);(* (#,##0.00);(* "-"??);(@_)
44 (* #,##0.00_);_(* (#,##0.00);($* "-"??);(@_)
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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 使用首先需要了解他的工作原理 1.POI結(jié)構(gòu)與常用類(lèi) (1)創(chuàng)建Workbook和Sheet (2)創(chuàng)建單元格 (...
    長(zhǎng)城ol閱讀 8,745評(píng)論 2 25
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,662評(píng)論 19 139
  • 轉(zhuǎn)至:https://blog.51cto.com/dbaspace/2050922 DAX函數(shù)大全 針對(duì)Powe...
    夏沬沬閱讀 26,452評(píng)論 0 7
  • 第六章:Layouts layout 是 logback 的組件,負(fù)責(zé)將日志事件轉(zhuǎn)換為字符串。Layout 接口中...
    ChinaXieShuai閱讀 1,594評(píng)論 0 0
  • 什么是Vue.js Vue.js是目前最火的一個(gè)前端框架,React是最流行的一個(gè)前端框架,(React除了開(kāi)發(fā)網(wǎng)...
    EEEEsun閱讀 736評(píng)論 0 1

友情鏈接更多精彩內(nèi)容