11ya/excelbundle
这是一个Symfony2 Bundle,帮助您读取和写入Excel文件(包括pdf、xlsx、odt),归功于PHPExcel库
Requires
- php: >=5.3.2
- 11ya/phpexcel: 2.*@dev
- irongit/symfony2-stream-response: dev-master
- symfony/framework-bundle: 2.*
This package is not auto-updated.
Last update: 2024-09-24 03:46:04 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 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