本文介绍了用PhpSpreadsheet PHP样式化单元格边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用PhpSpreadsheet读取或写入Excel文件.我想在边框中添加边框样式,所以我使用了以下代码:
I use PhpSpreadsheet to read o write in Excel files. I want to add to my excel a border style so I used this code:
<?php
$fxls ='myfile.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fxls);
$xls_data = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
$sheet = $spreadsheet->getActiveSheet();
$styleArray = array(
'borders' => array(
'outline' => array(
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => array('argb' => 'FFFF0000'),
),
),
);
$sheet ->getStyle('B2:G8')->applyFromArray($styleArray);
/* Generate the Excel File */
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="myNEWFile.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('php://output');
exit;
我没有收到任何错误,但是创建的Excel文件没有边框.我想念什么!
I get no error but the excel file is created without border. What I miss !??
推荐答案
$styleArray = array(
'borders' => array(
'outline' => array(
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => array('argb' => 'FFFF0000'),
),
),
);
替换为:
$styleArray = array(
'borders' => array(
'outline' => array(
'style' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => array('argb' => 'FFFF0000'),
),
),
);
请参见 169-203.
borderStyle
.
之前,边框配置仍为style
这篇关于用PhpSpreadsheet PHP样式化单元格边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!