在PHPspreadsheet生成的Excel文件中添加边框,可以通过以下步骤实现:
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$cellRange = 'A1:D10'; // 例如,要添加边框的范围是从A1到D10的单元格
getStyle()
方法获取单元格样式对象,并使用getBorders()
方法获取边框对象。$style = $sheet->getStyle($cellRange);
$borders = $style->getBorders();
$borders->getTop()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$borders->getTop()->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color('000000'));
$borders->getBottom()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$borders->getBottom()->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color('000000'));
$borders->getLeft()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$borders->getLeft()->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color('000000'));
$borders->getRight()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
$borders->getRight()->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color('000000'));
$style->setBorders($borders);
$writer = new Xlsx($spreadsheet);
$writer->save('path/to/save/excel.xlsx');
这样,生成的Excel文件中的指定单元格范围将会添加边框。
腾讯云相关产品推荐:腾讯云对象存储(COS),提供高可靠、低成本的云端存储服务,适用于存储、备份和归档等场景。产品介绍链接地址:https://cloud.tencent.com/product/cos
领取专属 10元无门槛券
手把手带您无忧上云