简体   繁体   English

Symfony2,Doctrine,从数据库获取数据以生成Excel

[英]Symfony2, Doctrine, getting data from database to generate excel

I want to retrieve rows of data from the database in order to export an Excel file in Symfony. 我想从数据库中检索数据行,以便在Symfony中导出Excel文件。 I'm having trouble looping through each rows and I'm only getting the last element of the results. 我在遍历每一行时遇到麻烦,而我只得到结果的最后一个元素。 I am guessing I made a mistake somewhere with either the right kind of loops to have or that it's not looping correctly. 我猜我在某种程度上犯了一个错误,要么是正确的循环存在,要么是循环不正确。 Or maybe I am not retrieving the data correctly? 还是我没有正确检索数据? I am only used to retrieving data via Doctrine and outputting the loop in twig templates so this has been confusing me. 我只习惯于通过Doctrine检索数据并在树枝模板中输出循环,所以这一直让我感到困惑。 Help much appreciated. 帮助非常感谢。

$em = $this->getDoctrine()->getManager();
    $query = $em->createQuery('
           .......query ')
            ->setParameter('no', $no);

$results = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

$excel = $this->get('phpexcel')->createPHPExcelObject();
$excel->getProperties()->setCreator('iStyle')
    ->setTitle('Inventory Report');
$i = 2;
$excel->setActiveSheetIndex(0);
$excel->getActiveSheet()->setTitle('Inventory. '.$no)
        ->setCellValue('A1', 'Report')
        ->mergeCells('A1:G1')
        ->setCellValue('A'.$i, 'No.')
        ->setCellValue('B'.$i, 'Color')
        ->setCellValue('C'.$i, 'Weight')
        ->setCellValue('D'.$i, 'SKU')
        ->setCellValue('E'.$i, 'Dimensions')
        ->setCellValue('F'.$i, 'Qty Available');

for($d = 0; $d < count($results); $d++) {

        $i = 3;

        $excel->getActiveSheet()
            ->setCellValue('A'.$i, $results[$d]['no'])
            ->setCellValue('B'.$i, $results[$d]['color'])
            ->setCellValue('C'.$i, $results[$d]['weight'])
            ->setCellValue('D'.$i, $results[$d]['sku'])
            ->setCellValue('E'.$i, $results[$d]['dimensions'])
            ->setCellValue('F'.$i, $results[$d]['qty']);
        $i++;
    }

$writer = $this->get('phpexcel')->createWriter($excel, 'Excel5');
    $response = $this->get('phpexcel')->createStreamedResponse($writer);
    $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
    $response->headers->set('Content-Disposition', 'attachment;filename=invreport-'.$no.'.xls');
    $response->headers->set('Pragma', 'public');
    $response->headers->set('Cache-Control', 'maxage=1');

    return $response;

In your for loop you are setting $i explicitly to 3 then incrementing it $i++ . for循环中,将$i显式设置$i 3然后将其递增$i++ Therefore, you are never appending a new row, your simply updating the last row A3 , B3 , etc over and over again leaving you with the last row to be updated. 因此,您永远不会追加新行,只需一次又一次地更新最后一行A3B3 etc ,就剩下最后一行要更新了。

Take the $i = 3; $i = 3; out of the loop and place it before the for(..) call. 退出循环并将其放在for(..)调用之前。

Example

$i = 3;
for($d = 0; $d < count($results); $d++) {
        $excel->getActiveSheet()
            ->setCellValue('A'.$i, $results[$d]['no'])
            ->setCellValue('B'.$i, $results[$d]['color'])
            ->setCellValue('C'.$i, $results[$d]['weight'])
            ->setCellValue('D'.$i, $results[$d]['sku'])
            ->setCellValue('E'.$i, $results[$d]['dimensions'])
            ->setCellValue('F'.$i, $results[$d]['qty']);
        $i++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM