phpspreadsheet常用设置项

2021/11/8 1:10:07

本文主要是介绍phpspreadsheet常用设置项,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

实例化对象

composer 导包 "phpoffice/phpspreadsheet": "^1.11",
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
  • 合并单元格
$sheet->mergeCells('A1:J1');
  • 设置单元格的内容
$spreadsheet->getActiveSheet()
  ->setCellValue('A2', '序号')
  ->setCellValue('D2', '联系人')
  ->setCellValue('G2', '手机');
  • 单元格水平和垂直都居中
$styleArray = [
    'alignment' => [
        'horizontal' => Alignment::HORIZONTAL_CENTER,
        'vertical'=>Alignment::VERTICAL_CENTER
    ],
];
$sheet->getStyle('A1')->applyFromArray($styleArray);
  • 单元格垂直居中水平左对齐
$styleArray2 = [
    'alignment' => [
        'horizontal' => Alignment::HORIZONTAL_LEFT,
        'vertical'=>Alignment::VERTICAL_CENTER
    ],
];
$sheet->getStyle('A2')->applyFromArray($styleArray2);
  • 将单元格字体设置粗体,字体大小20
$spreadsheet->getActiveSheet()
	->getStyle('A1')
	->getFont()
	->setBold(true)
	->setSize(20);
  • 设置第一行的行高为30
$spreadsheet->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
  • 选择单元格设置自动换行,并且设置水平和垂直居中
$sheet->getStyle('A4:J8')
    ->getAlignment()->setWrapText(true) // 自动换行
    ->setVertical(Alignment::VERTICAL_CENTER)
    ->setHorizontal(Alignment::HORIZONTAL_CENTER);
  • 单元格所有边框
$styleArray = [
    'borders' => [
        'allBorders' => [
            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
            'color' => ['argb' => '000000'],
        ],
    ],
];
$sheet->getStyle('A3:J8')->applyFromArray($styleArray);
  • 设置列宽
$sheet->getColumnDimension("B")->setWidth(20);


这篇关于phpspreadsheet常用设置项的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程