我安装了这个包:
https://github.com/liuggio/ExcelBundle
在我的 Symfony2 中,基本上添加:
[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
到 deps,更新 Autoload.php 和 AppKernel.php 并在一个控制器中使用此操作:
$excelService = $this->get('xls.service_xls2007');
$excelService->excelObj->getProperties()
->setTitle("Reunión comercial")
->setDescription("Reunión Comercial - Generación automática desde la aplicación de RRHH.");
$excelService->excelObj->getActiveSheet()->setTitle('Reunión Comercial');
// 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=reuComercial-'.date("Y_m_d_His").'.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');
*/
$excelService->setActiveSheetIndex(0)
->setCellValue('A1', 'Lead')
->setCellValue('B1', 'Peticion')
->setCellValue('C1', 'Estado Candidato')
->setCellValue('D1', 'Nombre Candidato')
->setCellValue('E1', 'Apellido Candidato')
->setCellValue('F1', 'Cliente')
->setCellValue('G1', 'Descripcion');
$em = $this->getDoctrine()->getEntityManager();
$data = $em->getRepository('OportunidadBundle:Oportunidad')->reunionComercial();
$aux = 2;
foreach ($data as $row)
{
$excelService->setActiveSheetIndex(0)
->setCellValue('A'.$aux, $row->Lead)
->setCellValue('B'.$aux, $row->Peticion)
->setCellValue('C'.$aux, $row->Estado_Candidato)
->setCellValue('D'.$aux, $row->Nombre_Candidato)
->setCellValue('E'.$aux, $row->Apellido_Candidato)
->setCellValue('F'.$aux, $row->Cliente)
->setCellValue('G'.$aux, $row->Descripcion);
$aux++;
};
$excelService->getStreamWriter()->write( $filename );
return $response;
我得到这个错误:(
Runtime Notice: Declaration of PHPExcel_Style_Color::bindParent() should be compatible with that of PHPExcel_Style_Supervisor::bindParent() in C:\Program Files\EasyPHP-5.3.8.1\www\www\www\intranet\vendor\phpexcel\Classes\PHPExcel\Style\Color.php line 36
我不知道这意味着什么,因为 PHP 中有很多东西仍然让我无法理解:/