awurth/excelbundle

此包已被弃用,不再维护。未建议替换包。

这是一个 Symfony2 Bundle,可以帮助您读取和写入 Excel 文件(包括 pdf、xlsx、odt),得益于 PHPExcel 库。

安装: 333

依赖者: 0

建议者: 0

安全: 0

星级: 0

关注者: 1

分支: 123

类型:symfony-bundle

v2.1.1 2017-11-30 20:01 UTC

README

此包允许您创建、修改和读取 Excel 对象。

Build Status Total Downloads Latest Stable Version Latest Unstable Version

许可证

License

版本 2

这是全新的版本。与 1.* 版本存在重大 BC 问题,但 单元测试功能测试新工厂 非常容易使用。

版本 1.*

如果您已安装旧版本,并且愿意使用它,您可以在 tag v1.0.6 中找到文档和文件,浏览代码

注意事项

CSV 更快,如果您必须创建简单的 xls 文件,我建议您使用 csv 的内置函数:https://php.ac.cn/manual-lookup.php?pattern=csv&lang=en&scope=quickref

安装

1 将以下内容添加到 composer.json 的 require

    $composer require liuggio/excelbundle

2app/AppKernel.php 中注册包

    $bundles = array(
        // ...
        new Liuggio\ExcelBundle\LiuggioExcelBundle(),
    );

TL;DR

  • 创建一个空对象
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
  • 从文件创建对象
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject('file.xls');
  • 创建一个 Excel5 并将对象写入文件
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
$writer->save('file.xls');
  • 创建一个 Excel5 并创建 StreamedResponse
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
$response = $this->get('phpexcel')->createStreamedResponse($writer);
  • 创建带有图像的 Excel 文件
$writer = $this->get('phpexcel')->createPHPExcelObject();
$writer->setActiveSheetIndex(0);
$activesheet = $writer->getActiveSheet();

$drawingobject = $this->get('phpexcel')->createPHPExcelWorksheetDrawing();
$drawingobject->setName('Image name');
$drawingobject->setDescription('Image description');
$drawingobject->setPath('/path/to/image');
$drawingobject->setHeight(60);
$drawingobject->setOffsetY(20);
$drawingobject->setCoordinates('A1');
$drawingobject->setWorksheet($activesheet)

不仅仅是 'Excel5'

类型列表如下

  1. 'Excel5'
  2. 'Excel2007'
  3. 'Excel2003XML'
  4. 'OOCalc'
  5. 'SYLK'
  6. 'Gnumeric'
  7. 'HTML'
  8. 'CSV'

示例

伪控制器

开始的最佳地方是位于 Tests/app/Controller/FakeController.php 的伪控制器,这是一个工作示例。

更多示例

您可以在官方 PHPExcel 存储库中找到很多示例:https://github.com/PHPOffice/PHPExcel/tree/develop/Examples

对于懒惰的开发者

namespace YOURNAME\YOURBUNDLE\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

class DefaultController extends Controller
{

    public function indexAction($name)
    {
        // ask the service for a Excel5
       $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();

       $phpExcelObject->getProperties()->setCreator("liuggio")
           ->setLastModifiedBy("Giulio De Donato")
           ->setTitle("Office 2005 XLSX Test Document")
           ->setSubject("Office 2005 XLSX Test Document")
           ->setDescription("Test document for Office 2005 XLSX, generated using PHP classes.")
           ->setKeywords("office 2005 openxml php")
           ->setCategory("Test result file");
       $phpExcelObject->setActiveSheetIndex(0)
           ->setCellValue('A1', 'Hello')
           ->setCellValue('B2', 'world!');
       $phpExcelObject->getActiveSheet()->setTitle('Simple');
       // Set active sheet index to the first sheet, so Excel opens this as the first sheet
       $phpExcelObject->setActiveSheetIndex(0);

        // create the writer
        $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
        // create the response
        $response = $this->get('phpexcel')->createStreamedResponse($writer);
        // adding headers
        $dispositionHeader = $response->headers->makeDisposition(
            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
            'stream-file.xls'
        );
        $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
        $response->headers->set('Pragma', 'public');
        $response->headers->set('Cache-Control', 'maxage=1');
        $response->headers->set('Content-Disposition', $dispositionHeader);

        return $response;        
    }
}

贡献者

贡献者列表:https://github.com/liuggio/ExcelBundle/graphs/contributors

贡献

  1. 分支项目
  2. 克隆仓库
  3. 获取编码标准修复工具:wget http://cs.sensiolabs.org/get/php-cs-fixer.phar
  4. 在提交PullRequest之前,您应该使用php php-cs-fixer.phar fix -v .运行编码标准修复工具。