跳到主要内容
Office Open

XLSX

样式

字体、填充、边框、对齐和数字格式

XLSX 使用基于索引的样式系统。当你为单元格应用样式时,库会自动去重并注册样式。

#字体样式

JSON
{
  "worksheets": [
    {
      "name": "Sheet1",
      "rows": [
        {
          "cells": [
            { "value": "Bold" },
            {
              "value": "Styled",
              "style": {
                "font": {
                  "bold": true,
                  "italic": true,
                  "size": 14,
                  "color": { "rgb": "FFFF0000" },
                  "font": "Arial"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

#填充样式

JSON
{
  "worksheets": [
    {
      "name": "Sheet1",
      "rows": [
        {
          "cells": [
            {
              "value": "Yellow",
              "style": { "fill": { "foregroundColor": { "rgb": "FFFFFF00" } } }
            },
            { "value": "Blue", "style": { "fill": { "foregroundColor": { "rgb": "FF4472C4" } } } }
          ]
        }
      ]
    }
  ]
}

#边框样式

JSON
{
  "worksheets": [
    {
      "name": "Sheet1",
      "rows": [
        {
          "cells": [
            {
              "value": "Bordered",
              "style": {
                "border": {
                  "top": { "style": "thin", "color": { "rgb": "FF000000" } },
                  "bottom": { "style": "thin", "color": { "rgb": "FF000000" } },
                  "left": { "style": "thin", "color": { "rgb": "FF000000" } },
                  "right": { "style": "thin", "color": { "rgb": "FF000000" } }
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

边框样式可选值:thin、medium、thick、dotted、dashed、double、hair、mediumDashed、dashDot、mediumDashDot、dashDotDot、mediumDashDotDot、slantDashDot、none。

#对齐

JSON
{
  "worksheets": [
    {
      "name": "Sheet1",
      "rows": [
        {
          "cells": [
            {
              "value": "Centered",
              "style": {
                "alignment": { "horizontal": "center", "vertical": "center", "wrapText": true }
              }
            }
          ]
        }
      ]
    }
  ]
}

#组合样式

样式会自动组合和去重:

JSON
{
  "worksheets": [
    {
      "name": "Sheet1",
      "rows": [
        {
          "cells": [
            {
              "value": "Name",
              "style": {
                "font": { "bold": true, "color": { "rgb": "FFFFFFFF" } },
                "fill": { "foregroundColor": { "rgb": "FF4472C4" } },
                "alignment": { "horizontal": "center" }
              }
            },
            {
              "value": "Score",
              "style": {
                "font": { "bold": true, "color": { "rgb": "FFFFFFFF" } },
                "fill": { "foregroundColor": { "rgb": "FF4472C4" } },
                "alignment": { "horizontal": "center" }
              }
            }
          ]
        },
        { "cells": [{ "value": "Alice" }, { "value": 95 }] }
      ]
    }
  ]
}

重复使用相同的样式对象会复用同一个 cellXf 索引,不会在输出文件中产生不必要的重复。

#字体选项参考

选项类型说明
boldboolean加粗
italicboolean斜体
underlineboolean下划线
strikeboolean删除线
sizenumber字号(磅)
colorColorOptions电子表格颜色(CT_Color):rgb ARGB、theme、tint、indexed 或 auto
fontstring字体名称

#边框选项参考

选项类型说明
stylestringthin、medium、thick、dotted、dashed 等
colorColorOptionsCT_Color(rgb ARGB / theme / tint / indexed / auto)

#对齐选项参考

选项类型说明
horizontalstringgeneral、left、center、right、fill、justify、centerContinuous、distributed
verticalstringtop、center、bottom、justify、distributed
wrapTextboolean单元格内自动换行
textRotationnumber文本旋转角度
indentnumber缩进级别