【PhpSpreadsheet】实现excel冻结列和行(即左右滚动时指定列和行固定不动)

composer require phpoffice/phpspreadsheet
版本:^1.18

在做表格数据导出时,如何冻结列和行(即左右滚动时冻结的列和行固定不动)

代码

$spreadsheet = new Spreadsheet();

// 获取激活工作表
$sheet = $spreadsheet->getActiveSheet();

// 设定A3行(上)冻结窗格
$sheet->freezePane("A3");

// 设定B1行(左)冻结窗格
$sheet->freezePane("B1");

// 设定B3,行(上)、列(左)冻结窗格
$sheet->freezePane("B3");

效果

在这里插入图片描述

扩展

【源】phpspreadsheet的源码注解

github:
https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Worksheet/Worksheet.php

// file: vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/Worksheet.php [1950]

/**
 * Freeze Pane.
 *
 * Examples:
 *
 *     - A2 will freeze the rows above cell A2 (i.e row 1)
 *     - B1 will freeze the columns to the left of cell B1 (i.e column A)
 *     - B2 will freeze the rows above and to the left of cell B2 (i.e row 1 and column A)
 * 示例:
 *
 *     -A2将冻结单元格A2上方的行(即第1行)
 *     -B1将冻结单元格B1左侧的列(即A列)
 *     -B2将冻结单元格B2上方和左侧的行(即第1行和A列)
 *
 * @param null|string $cell Position of the split
 * @param null|string $topLeftCell default position of the right bottom pane
 *
 * @return $this
 */
public function freezePane($cell, $topLeftCell = null)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

转悠的陀螺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值