phpspreadsheet setFormatCode not working

我只是一个虾纸丫 提交于 2019-12-11 09:26:10

问题


Sometimes I'm unable to format an Excel cell data as date using $date wiht format 'yyyy-mm-dd' (eg. 2017-07-12)

if ($date != '') {
     $t_date   = PhpOffice\PhpSpreadsheet\Shared\Date::stringToExcel($date);
     $sheet->setCellValueByColumnAndRow($column,$row, $t_date);
     $sheet->getStyleByColumnAndRow($column,$row)->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DDMMYYYY);
  }

回答1:


The previous code fails when a $date is not valid (eg. 0000-00-00), and keeps failing in all sequent applies.

My solution is

if ($date != '') {
     $t_date   = PhpOffice\PhpSpreadsheet\Shared\Date::stringToExcel($date);
     if ($t_date !== false) {
        $sheet->setCellValueByColumnAndRow($column,$row, $t_date);
        $sheet->getStyleByColumnAndRow($column,$row)->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DDMMYYYY);
        $sheet->getStyleByColumnAndRow($column,$row)->getFont()->setBold(true);
        $sheet->getStyleByColumnAndRow($column,$row)->getFont()->setBold(false);
     }
  }

Settting and unsetting the bold stile kwwpd working the setFormatCode in moste fo the cases ... I do not know why.



来源:https://stackoverflow.com/questions/47883697/phpspreadsheet-setformatcode-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!