使用php和symfony从数组创建Excel文件 [英] Creating Excel file from array using php and symfony
本文介绍了使用php和symfony从数组创建Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正尝试使用PHP和symfony将数组导出为XLS文件,如下面的代码所示。创建XLS文件后,我只能得到数组的最后一行,并显示在文件的第一行。似乎 lignes变量没有增加。我不知道出什么问题了,有人可以帮忙吗?
I'm trying to export an array as XLS file using PHP and symfony as shown by the code below. Once the XLS file created, I can only get the last row of the array, and it's displayed in the first line of my file. It seems that the "lignes" variable is not incremented. I can't figure out what's wrong, can anyone help with that?
foreach ($adresses as $ad ){
$phpExcelObject->setActiveSheetIndex(0)->setCellValue('A' . $lignes, $ad->getId());
$phpExcelObject->setActiveSheetIndex(0)->setCellValue('E' . $lignes, $ad->getTypeVoie());
$phpExcelObject->setActiveSheetIndex(0)->setCellValue('F' . $lignes, $ad->getVoie());
$phpExcelObject->setActiveSheetIndex(0)->setCellValue('G' . $lignes, $ad->getTypeQuartier());
$phpExcelObject->setActiveSheetIndex(0)->setCellValue('H' . $lignes, $ad->getQuartier());
$phpExcelObject->setActiveSheetIndex(0)->setCellValue('I' . $lignes, $ad->getCodePostale());
$phpExcelObject->setActiveSheetIndex(0)->setCellValue('K' . $lignes, $ad->getPays());
$phpExcelObject->setActiveSheetIndex(0)->setCellValue('J' . $lignes, $ad->getVille());
$lignes++;
}
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel2007');
$writer->save($fichier->getWebPathOut() );
推荐答案
代码可能如下所示。这是工作代码。希望您在此代码中找到了答案。此功能使用php excel phpoffice / phpexcel: ^ 1.8
。
The code could be like below. This is working code. Hope you got your answere here in this code. This functionality uses php excel "phpoffice/phpexcel": "^1.8"
.
public function downloadAction(Request $request)
{
$phoneListId = $request->get('phonelist_id');
$em = $this->getDoctrine()->getManager();
$phoneList = $em->getRepository(PhoneList::class)->find($phoneListId);
$phoneNumbers = $phoneList->getPhoneNumbers();
// ask the service for a Excel5
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
$phpExcelObject->getProperties()->setCreator("liuggio")
->setTitle($phoneList->getTitle())
->setSubject($phoneList->getTitle());
$sheet = $phpExcelObject->setActiveSheetIndex(0);
$sheet->setCellValue('A1', 'Name');
$sheet->setCellValue('B1', 'Number');
$sheet->setCellValue('C1', 'Phone Number');
$sheet->setCellValue('D1', 'Designation');
$sheet->setCellValue('E1', 'Office');
$counter = 2;
foreach ($phoneNumbers as $phoneNumber) {
$sheet->setCellValue('A' . $counter, $phoneNumber->getName());
$sheet->setCellValue('B' . $counter, $phoneNumber->getNumber());
$sheet->setCellValue('C' . $counter, $phoneNumber->getPhoneNumber());
$sheet->setCellValue('D' . $counter, $phoneNumber->getDesignation());
$sheet->setCellValue('E' . $counter, $phoneNumber->getOffice());
$counter++;
}
$phpExcelObject->getActiveSheet()->setTitle($phoneList->getTitle());
// 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,
$phoneList->getTitle() . '.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;
}
这篇关于使用php和symfony从数组创建Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文