PhpSpreadsheet 在 XLSX 中有边框但在带有 mPDF 的 PDF 中没有边框 class
PhpSpreadsheet Have border in XLSX but no border in PDF with mPDF class
我正在尝试使用两种格式(xlsx 和 pdf)的 php 电子表格生成报告。当我 运行 我的代码并保存在 xlsx 中时,文档中的 table 有边框,但是当我将它保存为 pdf 时,它没有边框。所有其他的东西都是在两者中产生的。
首先,我使用了旧版本的 phpexcel v5.x 和 mpdf v.5x 以及 php v5.6.x
我已经迁移到 PHP 7.1; mpdf 7x phpspreadsheet 7x,希望能解决问题。
还尝试了不同的边框样式 BORDER_THIN BORDER_MEDIUM BORDER_HAIR 但没有成功。
有人知道我做错了什么吗?
<?php
enter code here
$fileformat = $_GET['fileformat'];
$d = new DateTime('2018-09');
$lastDayOfCurrentMonth = $d->format( 't/m/Y' );
$resultset = array();
$data1 = array();
$data1['id'] = '1';
$data1['description'] = 'product 1';
$data1['code'] = '32165478';
$data1['value1'] = '0.12';
$data1['value2'] = '0.54';
array_push($resultset, $data1);
$data2 = array();
$data2['id'] = '2';
$data2['description'] = 'product 2';
$data2['code'] = '32165478';
$data2['value1'] = '3.56';
$data2['value2'] = '3.23';
array_push($resultset, $data2);
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
error_reporting(E_ALL);
require_once $_SERVER["DOCUMENT_ROOT"] . "/dds/vendor/autoload.php";
$helper = new Sample();
if ($helper->isCli()) {
return;
}
$spreadsheet = new Spreadsheet();
$spreadsheet->getProperties()->setCreator("a.ro")
->setLastModifiedBy("a.ro")
->setTitle("asdfsdaf")
->setSubject("asdfsdaf")
->setDescription("asdfsdaf")
->setKeywords("asdfsdaf")
->setCategory("asdfsdaf");
$spreadsheet->getActiveSheet()
->getPageMargins()->setTop(1);
$spreadsheet->getActiveSheet()
->getPageMargins()->setRight(0.75);
$spreadsheet->getActiveSheet()
->getPageMargins()->setLeft(0.75);
$spreadsheet->getActiveSheet()
->getPageMargins()->setBottom(1);
$spreadsheet->getActiveSheet()
->getPageSetup()
->setRowsToRepeatAtTopByStartAndEnd(6, 7);
$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(3.86);
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(56.86);
$spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(5);
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(10.86);
$spreadsheet->getActiveSheet()->getColumnDimension('E')->setWidth(10.86);
$spreadsheet->getActiveSheet()->mergeCells('A1:D1');
$spreadsheet->getActiveSheet()->setCellValue('A1', 'HEADER row1');
$spreadsheet->getActiveSheet()->mergeCells('A2:D2');
$spreadsheet->getActiveSheet()->setCellValue('A2', 'HEADER row2');
$spreadsheet->getActiveSheet()->mergeCells('A3:E3');
$spreadsheet->getActiveSheet()->setCellValue('A3', 'TITLE');
$styleArray = array(
'font' => array(
'bold' => true,
'color' => array('rgb' => '000000'),
'size' => 14,
'name' => 'Verdana'
),
'alignment' => array(
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
)
);
$spreadsheet->getActiveSheet()->getStyle('A3')->applyFromArray($styleArray);
$spreadsheet->getActiveSheet()->mergeCells('A4:E4');
$spreadsheet->getActiveSheet()->setCellValue('A4', $lastDayOfCurrentMonth);
$spreadsheet->getActiveSheet()->getStyle('A4')->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)));
$spreadsheet->getActiveSheet()->mergeCells('A5:B5');
$spreadsheet->getActiveSheet()->setCellValue('A5', "code 02");
$spreadsheet->getActiveSheet()->setCellValue('E5', "- euro -");
$spreadsheet->getActiveSheet()->setCellValue('A6', 'ID');
$spreadsheet->getActiveSheet()->setCellValue('B6', 'DESCRTIPTION');
$spreadsheet->getActiveSheet()->setCellValue('C6', 'Code');
$spreadsheet->getActiveSheet()->setCellValue('D6', 'Value1');
$spreadsheet->getActiveSheet()->setCellValue('E6', 'Value2');
$spreadsheet->getActiveSheet()->getRowDimension(6)->setRowHeight(40);
$styleArrayTabel = array(
'alignment' => array(
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
'rotation' => 0,
'wrap' => true
),
'borders' => array(
'allBorders' => array(
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, //BORDER_THIN BORDER_MEDIUM BORDER_HAIR
'color' => array('rgb' => '000000')
)
)
);
$spreadsheet->getActiveSheet()->getStyle('A6:E6')->applyFromArray($styleArrayTabel);
$spreadsheet->getActiveSheet()->setCellValue('A7', 'A');
$spreadsheet->getActiveSheet()->setCellValue('B7', 'B');
$spreadsheet->getActiveSheet()->setCellValue('C7', 'C');
$spreadsheet->getActiveSheet()->setCellValue('D7', 'D');
$spreadsheet->getActiveSheet()->setCellValue('E7', 'E');
$spreadsheet->getActiveSheet()->getStyle('A7:E7')->applyFromArray($styleArrayTabel);
$baseRow = 9;
foreach($resultset as $r => $dataRow) {
$row = $baseRow + $r;
$spreadsheet->getActiveSheet()->insertNewRowBefore($row,1);
$spreadsheet->getActiveSheet()->setCellValue('A'.$row, $dataRow['id'])
->setCellValue('B'.$row, $dataRow['description'])
->setCellValue('C'.$row, $dataRow['code'])
->setCellValue('D'.$row, $dataRow['value1'])
->setCellValue('E'.$row, $dataRow['value2']);
$spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight(-1);
$spreadsheet->getActiveSheet()->getStyle('A'.$row.':E'.$row)->applyFromArray(array('borders' => array('allborders' => array('style' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN))));
$spreadsheet->getActiveSheet()->getStyle('A'.$row)->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER)));
$spreadsheet->getActiveSheet()->getStyle('B'.$row)->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT,'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,'wrapText' => true)));
$spreadsheet->getActiveSheet()->getStyle('C'.$row)->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER)));
$spreadsheet->getActiveSheet()->getStyle('D'.$row)->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER)));
$spreadsheet->getActiveSheet()->getStyle('D'.$row)->getNumberFormat()->setFormatCode('#0');
$spreadsheet->getActiveSheet()->getStyle('E'.$row)->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER)));
$spreadsheet->getActiveSheet()->getStyle('E'.$row)->getNumberFormat()->setFormatCode('#0');
}
$spreadsheet->getActiveSheet()->removeRow($baseRow-1,1);
$spreadsheet->getActiveSheet()->setTitle('Anexa2');
$spreadsheet->getActiveSheet()->setShowGridLines(false);
$spreadsheet->setActiveSheetIndex(0);
$helper->log('Hide grid lines');
$spreadsheet->getActiveSheet()->setShowGridLines(false);
$helper->log('Set orientation to landscape');
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
if($fileformat == 'pdf')
{
$helper->log('PDF file format selected');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Mpdf");
$writer->save("aaa.pdf");
exit();
} elseif ($fileformat == 'xlsx')
{
$helper->log('XLSX file format selected');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$writer->save("aaa.xlsx");
exit();
}
else { $helper->log('no file format selected'); }
?>
删除 setShowGridLines(false) 解决了 mPDF 渲染问题
//$spreadsheet->getActiveSheet()->setShowGridLines(false);
我正在尝试使用两种格式(xlsx 和 pdf)的 php 电子表格生成报告。当我 运行 我的代码并保存在 xlsx 中时,文档中的 table 有边框,但是当我将它保存为 pdf 时,它没有边框。所有其他的东西都是在两者中产生的。 首先,我使用了旧版本的 phpexcel v5.x 和 mpdf v.5x 以及 php v5.6.x 我已经迁移到 PHP 7.1; mpdf 7x phpspreadsheet 7x,希望能解决问题。 还尝试了不同的边框样式 BORDER_THIN BORDER_MEDIUM BORDER_HAIR 但没有成功。
有人知道我做错了什么吗?
<?php
enter code here
$fileformat = $_GET['fileformat'];
$d = new DateTime('2018-09');
$lastDayOfCurrentMonth = $d->format( 't/m/Y' );
$resultset = array();
$data1 = array();
$data1['id'] = '1';
$data1['description'] = 'product 1';
$data1['code'] = '32165478';
$data1['value1'] = '0.12';
$data1['value2'] = '0.54';
array_push($resultset, $data1);
$data2 = array();
$data2['id'] = '2';
$data2['description'] = 'product 2';
$data2['code'] = '32165478';
$data2['value1'] = '3.56';
$data2['value2'] = '3.23';
array_push($resultset, $data2);
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
error_reporting(E_ALL);
require_once $_SERVER["DOCUMENT_ROOT"] . "/dds/vendor/autoload.php";
$helper = new Sample();
if ($helper->isCli()) {
return;
}
$spreadsheet = new Spreadsheet();
$spreadsheet->getProperties()->setCreator("a.ro")
->setLastModifiedBy("a.ro")
->setTitle("asdfsdaf")
->setSubject("asdfsdaf")
->setDescription("asdfsdaf")
->setKeywords("asdfsdaf")
->setCategory("asdfsdaf");
$spreadsheet->getActiveSheet()
->getPageMargins()->setTop(1);
$spreadsheet->getActiveSheet()
->getPageMargins()->setRight(0.75);
$spreadsheet->getActiveSheet()
->getPageMargins()->setLeft(0.75);
$spreadsheet->getActiveSheet()
->getPageMargins()->setBottom(1);
$spreadsheet->getActiveSheet()
->getPageSetup()
->setRowsToRepeatAtTopByStartAndEnd(6, 7);
$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(3.86);
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(56.86);
$spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(5);
$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(10.86);
$spreadsheet->getActiveSheet()->getColumnDimension('E')->setWidth(10.86);
$spreadsheet->getActiveSheet()->mergeCells('A1:D1');
$spreadsheet->getActiveSheet()->setCellValue('A1', 'HEADER row1');
$spreadsheet->getActiveSheet()->mergeCells('A2:D2');
$spreadsheet->getActiveSheet()->setCellValue('A2', 'HEADER row2');
$spreadsheet->getActiveSheet()->mergeCells('A3:E3');
$spreadsheet->getActiveSheet()->setCellValue('A3', 'TITLE');
$styleArray = array(
'font' => array(
'bold' => true,
'color' => array('rgb' => '000000'),
'size' => 14,
'name' => 'Verdana'
),
'alignment' => array(
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
)
);
$spreadsheet->getActiveSheet()->getStyle('A3')->applyFromArray($styleArray);
$spreadsheet->getActiveSheet()->mergeCells('A4:E4');
$spreadsheet->getActiveSheet()->setCellValue('A4', $lastDayOfCurrentMonth);
$spreadsheet->getActiveSheet()->getStyle('A4')->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER)));
$spreadsheet->getActiveSheet()->mergeCells('A5:B5');
$spreadsheet->getActiveSheet()->setCellValue('A5', "code 02");
$spreadsheet->getActiveSheet()->setCellValue('E5', "- euro -");
$spreadsheet->getActiveSheet()->setCellValue('A6', 'ID');
$spreadsheet->getActiveSheet()->setCellValue('B6', 'DESCRTIPTION');
$spreadsheet->getActiveSheet()->setCellValue('C6', 'Code');
$spreadsheet->getActiveSheet()->setCellValue('D6', 'Value1');
$spreadsheet->getActiveSheet()->setCellValue('E6', 'Value2');
$spreadsheet->getActiveSheet()->getRowDimension(6)->setRowHeight(40);
$styleArrayTabel = array(
'alignment' => array(
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
'rotation' => 0,
'wrap' => true
),
'borders' => array(
'allBorders' => array(
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN, //BORDER_THIN BORDER_MEDIUM BORDER_HAIR
'color' => array('rgb' => '000000')
)
)
);
$spreadsheet->getActiveSheet()->getStyle('A6:E6')->applyFromArray($styleArrayTabel);
$spreadsheet->getActiveSheet()->setCellValue('A7', 'A');
$spreadsheet->getActiveSheet()->setCellValue('B7', 'B');
$spreadsheet->getActiveSheet()->setCellValue('C7', 'C');
$spreadsheet->getActiveSheet()->setCellValue('D7', 'D');
$spreadsheet->getActiveSheet()->setCellValue('E7', 'E');
$spreadsheet->getActiveSheet()->getStyle('A7:E7')->applyFromArray($styleArrayTabel);
$baseRow = 9;
foreach($resultset as $r => $dataRow) {
$row = $baseRow + $r;
$spreadsheet->getActiveSheet()->insertNewRowBefore($row,1);
$spreadsheet->getActiveSheet()->setCellValue('A'.$row, $dataRow['id'])
->setCellValue('B'.$row, $dataRow['description'])
->setCellValue('C'.$row, $dataRow['code'])
->setCellValue('D'.$row, $dataRow['value1'])
->setCellValue('E'.$row, $dataRow['value2']);
$spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight(-1);
$spreadsheet->getActiveSheet()->getStyle('A'.$row.':E'.$row)->applyFromArray(array('borders' => array('allborders' => array('style' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN))));
$spreadsheet->getActiveSheet()->getStyle('A'.$row)->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER)));
$spreadsheet->getActiveSheet()->getStyle('B'.$row)->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT,'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,'wrapText' => true)));
$spreadsheet->getActiveSheet()->getStyle('C'.$row)->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER)));
$spreadsheet->getActiveSheet()->getStyle('D'.$row)->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER)));
$spreadsheet->getActiveSheet()->getStyle('D'.$row)->getNumberFormat()->setFormatCode('#0');
$spreadsheet->getActiveSheet()->getStyle('E'.$row)->applyFromArray(array('alignment' => array('horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER)));
$spreadsheet->getActiveSheet()->getStyle('E'.$row)->getNumberFormat()->setFormatCode('#0');
}
$spreadsheet->getActiveSheet()->removeRow($baseRow-1,1);
$spreadsheet->getActiveSheet()->setTitle('Anexa2');
$spreadsheet->getActiveSheet()->setShowGridLines(false);
$spreadsheet->setActiveSheetIndex(0);
$helper->log('Hide grid lines');
$spreadsheet->getActiveSheet()->setShowGridLines(false);
$helper->log('Set orientation to landscape');
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
if($fileformat == 'pdf')
{
$helper->log('PDF file format selected');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Mpdf");
$writer->save("aaa.pdf");
exit();
} elseif ($fileformat == 'xlsx')
{
$helper->log('XLSX file format selected');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$writer->save("aaa.xlsx");
exit();
}
else { $helper->log('no file format selected'); }
?>
删除 setShowGridLines(false) 解决了 mPDF 渲染问题
//$spreadsheet->getActiveSheet()->setShowGridLines(false);