flowcode/excel-service-provider

为 Silex 提供的 Excel ServiceProvider。

dev-master / 1.0.x-dev 2014-11-22 23:53 UTC

This package is not auto-updated.

Last update: 2024-09-24 15:47:44 UTC


README

Silex 提供的 Excel ServiceProvider。

此包基于 https://github.com/liuggio/ExcelBundle - 代码的许多部分直接从中复制。此包将 PHPExcel 集成作为 Silex 服务提供者实现,而不是作为 Symfony2 组件。

我只实现了 Excel5 支持,并且我怀疑我不会实现 liuggio 版本提供的其他导出格式,因为我根本不需要它们。如果您有兴趣实现其他格式,请随意分支或提交补丁。

安装

1 在 composer.json 的 'require' 部分添加

    "require" : {
        "jaza/excel-service-provider": "1.0.*@dev",
    }

2 注册提供者

$app->register(new Jaza\Silex\ExcelServiceProvider());

用法

在 Silex 回调函数内部或其它地方

$excelService = $app['xls.service_xls5'];

$excelService->excelObj->getProperties()->setCreator("Me")
                       ->setLastModifiedBy("Me")
                       ->setTitle("Test Document")
                       ->setSubject("Test Document")
                       ->setDescription("Testing a document.")
                       ->setKeywords("testdoc")
                       ->setCategory("Test doc");
    
$excelService->excelObj->setActiveSheetIndex(0)
             ->setCellValue('A1', 'Hello')
             ->setCellValue('B2', 'world!');

$excelService->excelObj->getActiveSheet()->setTitle('Simple');
$excelService->excelObj->setActiveSheetIndex(0);

$response = $excelService->getResponse();
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Content-Disposition', 'attachment;filename=test.xls');

// If you are using a https connection, you have to set those two headers for compatibility with IE <9
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');

return $response;