简体   繁体   中英

Symfony2: How do i export the SonataAdminBundle listing data to excel sheet?

I need to export sonata admin data via excel or pdf. How can i be able to achieve this? Is there any bundle that helps me do that?

Any ideas!?

Thanks.

You have to override the admin method. 方法。

If the entity has some (X)-to-many or many-to-(X) relations you have to override all the fields

public function getExportFields()
{
     $exportFields = parent::getExportFields(); //It's not working in case of *-to many relation, so remove it, but it's ok for small customisation

     $exportFields["Date"] = 'date';             //if your date is an instance of DateTime, you have to format it
     $exportFields["User name"] = 'user.name';   // in case of *-to-many relations
     $exportFields["Status"] = 'statusAsString'; //for customise format implement getStatusAsString() in you entity
     return $exportFields;
}

To customise the type of report to download you have to override, removing the default one:

public function getExportFormats()
{
    return array(
        'json', 'xml', 'csv', 'xls'
    );
}

I've implemented Bundle for Symfony and Sonata to export lists to PDF. You could get it here: https://github.com/whyte624/sonata-admin-extra-export-bundle . It is based on solution suggested here: http://cristian-radulescu.ro/article/add-pdf-export-functionality-in-sonataadminbundle.html . I know that the question is quite old, but hopefully it helps someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM