pdfloresjdav / excelbundle
这是一个帮助您读取和写入 Excel 文件(包括 pdf、xlsx、odt)的 Symfony2 Bundle,得益于 PHPExcel 库。
Requires
- php: >=5.3.2
- irongit/symfony2-stream-response: >=1.0
- pdfloresjdav/phpexcel: dev-master
- symfony/framework-bundle: 2.*
This package is not auto-updated.
Last update: 2024-09-21 21:09:28 UTC
README
此 Bundle 允许您创建一个易于修改的 Excel 对象。这只是一个依赖注入,它链接
3 个对象
-
此 Bundle 中的容器
-
n3bStreamresponse 中的 StreamWrapper
-
一个写入器。
您可以创建自己的写入器,通过扩展 n3b\Bundle\Util\HttpFoundation\StreamResponse\StreamWriterInterface,或者您可以使用出色的 PHPExcel 库。使用 PHPExcel,您可以创建 xls、ods、pdf 以及更多。
您需要知道 csv 速度更快,因此我鼓励您使用内置的 csv 功能: http://it.php.net/manual-lookup.php?pattern=csv&lang=en&scope=quickref
迁移
为了遵循命名约定 https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md,所有 liuggio 命名空间都已迁移到 Liuggio。
此 master 已更新到 symfony/symfony master,目前为 2.1。
使用 COMPOSER 安装
1 将以下内容添加到 composer.json 中的 require 键
"require" : {
"liuggio/excelbundle": ">=1.0.4",
}
2 在 app/AppKernel.php 中注册该 Bundle
$bundles = array( // ... new Liuggio\ExcelBundle\LiuggioExcelBundle(), );
使用 deps 文件安装
1 将以下内容添加到您的 deps 文件中,然后运行 php bin/vendors install
[PHPExcel] git=http://github.com/PHPOffice/PHPExcel.git target=/phpexcel version=origin/master [n3bStreamresponse] git=git://github.com/liuggio/Symfony2-StreamResponse.git target=n3b/src/n3b/Bundle/Util/HttpFoundation/StreamResponse [LiuggioExcelBundle] git=https://github.com/liuggio/ExcelBundle.git target=/bundles/Liuggio/ExcelBundle
2 在 app/autoload.php 中注册命名空间和前缀
$loader->registerNamespaces(array( // ... 'n3b\\Bundle\\Util\\HttpFoundation\\StreamResponse' => __DIR__.'/../vendor/n3b/src', 'Liuggio' => __DIR__.'/../vendor/bundles', )); $loader->registerPrefixes(array( // ... 'PHPExcel' => __DIR__.'/../vendor/phpexcel/Classes', ));
3 在 app/AppKernel.php 中启用该 Bundle
$bundles = array( // ... new Liuggio\ExcelBundle\LiuggioExcelBundle(), );
可用服务
如果您想写入
// create MS Excel5 $excelService = $this->get('xls.service_xls5'); // create pdf $this->get('xls.service_pdf'); // create MS Excel 2007 $this->get('xls.service_xls2007');
如果您想读取 xls
$excelObj = $this->get('xls.load_xls5')->load($filename);
用法
在您的 Bundle 中创建一个控制器
namespace YOURNAME\YOURBUNDLE\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class DefaultController extends Controller { public function indexAction($name) { // ask the service for a Excel5 $excelService = $this->get('xls.service_xls5'); // or $this->get('xls.service_pdf'); // or create your own is easy just modify services.yml // create the object see http://phpexcel.codeplex.com documentation $excelService->excelObj->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->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"); $excelService->excelObj->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B2', 'world!'); $excelService->excelObj->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $excelService->excelObj->setActiveSheetIndex(0); //create the response $response = $excelService->getResponse(); $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8'); $response->headers->set('Content-Disposition', 'attachment;filename=stdream2.xls'); // If you are using a https connection, you have to set those two headers and use sendHeaders() for compatibility with IE <9 $response->headers->set('Pragma', 'public'); $response->headers->set('Cache-Control', 'maxage=1'); return $response; } }
使用正确的写入器(例如 PHPExcel_Writer_Excel5),您还可以将输出写入文件
public function indexAction($name) { $excelService = $this->get('xls.service_xls5'); //...create php excel object $excelService->getStreamWriter()->write( $filename ); }
高级用法
如果您需要,请查看和修改 Liuggio\ExcelBundle\Resources\config\services.yml
贡献者
@pivasyk
@dirkbl
@DerStoffel
@artturi
@isqad88
@mazenovi
@jochenhilgers
@Squazic