跳转至

边框

本页面介绍如何使用边框样式。

基本边框

设置单边框

from symphra_excel import Workbook
from symphra_excel.styles import CellStyle

wb = Workbook()
sheet = wb.create_worksheet("边框示例")

style = CellStyle()
style.set_border(left="thin")
sheet.set_cell_value("A1", "左边框")
sheet.apply_style("A1", style)

style2 = CellStyle()
style2.set_border(right="thin")
sheet.set_cell_value("A2", "右边框")
sheet.apply_style("A2", style2)

设置四边边框

style = CellStyle()
style.set_border(
    left="thin",
    right="thin",
    top="thin",
    bottom="thin"
)
sheet.set_cell_value("A3", "四边边框")
sheet.apply_style("A3", style)

边框样式

粗细样式

styles = ["thin", "medium", "thick"]

for idx, border_style in enumerate(styles, start=1):
    style = CellStyle()
    style.set_border(
        left=border_style,
        right=border_style,
        top=border_style,
        bottom=border_style
    )
    sheet.set_cell_value(f"B{idx}", border_style)
    sheet.apply_style(f"B{idx}", style)

线条类型

line_styles = ["dashed", "dotted", "double"]

for idx, line_style in enumerate(line_styles, start=1):
    style = CellStyle()
    style.set_border(
        left=line_style,
        right=line_style,
        top=line_style,
        bottom=line_style
    )
    sheet.set_cell_value(f"C{idx}", line_style)
    sheet.apply_style(f"C{idx}", style)

边框颜色

设置边框颜色

from symphra_excel.styles import Color

style = CellStyle()
style.set_border(
    left="medium",
    right="medium",
    top="medium",
    bottom="medium",
    left_color="#FF0000",
    right_color="#00FF00",
    top_color="#0000FF",
    bottom_color="#FFFF00"
)
sheet.set_cell_value("D1", "彩色边框")
sheet.apply_style("D1", style)

完整示例

示例1: 表格边框

from symphra_excel import Workbook
from symphra_excel.styles import CellStyle

with Workbook() as wb:
    sheet = wb.create_worksheet("表格")

    header_style = CellStyle()
    header_style.set_border(
        left="medium",
        right="medium",
        top="medium",
        bottom="medium"
    )
    header_style.set_font(bold=True)
    header_style.set_background_color("#E0E0E0")

    cell_style = CellStyle()
    cell_style.set_border(
        left="thin",
        right="thin",
        top="thin",
        bottom="thin"
    )

    headers = ["姓名", "年龄", "城市"]
    for col_idx, header in enumerate(headers, start=1):
        cell = f"{chr(64+col_idx)}1"
        sheet.set_cell_value(cell, header)
        sheet.apply_style(cell, header_style)

    data = [
        ["张三", 25, "北京"],
        ["李四", 30, "上海"],
    ]

    for row_idx, row in enumerate(data, start=2):
        for col_idx, value in enumerate(row, start=1):
            cell = f"{chr(64+col_idx)}{row_idx}"
            sheet.set_cell_value(cell, value)
            sheet.apply_style(cell, cell_style)

    wb.save("bordered_table.xlsx")

示例2: 突出边框

from symphra_excel import Workbook
from symphra_excel.styles import CellStyle

with Workbook() as wb:
    sheet = wb.create_worksheet("突出")

    important_style = CellStyle()
    important_style.set_border(
        left="thick",
        right="thick",
        top="thick",
        bottom="thick",
        left_color="#FF0000",
        right_color="#FF0000",
        top_color="#FF0000",
        bottom_color="#FF0000"
    )

    sheet.set_cell_value("A1", "重要信息")
    sheet.apply_style("A1", important_style)

    wb.save("highlighted_border.xlsx")

边框样式类型

可用样式列表

border_styles = [
    "thin",
    "medium",
    "thick",
    "dashed",
    "dotted",
    "double",
    "hair",
    "mediumDashed",
    "dashDot",
    "mediumDashDot",
    "dashDotDot",
    "mediumDashDotDot",
    "slantDashDot",
]

最佳实践

✅ 推荐做法

table_style = CellStyle()
table_style.set_border(
    left="thin",
    right="thin",
    top="thin",
    bottom="thin"
)

header_style = CellStyle()
header_style.set_border(
    left="medium",
    right="medium",
    top="medium",
    bottom="medium"
)

❌ 避免的做法

style = CellStyle()
style.set_border(left="invalid_style")

for i in range(10000):
    style = CellStyle()
    style.set_border(
        left="thick",
        right="thick",
        top="thick",
        bottom="thick"
    )
    sheet.apply_style(f"A{i}", style)

下一步