sostheng/entity-portation-bundle

这是一个允许您使用PHPExcel简单导出实体数组的包

安装: 32

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master 2017-05-25 15:34 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:49:41 UTC


README

这是一个允许您使用PHPExcel简单导出实体数组的包。

只需在您想要导出的类的元素上放置注解,EntityPortationBundle就会完成其余工作!传递要导出的实体列表并享受吧!

目前,只提供基本导出功能,但读取CSV/Excel文件并从中创建实体是计划中的。

请注意,此包仍在开发中,可能存在一些错误或奇怪的行为。请随时发送您的反馈!

许可证

License

安装

1 将其添加到composer.json

    $composer require sostheng/entity-portation-bundle

2app/AppKernel.php中注册该包

    $bundles = array(
        // ...
        new SosthenG\EntityPortationBundle\EntityPortationBundle(),
    );

如何使用

此包使用注解来检测需要导出什么。要使用注解,您必须包含这两个类

use SosthenG\EntityPortationBundle\Annotation\EntityPortation; // To pass custom parameters for the export
use SosthenG\EntityPortationBundle\Annotation\PortationGetter; // Required to tell which getter will be portable

以下是一个所有参数都已填充的示例

/**
 * @EntityPortation(csvDelimiter=";", sheetTitle="My sheet Title", fallBackValue="N/A")
 */
 class MyClass { 
    private $field;
 
    /**
     * @PortationGetter(label="Rôles", position="auto", visible=true, valueType="string")
     */
    public function getField()
    {
        return $this->_field;
    }
 }

它们都是可选的,唯一必需的事情是在您想要用于导出的getter上使用@PortationGetter()注解。

您已经完成了最难的部分!现在,只需创建一个Export对象,添加或更改一些额外参数(如果需要),然后获取您的导出文件!

    /**
     * @Route("export/{format}", name="export", requirements={"format" = "PDF|Excel2007|Excel5|CSV|HTML|OpenDocument"})
     */
    public function exportAction($format) {
        $entities = $this->getDoctrine()->getManager()->getRepository("Bundle:MyClass")->findAll();

        $exporter = new Export($this->get("phpexcel"), $this->get("translator"));

        $exporter->setEntities($entities);
        
        // You can save the export as file or get a response from now, but if you want, you can change some parameters
        
        $prop = $exporter->getProperties(); // Returns the PhpExcel Properties object.
        $prop->setAuthor("You"); // Check the PHPExcel documentation for other parameters
        
        $exporter->setAllVisible(true);
        // etc.
        
        // Save the file
        $file = $exporter->saveAsFile($format, "web/files/myFileName"); // Extension is optionnal, it will be added if not filled
        // or return it as a Response
        return $exporter->getResponse($format, "myFileName");
    }

一些文档

此包使用liuggio的ExcelBundle,这是一个PhpExcel集成包,用于Symfony。

此包的文档

暂不可用。让我先完成它:)