0

I am trying to use an excel template file in Laravel to create new files. I seem to not be able to call certain methods and get errors like:

PHP Error:  Call to undefined method PHPExcel_Worksheet::cells()

I can populate cells with data, but don't seem to be able to change any formatting.

Code looks like this:

Excel::load(storage_path('templates/consolidatedInvoice.xlsx'), function($excel) use($name, $invoice, $customer, $imported){
        $excel->setActiveSheetIndex(0);
       // deal with dates
        $excel->getActiveSheet()
        ->setColumnFormat(array(
            'E3' => 'dd-mmm-yyyy'));
        $invDate = $invoice->ADPConsolidatedInvoice->importedInvoices->first();
        $excel->getActiveSheet()
              ->setCellValue('E3', $invDate->formattedInvoiceDate)
              ->setCellValue('E4', $invoice->invoice_number)

Can anyone tell me what I am doing wrong?

Andy P.
  • 29
  • 5

1 Answers1

0

PhpExcel has moved to PhpSpreadsheet and no longer contains the method setColumnFormat but instead you can do something like this:

$styleArray = array(
   'font' => array(
        'bold' => true,
    ),
    'alignment' => array(
        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
    ),
    'borders' => array(
        'top' => array(
            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
        ),
    ),
    'fill' => array(
        'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
        'rotation' => 90,
        'startColor' => array(
            'argb' => 'FFA0A0A0',
        ),
        'endColor' => array(
            'argb' => 'FFFFFFFF',
        ),
    ),
);
$excel->getActiveSheet()->getStyle("E3")->applyFromArray($styleArray);
//or individually
$excel->getActiveSheet()->getStyle("E3")->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);

You can get some more info on the following page and take a look at their github source code too to find out what functions you can call on Worksheet/Spreadsheet. Official docs

EDIT: For your particular case you can use

 $spreadsheet->getActiveSheet()->getStyle("E3")
 ->getNumberFormat()
 ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);

Available constants for date that you can use

enter image description here

Nikola Gavric
  • 3,507
  • 1
  • 8
  • 16