Laravel PHPExcel - 无法访问某些方法

Laravel PHPExcel - No access to certain methods

我正在尝试使用 Laravel 中的 excel 模板文件来创建新文件。我似乎无法调用某些方法并得到如下错误:

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

我可以用数据填充单元格,但似乎无法更改任何格式。

代码如下所示:

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)

谁能告诉我我做错了什么?

PhpExcel 已移至 PhpSpreadsheet,不再包含方法 setColumnFormat,但您可以这样做:

$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);

您可以在下一页获得更多信息,也可以查看他们的 github 源代码,了解您可以在 Worksheet/Spreadsheet 上调用哪些函数。 Official docs

编辑: 对于您的特定情况,您可以使用

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

您可以使用的可用日期常量