我遇到以下代码的问题:
/**
* @Route("/dl", name="dl_xls")
* @Method({"GET"})
*/
public function exportXLS()
{
$em = $this->getDoctrine()->getManager();
$query = $em->getRepository('App:Post')
->createQueryBuilder('u')
->select('u.title')
->getQuery();
$result = $query->getResult();
$phpExcelObject = $this->get('phpspreadsheet')->createSpreadsheet();
$row = 1;
foreach ($result as $item) {
$phpExcelObject->setActiveSheetIndex(0)
->setCellValue('A' . $row, $item['title']);
$row++;
}
$writer = $this->get('phpspreadsheet')->createWriter($phpExcelObject, 'Csv');
/*var_dump($writer);*/
$writer->save("php://output");
$response = $this->get('phpspreadsheet')->createStreamedREsponse($writer);
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'resultat.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;
}
此代码为我提供了一个xls文件,其中包含我的数据库结果。问题在于它给了我两次结果。
我无法预先知道如何解决它,坦克提前为你提供帮助。
ps:我已经检查了我的数据库,我的条目没有重复。