jaza/excel-service-provider

为Silex提供Excel ServiceProvider。

dev-master / 1.0.x-dev 2013-02-13 01:57 UTC

This package is not auto-updated.

Last update: 2024-09-28 14:44:13 UTC


README

Silex提供的Excel ServiceProvider。

此包基于https://github.com/liuggio/ExcelBundle - 代码的许多部分直接从中复制。此包将PHPExcel集成作为Silex Service Provider,而不是作为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;