跳至主要内容

如何在打印Excel工作表时为页眉或页脚添加背景色?

Author: Sun Last Modified: 2025-05-07

众所周知,我们可以在工作表的页眉或页脚中添加文本或图片,这些内容在打印时会显示出来。但你是否尝试过为页眉或页脚添加背景色呢?实际上,Excel并没有内置功能可以实现这一点,但在这里我将介绍一些间接的方法来完成该操作。

通过填充颜色为页眉或页脚添加背景色

通过VBA为页眉和页脚添加背景色


通过填充颜色为页眉或页脚添加背景色

1. 点击 页面布局 > 页面设置.
A screenshot showing Page Layout Page Setup option in Excel

2. 在 页面设置 对话框中,切换到 页边距 选项卡,在 0 输入 顶部, 页眉, 底部页脚 文本框中设置值。
A screenshot showing Page Setup dialog to adjust margins in Excel

3. 点击 确定。然后选择第一行并点击 开始 > 合并居中.
A screenshot of Merge and Center option

3. 接着从 填充颜色 列表中选择一种颜色,为合并后的单元格添加背景色。
A screenshot of selecting fill color for Excel header

4. 重复步骤2和步骤3,为所需的页脚行添加背景色。

现在,页面看起来已经为页眉和页脚添加了背景色。
A screenshot showing Excel worksheet with header and footer color


通过VBA为页眉和页脚添加背景色

这里有一个VBA代码可以快速同时为页眉和页脚添加背景色。

1. 按下 Alt + F11 键打开 Microsoft Visual Basic for Applications 窗口。

2. 点击 插入 > 模块,复制并将以下代码粘贴到新模块中。

VBA:为页眉和页脚添加背景色

Sub FakeHeaderFooter()
'UpdatebyExtendoffice20180628
    Dim I, J As Long
   Dim xRg As Range
    Dim xRow, xCol As Long
    Dim xRows, xCols As Long
    Dim xDivRow, xDivCol As Long
    Dim xTopArr, xButtArr As Variant
    Dim PageSize1, PageSize2 As Integer
    On Error Resume Next
    xTopArr = Array("Top Left", "", "", "Top Center", "", "", "", "")
    xButtArr = Array("Bottom Left", "", "", "Bottom Center", "", "", "", "")
    PageSize1 = 46
    PageSize2 = 8
    With ActiveSheet.PageSetup
        .TopMargin = Application.InchesToPoints(0)
        .BottomMargin = Application.InchesToPoints(0)
        .HeaderMargin = Application.InchesToPoints(0)
        .FooterMargin = Application.InchesToPoints(0)
        .BlackAndWhite = False
    End With
    Set xRg = ActiveSheet.UsedRange
    If xRg Is Nothing Then Exit Sub
    xRows = xRg(xRg.Count).Row
    xCols = xRg(xRg.Count).Column
    If xRows < 46 Then xRows = 46
    If xCol < 8 Then xCol = 8
    xDivRow = Int(xRows / PageSize1)
    xDivCol = Int(xCols / PageSize2)
    If ((xRows Mod PageSize1) > 0) And (xDivRow <> 0) Then xDivRow = xDivRow + 1
    If ((xCols Mod PageSize2) > 0) And (xDivCol <> 0) Then xDivCol = xDivCol + 1
    If xDivRow = 0 Then xDivRow = 1
    If xDivCol = 0 Then xDivCol = 1
    Set xRg = Range("A1").Resize(xDivRow * PageSize1, xDivCol * PageSize2)
    xRow = 1
    xCol = 1
    Cells.PageBreak = xlPageBreakNone
    For I = 1 To xDivRow * PageSize1 Step PageSize1 + 1
        For J = 1 To xDivCol * PageSize2 Step PageSize2
            Cells(I, J).Resize(1, PageSize2) = xTopArr
            Cells(I, J).Resize(1, PageSize2).Interior.ColorIndex = 34
            Cells(I + PageSize1, J).Resize(1, PageSize2) = xButtArr
            Cells(I + PageSize1, J).Resize(1, PageSize2).Interior.ColorIndex = 34
            Rows(I + PageSize1 + 1).PageBreak = xlManual
            Columns(J + PageSize2).PageBreak = xlManual
        Next
    Next
End Sub

A screenshot showing VBA code pasted in a module

3. 按下 F5 键运行代码,随后每个工作表的第一行和第47行将被添加指定的背景色,作为伪页眉和页脚显示。
A screenshot of VBA-applied header and footer color in Excel

注意:在代码中,你可以根据需要更改以下脚本。

1. 根据需要更改字符串中的页眉和页脚内容:

xTopArr = Array("左上", "", "", "中上", "", "", "", "")

xButtArr = Array("左下", "", "", "中下", "", "", "", "")

2. 更改此字符串中的背景色编号:

Cells(I, J).Resize(1, PageSize2).Interior.ColorIndex = 34

Cells(I + PageSize1, J).Resize(1, PageSize2).Interior.ColorIndex = 34


最佳办公效率工具

🤖 Kutools AI 助手:基于智能执行生成代码创建自定义公式分析数据并生成图表调用 Kutools 函数等功能,彻底改变数据分析方式…
热门功能查找、高亮或标记重复项 | 删除空行 | 合并不丢失数据的列或单元格 | 四舍五入 ...
高级 LOOKUP多条件 VLookup | 多值 VLookup | 多表查找 | 模糊查找 ....
高级下拉列表快速创建下拉列表 | 从属下拉列表 | 多选下拉列表 ....
列管理器添加指定数量的列 | 移动列 | 切换隐藏列的可见状态 | 比较区域和列 ...
精选功能网格聚焦 | 设计视图 | 增强编辑栏 | 工作簿与工作表管理器 | 资源库(自动文本) | 日期提取 | 合并数据 | 加密/解密单元格 | 按列表发送电子邮件 | 超级筛选 | 特殊筛选(筛选粗体/斜体/删除线...)...
排名前 15 的工具集12 种文本 工具添加文本删除特定字符等) | 50 多 种图表 类型甘特图等) | 40 多种实用 公式基于生日计算年龄等) | 19 种插入 工具插入二维码根据路径插入图片等) | 12 种转换 工具小写金额转大写汇率转换等) | 7 种合并与分割 工具高级合并行分割单元格等) | 还有更多...

使用 Kutools for Excel 提升您的 Excel 技能,体验前所未有的高效。 Kutools for Excel 提供超过 300 种高级功能来提高生产力并节省时间。 单击此处获取您最需要的功能...


Office Tab 将标签式界面引入 Office,让您的工作更加轻松

  • 在 Word、Excel、PowerPoint 中启用标签式编辑和阅读
  • 在同一窗口的新标签页中打开和创建多个文档,而不是在新窗口中进行操作。
  • 将您的生产力提升 50%,每天为您减少数百次鼠标点击!