totalcrm / excel-bundle
这是一个Symfony4 Bundle,帮助您读取和写入Excel文件(包括pdf、xlsx、odt),归功于PHPExcel库
v2.2.5
2022-01-26 14:18 UTC
Requires
- php: >=7.2
- phpoffice/phpexcel: ~1.8.1
Requires (Dev)
- symfony/browser-kit: ~3.0|~4.0|~5.0
- symfony/class-loader: ~3.0|~4.0|~5.0
- symfony/finder: ~3.0|~4.0|~5.0
- symfony/form: ~3.0|~4.0|~5.0
- symfony/validator: ~3.0|~4.0|~5.0
README
需要注意的事项
CSV更快,如果您需要创建简单的xls文件,我建议您使用csv的内置功能: https://php.ac.cn/manual-lookup.php?pattern=csv&lang=en&scope=quickref
安装
1 将以下内容添加到composer.json的require
键中
$composer require totalcrm/excel-bundle
2 在app/AppKernel.php
中注册bundle
$bundles = array( // ... new TotalCRM\ExcelBundle\TotalCRMExcelBundle(), );
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'
类型列表如下
- 'Excel5'
- 'Excel2007'
- 'Excel2003XML'
- 'OOCalc'
- 'SYLK'
- 'Gnumeric'
- 'HTML'
- '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("totalcrm") ->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; } }
贡献
- 创建项目分支
- 克隆仓库
- 获取编码标准修复器:
wget http://cs.sensiolabs.org/get/php-cs-fixer.phar
- 在PullRequest之前,您应该运行编码标准修复器:
php php-cs-fixer.phar fix -v .