PhpSpreadsheet在XLSX中具有边框,但在具有mPDF类的PDF中没有边框

时间:2018-09-09 19:45:43

标签: pdf border xlsx mpdf phpspreadsheet

我正在尝试使用phpspreadsheet生成两种格式(xlsx和pdf)的报告。当我运行代码并将其保存在xlsx中时,文档中的表格带有边框,但是当我将其保存为pdf时则没有边框。所有其他事物都在两者中生成。 首先,我使用了旧版本的phpexcel v5.x和mpdf v.5x和php v5.6.x 我已经迁移到PHP 7.1; mpdf 7x phpspreadsheet 7x,希望能解决问题。 还尝试了不同的边框样式BORDER_THIN BORDER_MEDIUM BORDER_HAIR,但没有运气。

有人知道我在做什么错吗?

phpspreadsheet mpdf no border

    <?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'); }

?>

1 个答案:

答案 0 :(得分:0)

删除setShowGridLines(false)可解决mPDF渲染的问题

//$spreadsheet->getActiveSheet()->setShowGridLines(false);