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)