Go语言excelize包-06-样式设置(样式设置、区间使用样式、行使用样式、列使用样式)

1. 样式设置

1.1 创建样式

func (f *File) NewStyle(style interface{}) (int, error)

1.2 Style 结构体

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"`
}
  • 成员说明
    • Border :边界
    • Fill :填充色
    • Font :字体
    • Alignment :对齐
    • Protection : ?
    • NumFmt :自定义格式
    • DecimalPlaces :小数点位置
    • CustomNumFmt :自定义数字格式
    • Lang : 谁的长度
    • NegRed :是否粗体?

几个常用成员(如BorderFill等)使用的结构体我们接下来将做说明:

1.2.1 Border结构体(边框设置)

结构体语法
type Border struct {
    Type  string `json:"type"`
    Color string `json:"color"`
    Style int    `json:"style"`
}
  • 成员说明
    • Type:边线方向
      • left
      • right
      • top
      • bottom
      • diagonalDown:左上到右下
      • diagonalUP:左下到右上
    • Color:颜色
    • Style:边线类型

Style边线类型效果如下:

完整示例
  • 代码
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)
	}
}
  • 结果显示
    在这里插入图片描述

注意:
diagonalDowndiagonalUp同时设置,如果二者样式不同,则diagonalDown会被diagonalUp的样式覆盖。

1.2.2 Fill结构体(填充设置)

结构体语法
type Fill struct {
    Type    string   `json:"type"`
    Pattern int      `json:"pattern"`
    Color   []string `json:"color"`
    Shading int      `json:"shading"`
}
  • 说明:
    • Type
      • gradient:渐变
      • pattern:填充图
    • Shading(Type为gradient时生效)
      • 1:横向填充
      • 2:纵向填充
      • 3:对角线向下填充
      • 4:对角线向上填充
      • 5:从内向外填充
    • Pattern(Type为pattern时生效)
      • 值从1~18(图片“Pattern值”)。
      • 1 表示纯色填充
    • Color
      • Type为gradient时,Color 有两个值,Pattern不生效
      • 切片只有一个成员时,Shading 不生效。

Pattern值:

在这里插入图片描述

完整示例(渐变填充)
  • 代码
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)
	}
}

  • 结果显示
    在这里插入图片描述
示例(纯色填充)
style, err := f.NewStyle(&excelize.Style{
    Fill: excelize.Fill{Type: "pattern", Color: []string{"FF0000"}, Pattern: 1},
})

1.2.3 Font结构体(字体设置)

结构体语法
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"`
}
  • 说明:
    • Bold:是否粗体
    • Italic:是否斜体
    • Underline: 下划线
      • single :单线
      • 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)
	}
}
  • 结果显示
    在这里插入图片描述

1.2.4 Alignment结构体(对齐方式)

结构体语法
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"`
}
  • 说明
    • Horizontal:水平对齐
      • right
      • left
      • center
    • Indent:缩进
    • JustifyLastLine:两端对齐
    • ReadingOrder:文字方向
    • RelativeIndent:相对缩进
    • ShrinkToFit:缩小字体
    • TextRotation:文字旋转
    • Vertical:垂直对齐
      • top
      • bottom
      • center
    • WrapText:自动换行
完整示例
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)
	}
}

结果显示
在这里插入图片描述

1.2.4 NumFmt编号(自定义格式)

参数
索引类型
27yyyy"年"m"月"
28m"月"d"日"
29m"月"d"日"
30m-d-yy
31yyyy"年"m"月"d"日"
32h"时"mm"分"
33h"时"mm"分"ss"秒"
34上午/下午 h"时"mm"分"
35上午/下午 h"时"mm"分"ss"秒
36yyyy"年"m"月
50yyyy"年"m"月
51m"月"d"日
52yyyy"年"m"月
53m"月"d"日
54m"月"d"日
55上午/下午 h"时"mm"分
56上午/下午 h"时"mm"分"ss"秒
57yyyy"年"m"月
58m"月"d"日"
完整示例
import (
    "fmt"
    "github.com/xuri/excelize/v2"
	"time"
)

func main() {
	f := excelize.NewFile()
	numFmt := "yyyy\"年\"m\"月\"d\"日\""
	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)
	}
}
  • 效果

在这里插入图片描述

1.2.5 CustomNumFmt编号(自定义数字)

参数
索引类型
0General
10
20.00
3#,##0
4#,##0.00
5(KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲,##0_);(#,##0)
6(KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲,##0_);[Red](#,##0)
7(KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲,##0.00_);(#,##0.00)
8(KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲,##0.00_);[Red]…#,##0.00)
90%
100.00%
110.00E+00
12# ?/?
13# ??/??
14m/d/yy
15d-mmm-yy
16d-mmm
17mmm-yy
18h:mm AM/PM
19h:mm:ss AM/PM
20h:mm
21h:mm:ss
22m/d/yy h:mm
37(#,##0_) ; (#,##0)
38(#,##0_);Red
39(#,##0.00_); (#,##0.00)
40(#,##0.00_);Red
41(* #,##0);(* (#,##0);(* “-”);(@_)
42(KaTeX parse error: Expected 'EOF', got '#' at position 3: * #̲,##0_);_(* (#,##0);($* “-”);(@_)
43(* #,##0.00);(* (#,##0.00);(* “-”??);(@_)
44(KaTeX parse error: Expected 'EOF', got '#' at position 3: * #̲,##0.00_);_(* (#,##0.00);($* “-”??);(@_)
45mm:ss
46[h]:mm:ss
47mm:ss.0
48##0.0E+0
49@
完整示例(指定小数位)

显示为五位小数

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)
	}
}
  • 效果
    在这里插入图片描述
完整示例(显示节)
  • 代码

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)
	}
}
  • 效果
    在这里插入图片描述

2. 样式使用

2.1 单元格使用样式

  • 语法
func (f *File) SetCellStyle(sheet string, hCell string, vCell string, styleID int) error

2.2 列使用样式

  • 语法
func (f *File) SetColStyle(sheet, columns string, styleID int) error
  • 语法示例
err = f.SetColStyle("Sheet1", "H", style)
err = f.SetColStyle("Sheet1", "C:F", style)

2.3 行使用样式

  • 语法
func (f *File) SetRowStyle(sheet string, start int, end int, styleID int) error
  • 语法示例
err = f.SetRowStyle("Sheet1", 1,3,style)

在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: 要在el-table中固定样式,可以根据以下几种方法进行设置。首先,可以在想要固定的上加上fixed属性,如果固定的不是最左边或最右边的,需要将前面的也加上固定属性,否则只给固定加上fixed属性是不起作用的。\[1\]另外,你也可以使用封装的表格,在循环出表的时候,给索引加上fixed属性,例如使用el-table-column标签,设置width为固定宽度,label为"序号",type为"index",align为"center",并在template中使用scope.$index + 1来显示序号。\[2\]此外,你还可以使用两种方法来设置固定样式。一种方法是在el-table中设置cell-style属性,并在method中定义setRowStyle方法,根据row的状态来返回不同的样式,例如设置成功状态为绿色,失败状态为红色。另一种方法是在el-table-column中使用template来根据row的状态来显示不同的内容,并设置不同的样式,例如成功状态为绿色,失败状态为红色。\[3\] #### 引用[.reference_title] - *1* *2* [el-table设置指定固定,不受滚动条影响](https://blog.csdn.net/weixin_49668076/article/details/124769459)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [vue之el-table某一设置样式](https://blog.csdn.net/u010520146/article/details/116157081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值