注册 登录

清河洛

go的excelize库中的创建样式

qingheluo2022-12-08清河洛953
由于在excel中的样式有多种,excelize库中为每种类型的样式制定的不同的数据机构,最后将所有的样式合并颜色使用6位的16进制颜色字符串表示对齐样式type Alignment struct { Horizontal string `json:"horizontal"` Indent int `json:"indent"` ShrinkToFit bool `json:"shrink_to_fit"` TextRotation int ...

由于在excel中的样式有多种,excelize库中为每种类型的样式制定的不同的数据机构,最后将所有的样式合并

颜色使用6位的16进制颜色字符串表示

对齐样式

type Alignment struct {
    Horizontal      string `json:"horizontal"`
    Indent          int    `json:"indent"`
    ShrinkToFit     bool   `json:"shrink_to_fit"`
    TextRotation    int    `json:"text_rotation"`
    Vertical        string `json:"vertical"`
    WrapText        bool   `json:"wrap_text"`
}

Horizontal:水平对齐,可选值
    left    向左
    center  居中
    right   靠右
    fill    填充
    justify 两端对齐
    centerContinuous    跨列居中
    distributed 分散对齐
Indent:缩进
ShrinkToFit:收缩到适合,当单元格撑不下时自动缩小
TextRotation:文本旋转,正数表示逆时针角度
Vertical:垂直对齐,可选值
    top 顶端对齐
    center  居中
    justify 两端对齐
    distributed 分散对齐
WrapText:自动换行

边框样式

type Border struct {
    Type  string `json:"type"`
    Color string `json:"color"`
    Style int    `json:"style"`
}

Type:边框位置,可选值:"left"、"right"、"top"、"bottom"
Style:边框样式,只能根据索引使用预设的样式

边框样式.png

边框设置中一般同时设置多条

Border:[]excelize.Border{
    {Type:"left",Color:"2F75B5",Style:5},
    {Type:"right",Color:"C65911",Style:6},
    {Type:"top",Color:"FFD966",Style:8},
    {Type:"bottom",Color:"ACB9CA",Style:10},
}

字体样式

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"`
}

Bold:加粗
Italic:斜体
Underline:下划线,"single"单下划线,"double"双下划线
Family:字体名称
Size:字体大小
Strike:删除线
Color:颜色

单元格填充

type Fill struct {
    Type    string   `json:"type"`
    Pattern int      `json:"pattern"`
    Color   []string `json:"color"`
    Shading int      `json:"shading"`
}
Type:填充类别,可选值"gradient"(渐变)和"pattern"(图案)
Pattern:图案填充,索引值为0-18,具体图案请自行测试
Shading:颜色填充方式
    0   横向
    1   纵向
    2   对角线向上
    3   对角线向下
    4   从对角线向内
    5   从中心向外

整合样式

type Style struct {
    Border        []Border    `json:"border"`
    Fill          Fill        `json:"fill"`
    Font          *Font       `json:"font"`
    Alignment     *Alignment  `json:"alignment"`
    DecimalPlaces int         `json:"decimal_places"`
    CustomNumFmt  *string     `json:"custom_number_format"`
}

DecimalPlaces:小数保留位数
CustomNumFmt:自定义格式,字符串"General"表示默认格式



网址导航