phpspreadsheet 设置范围内所有单元格的边框

phpspreadsheet set border for all cell in range

提问人:Premlatha 提问时间:10/19/2023 更新时间:10/19/2023 访问量:33

问:

我想为给定范围内的所有单元格设置上边框。但是,边框仅适用于范围中的第一个单元格。我尝试了另一种类型的样式,填充单元格颜色。它适用于范围内的所有单元格。对于单个单元格和范围内的单元格,使用了相同的代码

<?php
 require 'vendor/autoload.php';
 use PhpOffice\PhpSpreadsheet\Spreadsheet;
 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
 //border
 use PhpOffice\PhpSpreadsheet\Style\Border;
 use PhpOffice\PhpSpreadsheet\Style\Color;
 use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
 $spreadsheet = new Spreadsheet();
 $activeWorksheet = $spreadsheet->getActiveSheet();    
 $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()- >setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK);    
 $spreadsheet->getActiveSheet()->getStyle('D2:D4')->getBorders()->getTop()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK);    
 
 $spreadsheet->getActiveSheet()->getStyle('F2')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('FFFF0000');   ;    
 $spreadsheet->getActiveSheet()->getStyle('H2:H4')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('FFFF0000');

 $styleArray = [

   'borders' => [
      'top' => [
        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
       ],
    ],  
  ];
 $spreadsheet->getActiveSheet()->getStyle('J2:J4')->applyFromArray($styleArray);       
 $writer = new Xlsx($spreadsheet);
 $writer->save('output_rangeStyling.xlsx');
?>

output

我试过使用 ,但不适用于范围内的所有单元格。是否有任何选项而不是循环来设置单元格范围的边框? 谢谢。$spreadsheet->getActiveSheet()->getStyle($rangeTotal)->applyFromArray($styleArray);

php phpspreadsheet phpoffice-phpspreadsheet

评论


答: 暂无答案