Excel export date field with phpspreadsheet extension

Hi, I want to export a date field from a view to Excel, but it comes up there as text with a ' in front. I'm using the PhpSpreadsheet extension, and as recommended by support, I also created a custom file, but I can't get it to work. Does anyone have an idea and can help me? Custom File: MyExportExcel.php also ends up in the src directory, but it doesn't get executed at all, a log output in there doesn't come through. Cheers, Jürgen

You may post your custom export class for discussion.

I created this custom file, the support suggested it, I got the content from Google, I’m not very familiar with it. It also ends up in the src directory. But nothing changes, I don’t even get the impression that it’s being called.

namespace PHPMaker2026\\Pwarneken;

use PhpOffice\\PhpSpreadsheet;

class MyExportExcel extends ExportExcel

public function exportValueBy(DbField $fld, int $col, int $row): void
{
if ($fld->FieldName == 'Montagetermin' && $this->RowType > 0) {

    if ($fld->FieldName == 'Montagetermin' && $value !== null && $value !== '') {
        $cell = $this->ActiveSheet->getCell($this->ExportColumnIndex); // Zellreferenz ggf. anpassen
        $excelDate = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($value);
        $cell->setValueExplicit($excelDate, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC);
        $cell->getStyle()->getNumberFormat()->setFormatCode('DD.MM.YYYY');
        return;
    }
    parent::exportValueBy($fld, $value, $row);
}

}

Thank you.

You may refer to Customizing Export (v2021). (Note also that you have wrong syntax - missing { and } for the class.)

It works with a custom file MyExportExcel.php which ends up in src and an export.php, which I saved in config/extensions.

MyExportExcel.php:

<?php
namespace PHPMaker2026\\Pwarneken;
use PhpOffice\\PhpSpreadsheet;
class MyExportExcel extends ExportExcel
{
    public function exportValueBy(DbField $fld, int $col, int $row): void
    {
#        $logDir = dirname(\__DIR_\_) . '/var/log';
#        if (is_dir($logDir) && is_writable($logDir)) {
#            file_put_contents($logDir . '/myexport_debug.log', date('c') . " Feld={$fld->Name} RowType={$this->RowType}\\n", FILE_APPEND);
#        }
        if ($fld->Name == 'Montagetermin' && $this->RowType > 0) {
            $value = $fld->CurrentValue;
            if ($value !== null && $value !== '') {
                $sheet = $this->getActiveSheet();
                $letter = PhpSpreadsheet\\Cell\\Coordinate::stringFromColumnIndex($col);
                $cell = $sheet->getCell($letter . $row);
                $dt = $value instanceof \\DateTimeInterface ? $value : new \\DateTime((string)$value);
                $excelDate = PhpSpreadsheet\\Shared\\Date::PHPToExcel($dt);
                $cell->setValueExplicit($excelDate, PhpSpreadsheet\\Cell\\DataType::TYPE_NUMERIC);
                $cell->getStyle()->getNumberFormat()->setFormatCode('DD.MM.YYYY');
                return;
            }
        }
        parent::exportValueBy($fld, $col, $row);
    }
}

export.php:

<?php
declare(strict_types=1);
namespace PHPMaker2026\Pwarneken;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
    $services = $containerConfigurator->services()
        ->defaults()
        ->autowire()
        ->autoconfigure()
        ->public();
    $services->set(MyExportExcel::class);
    ...

You may simply replace the export class in Global Code: (no need to add export.php)

// Replace the default ExportExcel class 
Config("EXPORT_CLASSES.excel",  "MyExportExcel"); // Note: For tables only. This example is not applicable to reports.