我使用PhpSpreadsheet来读取写入Excel文件。我想添加到我的excel边框样式,所以我使用了这段代码:
<?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文件。我想念的是什么!??
答案 0 :(得分:5)
除了 style array way 之外,您还可以通过方法链接的方式来实现:
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Color;
$spreadsheet
->getActiveSheet()
->getStyle('B2')
->getBorders()
->getOutline()
->setBorderStyle(Border::BORDER_THICK)
->setColor(new Color('FFFF0000'));
公然抄袭official docs on cell formatting。
docs on cell formatting 包含可用模式列表及其在样式数组形式中使用的键。对于方法链接形式,只需在密钥的大写版本之前添加一个 get
。这些方法都在 ->getActiveSheet()->getStyle('B2')->getBorders()
下可用,就像示例中一样。
->getLeft()
->getRight()
->getTop()
->getBottom()
->getDiagonal()
->getAllBorders()
->getOutline()
->getInside()
->getVertical()
->getHorizontal()
可视化的模式(也来自docs):
Border::BORDER_DASHDOT
Border::BORDER_DASHDOTDOT
Border::BORDER_DASHED
Border::BORDER_DOTTED
Border::BORDER_DOUBLE
Border::BORDER_HAIR
Border::BORDER_MEDIUM
Border::BORDER_MEDIUMDASHDOT
Border::BORDER_MEDIUMDASHDOTDOT
Border::BORDER_MEDIUMDASHED
Border::BORDER_NONE
Border::BORDER_SLANTDASHDOT
Border::BORDER_THICK
Border::BORDER_THIN
答案 1 :(得分:3)
$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行
在2017-11-26发布1.0.0-beta2后, borderStyle
已添加。
之前,边框配置仍然是style
答案 2 :(得分:1)
您需要将值重新分配给工作表:
$styleArray = array(
'borders' => array(
'outline' => array(
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => array('argb' => 'FFFF0000'),
),
),
);
$sheet = $sheet ->getStyle('B2:G8')->applyFromArray($styleArray);