我有一些从DataBase获取数据的路由,打开template-file.xlsx并放入该数据,然后发送创建对autodownloaded .xlsx文件的响应。
在localhost工作从未遇到任何问题,但今天部署了项目,一切正常,除了路线。
它生成了一个result-xlsx文件,但是当我尝试打开它时,它被视为一个xml,它都是随机符号等等。
我使用PHPExcel捆绑包用于Symfony,它有一些PHPExcel库的快捷方式,但实际上完全相同。
我确定我的代码中没有问题,但我会在下面提供:
我的代码:
/**
* @Route("/download-til/{id}", name="downloadTIL")
* @Method({"GET","HEAD", "POST"})
*/
public function downloadTILAction(Request $request, $id, Loan $loan)
{
if($loan->getLine()->getDosier()->getUserId() != $this->getUser()){
throw $this->createNotFoundException("Access denied"); }
$query = $this->getDoctrine()
->getRepository('AppBundle:Loan')
->find($id);
$em = $this->getDoctrine()->getManager();
$query1 = $em->createQuery(
'SELECT p
FROM AppBundle:Charge p
WHERE p.loanId = :loanId
AND p.isActive = true
')->setParameter('loanId', $id);
$query2 = $em->createQuery(
'SELECT p
FROM AppBundle:Payment p
WHERE p.loanId = :loanId
')->setParameter('loanId', $id);
$query3 = $em->createQuery(
'SELECT d
FROM AppBundle:Deferral d
WHERE d.loanId = :loanId
')->setParameter('loanId', $id);
$query4 = $em->createQuery(
'SELECT p
FROM AppBundle:Charge p
WHERE p.loanId = :loanId
AND p.isActiveTEG = true
')->setParameter('loanId', $id);
$query5 = $em->createQuery(
'SELECT p
FROM AppBundle:Charge p
WHERE p.loanId = :loanId
')->setParameter('loanId', $id);
$calculator = new Calculator(
$query->getDate(),
$query->getCapital(),
$query->getRate(),
$query->getFrequency(),
$query->getDuration(),
$query->getType(),
$query1->getArrayResult(),//charges with active SCH
$query2->getArrayResult(),//forced payments
$query3->getArrayResult(),//deferrals
$query4->getArrayResult(),//charges with active BANK
$query5->getArrayResult(),//get absolute all charges(active and inactive), to sort them in Calculator, and return to view
$query->getForcedTIL()
);
$array = $calculator->getArray();
$chargesArray = $calculator->getChargeTableArray();
$dur = $query->getDuration();
// ask the service for a Excel5
$dir = $this->get('kernel')->getRootDir();
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($dir.'/OutputTIL.xlsx');
$phpExcelObject->getProperties()->setCreator("liuggio")
->setLastModifiedBy("Expertiseur")
->setTitle("Office 2005 XLSX Test Document")
->setSubject("Office 2005 XLSX Test Document")
->setDescription("Raport")
->setKeywords("office 2005 openxml php")
->setCategory("Analyse result file");
$phpExcelObject->setActiveSheetIndex(0)
->setCellValue('B3', $query->getCapital())
->setCellValue('E3', $dur)
->setCellValue('H3', $array[0]['Date'])
->setCellValue('K3', $array[0]['Rate']/100)
->setCellValue('B12', $array[0]['Date'])
->setCellValue('I12', $array[$dur-1]['Date'])
->setCellValue('D12', $chargesArray[1000]['bankInterest1'])
->setCellValue('E12', $chargesArray[1000]['legalInterest1'])
->setCellValue('E13', $chargesArray[1000]['currentReparation1'])
->setCellValue('J12', $chargesArray[1000]['bankInterest2'])
->setCellValue('K12', $chargesArray[1000]['legalInterest2'])
->setCellValue('K13', $chargesArray[1000]['currentReparation2'])
->setCellValue('H15', $chargesArray[1000]['reparationGlobalPotential']);
for($i = 20, $j = $dur; $j < $dur*2; $i++,$j++) {
$phpExcelObject->setActiveSheetIndex(0)
->setCellValue('A'.$i, $i-19)
->setCellValue('B'.$i, $array[$i-20]['Date'])
->setCellValue('C'.$i, $array[$i-20]['Capital'])
->setCellValue('D'.$i, $array[$i-20]['Rate']/100)
->setCellValue('E'.$i, $array[$i-20]['Interest'])
->setCellValue('F'.$i, $array[$i-20]['Payment'])
->setCellValue('H'.$i, $array[$j]['CapitalTIL'])
->setCellValue('I'.$i, $array[$j]['RateTIL']/100)
->setCellValue('J'.$i, $array[$j]['InterestTIL'])
->setCellValue('K'.$i, $array[$j]['PaymentTIL'])
;
}
$BStyle = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)));
$highestRow = $phpExcelObject->setActiveSheetIndex(0)->getHighestDataRow();
$phpExcelObject->setActiveSheetIndex(0)->getStyle("A20:K{$highestRow}")->applyFromArray($BStyle);
$phpExcelObject->getActiveSheet()->setTitle('Output TIL');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
// create the writer
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel2007');
// create the response
$response = $this->get('phpexcel')->createStreamedResponse($writer);
// adding headers
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'4 Calcul au tx legal.xlsx'
);
$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;
}//downloadTILAction
导致该错误的原因是什么?谢谢!
答案 0 :(得分:1)
选择正确的Content-Type
:
对于BIFF .xls文件
application/vnd.ms-excel
对于Excel2007及更高版本的.xlsx文件
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
更改此行:
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
到此:
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
作为奖励,您没有提出问题的答案:
可能甚至不知道,你正在使用type hinting内置的Doctrine转换器。这使得这些线条毫无用处:
$query = $this->getDoctrine()
->getRepository('AppBundle:Loan')
->find($id);
只需使用$loan->getDate()
即可。而不是$query* = $em->createQuery()
,您可以使用关联。如果您的Association Mapping是正确的(如果没有,请修复它!),您可以使用$loan->getPayments()
。你正在使用一个非常强大和广泛的框架,所以使用它的功能!