XLSX

样式

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

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

字体样式

{
  "worksheets": [
    {
      "name": "Sheet1",
      "children": [
        {
          "cells": [
            { "value": "Bold" },
            {
              "value": "Styled",
              "style": {
                "font": {
                  "bold": true,
                  "italic": true,
                  "size": 14,
                  "color": "FF0000",
                  "fontName": "Arial"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

填充样式

{
  "worksheets": [
    {
      "name": "Sheet1",
      "children": [
        {
          "cells": [
            { "value": "Yellow", "style": { "fill": { "color": "FFFF00" } } },
            { "value": "Blue", "style": { "fill": { "color": "4472C4" } } }
          ]
        }
      ]
    }
  ]
}

边框样式

{
  "worksheets": [
    {
      "name": "Sheet1",
      "children": [
        {
          "cells": [
            {
              "value": "Bordered",
              "style": {
                "border": {
                  "top": { "style": "thin", "color": "000000" },
                  "bottom": { "style": "thin", "color": "000000" },
                  "left": { "style": "thin", "color": "000000" },
                  "right": { "style": "thin", "color": "000000" }
                }
              }
            }
          ]
        }
      ]
    }
  ]
}

边框样式可选值:thinmediumthickdotteddasheddoublehairnone

对齐

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

组合样式

样式会自动组合和去重:

{
  "worksheets": [
    {
      "name": "Sheet1",
      "children": [
        {
          "cells": [
            {
              "value": "Name",
              "style": {
                "font": { "bold": true, "color": "FFFFFF" },
                "fill": { "color": "4472C4" },
                "alignment": { "horizontal": "center" }
              }
            },
            {
              "value": "Score",
              "style": {
                "font": { "bold": true, "color": "FFFFFF" },
                "fill": { "color": "4472C4" },
                "alignment": { "horizontal": "center" }
              }
            }
          ]
        },
        { "cells": [{ "value": "Alice" }, { "value": 95 }] }
      ]
    }
  ]
}

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

字体选项参考

选项类型说明
boldboolean加粗
italicboolean斜体
underlineboolean下划线
strikeboolean删除线
sizenumber字号(磅)
colorstring十六进制颜色
fontNamestring字体名称

边框选项参考

选项类型说明
stylestringthinmediumthickdotteddashed
colorstring十六进制颜色

对齐选项参考

选项类型说明
horizontalstringleftcenterrightfilljustify
verticalstringtopcenterbottom
wrapTextboolean单元格内自动换行
textRotationnumber文本旋转角度
indentnumber缩进级别
Copyright © 2026