【问题标题】:Creating Excel file from array using php and symfony使用 php 和 symfony 从数组创建 Excel 文件
【发布时间】:2017-01-04 06:58:15
【问题描述】:

我正在尝试使用 PHP 和 symfony 将数组导出为 XLS 文件,如下面的代码所示。创建 XLS 文件后,我只能获取数组的最后一行,并且它显示在文件的第一行。似乎“lignes”变量没有增加。我不知道怎么回事,谁能帮忙解决一下?

  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() );

【问题讨论】:

  • $lignes++ 在你的循环中......这是行号,你永远不会改变它

标签: php arrays excel symfony


【解决方案1】:

代码可能如下所示。这是工作代码。希望你在这段代码中得到你的答案。此功能使用 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;
}

【讨论】:

    【解决方案2】:

    我会尝试类似:

        $lignes = 1;
        $phpExcelObject->setActiveSheetIndex(0);
        foreach ($adresses as $ad ){
                $phpExcelObject->getActiveSheet()
                ->setCellValue('A' . $lignes, $ad->getId())
                ->setCellValue('E' . $lignes, $ad->getTypeVoie())
                ->setCellValue('F' . $lignes, $ad->getVoie())
                ->setCellValue('G' . $lignes, $ad->getTypeQuartier())
                ->setCellValue('H' . $lignes, $ad->getQuartier())
                ->setCellValue('I' . $lignes, $ad->getCodePostale())
                ->setCellValue('K' . $lignes, $ad->getPays())
                ->setCellValue('J' . $lignes, $ad->getVille())
                $lignes++;
    
            }
            $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel2007');
            $writer->save($fichier->getWebPathOut() );
    

    【讨论】:

    • 其实我是从2$lignes=2开始的
    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    相关资源
    最近更新 更多