编程语言
首页 > 编程语言> > phpspreadsheet常用设置项

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);
$spreadsheet->getActiveSheet()
	->getStyle('A1')
	->getFont()
	->setBold(true)
	->setSize(20);
$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);

标签:常用,sheet,CENTER,单元格,spreadsheet,phpspreadsheet,getStyle,设置,Alignment
来源: https://blog.csdn.net/m0_37894494/article/details/121199009