我正在从数据库中提取特定ID的数组数据,并定义了需要存储这些值以便导出的单元格。
我正在通过函数从服务中调用它,但找不到如何在特定单元格中设置特定数据的任何示例。
我的代码:
通过我的服务。
public function getExport($account)
{
$all = $this->em->getRepository('AppBundle:App')->findBy([
'account' => $account
]);
$rows = [];
$rows[] = [
"client",
"payer_name",
"payer_info"
];
foreach ($all as $row) {
$client = $row->getAccount();
$payerName = $row->getName();
$payerInfo = $row->getInfo();
if (empty($rows[$client])) {
$rows[] = [
$client,
$payerName,
$payerInfo
];
} else {
$rows[$client][1][] = $payerInfo;
}
};
return $rows;
}
然后在控制器中,我准备了导出逻辑。
public function downloadInvoice($account) {
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($webPath);
$rows= $this->get('app')->getExport($account);
$phpExcelObject->setActiveSheetIndex(0)
->setCellValue('A17', 'Here need to go client')
->setCellValue('E17', 'Here needs to go payer info etc...')
$writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel2007');
$response = $this->get('phpexcel')->createStreamedResponse($writer);
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'the-file.xlsx'
);
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
return $response;
}
答案 0 :(得分:0)
所以我做到了:
if($rows) {
foreach ($rows as $row) {
$phpExcelObject->setActiveSheetIndex(0)
->setCellValue('A17', $row[0])
->setCellValue('E17', $row[1])
->setCellValue('F17', $row[2])
->setCellValue('G17', $row[3])
->setCellValue('H17', $row[4]);
}
}
我最终发现它非常简单。