如果大量的行

时间:2016-08-04 08:26:03

标签: php symfony

您好我用PHPExcel生成xls文件并且它运行良好(即使使用6000行文件)但是当我尝试使用9000行时(我没有尝试使用8000)它会使Excel无法读取文件。

这是我的代码:

public function generationExcelAction(Request $request)
{
    /*
     * set_time_limit & ini_set on été ajouté afin d'éviter le timeout lors d'un export excel trop volumineux.
     */
    set_time_limit (0);
    ini_set('memory_limit', '-1');
    $this->getDoctrine()->getConnection()->getConfiguration()->setSQLLogger(null);
    /** @var \Nalods\Entity\ArticleRepository $repoArticles */
    $repoArticles = $this->getDoctrine()->getRepository('Nalods\Entity\Article');
    $requeteArticle = $repoArticles->findByPlateforme($this->getUser()->getPlateforme()->getId());
    $filter = new ReassortFilter();
    $filterForm = $this->createForm(
        ReassortFilterType::class,
        $filter,
        [
            'arbo' => $this->getArborescenceManager()->getArborescenceForReassort(),
            'fournisseurs' => $this->getDoctrine()->getRepository('Nalods\Entity\FournisseurCommun')->getFournisseursForReassort($this->getUser()->getPlateforme()->getId()),
        ]
    );
    $filterForm->handleRequest($request);
    $requeteArticle = $repoArticles->applyFilters($requeteArticle, $filter, $request->getSession()->get(Commande::ID_SESSION));
    $requeteArticle->select('DISTINCT a.id');
    $listeIdArticles = $requeteArticle->getQuery()->getScalarResult();

    $response = new StreamedResponse();
    $response->setCallback(function () use ($listeIdArticles) {

        // Crée un nouveau objet PHPExcel
        $objPHPExcel = new PHPExcel();

        // Initialise les propriété du classeur Excel
        $objPHPExcel->getProperties()->setCreator("Nalod's")
            ->setLastModifiedBy("Nalod's")
            ->setTitle("Export XLS");

        // Ajout de la ligne d'entête de colonne
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Code Centrale')
            ->setCellValue('B1', 'Désignation')
            ->setCellValue('C1', 'Gencod')
            ->setCellValue('D1', 'Réf Produit')
            ->setCellValue('E1', 'N°Rayon')
            ->setCellValue('F1', 'Rayon')
            ->setCellValue('G1', 'N°Famille')
            ->setCellValue('H1', 'Famille')
            ->setCellValue('I1', 'N°Sous famille')
            ->setCellValue('J1', 'Sous famille')
            ->setCellValue('K1', 'N° Gamme')
            ->setCellValue('L1', 'Unité d\'achat')
            ->setCellValue('M1', 'Unité de vente')
            ->setCellValue('N1', 'Nombre UV / UA')
            ->setCellValue('O1', 'Colisage')
            ->setCellValue('P1', 'Appro Mini')
            ->setCellValue('Q1', 'Prix achat brut')
            ->setCellValue('R1', 'Remise magasin 1')
            ->setCellValue('S1', 'Remise magasin 2')
            ->setCellValue('T1', 'Remise magasin 3')
            ->setCellValue('U1', 'Remise magasin 4')
            ->setCellValue('V1', 'Remise magasin 5')
            ->setCellValue('W1', 'Pa net sans remise de base')
            ->setCellValue('X1', 'Prix achat Net')
            ->setCellValue('Y1', 'Coefficient')
            ->setCellValue('Z1', 'Taux marge')
            ->setCellValue('AA1', 'Prix vente conseillé')
            ->setCellValue('AB1', 'Taux de TVA')
            ->setCellValue('AC1', 'Date Début Validité')
            ->setCellValue('AD1', 'Date Fin Validité')
            ->setCellValue('AE1', 'Date MAJ')
            ->setCellValue('AF1', 'N°Fournisseur')
            ->setCellValue('AG1', 'Caractéristiques')
            ->setCellValue('AH1', 'Marque')
            ->setCellValue('AI1', 'Montant_ttc_ecotaxe')
            ->setCellValue('AJ1', 'Montant écomobilier')
            ->setCellValue('AK1', 'Stock disponible');

        $i = 0;
        $j = 0;
        $listeBatch = array();
        $total = count($listeIdArticles);
        while ($i < $total) {
            $listeBatch[] = $listeIdArticles[$i]['id'];
            if ($j < 50 && $i != $total - 1) {
                $j++;
            } else {
                $repoArticles = $this->getDoctrine()->getRepository('Nalods\Entity\Article');
                $requeteArticle = $repoArticles->findByPlateforme($this->getUser()->getPlateforme()->getId());
                $requeteArticle->andWhere($requeteArticle->expr()->in('a.id', $listeBatch));
                $listeArticles = $requeteArticle->getQuery()->getResult();
                /** @var \Nalods\Entity\Article $article */
                foreach ($listeArticles as $k => $article) {
                    $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A'.($i-$j+$k+2), $article->getId())
                        ->setCellValue('B'.($i-$j+$k+2), $article->getNom())
                        ->setCellValue('C'.($i-$j+$k+2), $article->getArticleGencod()->first()->getGencod()->getCodeBarreComplet())
                        ->setCellValue('D'.($i-$j+$k+2), $article->getArticlePlateforme()->first()->getReferenceFournisseur())
                        ->setCellValue('E'.($i-$j+$k+2), $article->getSousFamille()->getFamille()->getRayon()->getId())
                        ->setCellValue('F'.($i-$j+$k+2), $article->getSousFamille()->getFamille()->getRayon()->getLibelleComplet())
                        ->setCellValue('G'.($i-$j+$k+2), $article->getSousFamille()->getFamille()->getId())
                        ->setCellValue('H'.($i-$j+$k+2), $article->getSousFamille()->getFamille()->getLibelleComplet())
                        ->setCellValue('I'.($i-$j+$k+2), $article->getSousFamille()->getId())
                        ->setCellValue('J'.($i-$j+$k+2), $article->getSousFamille()->getLibelleComplet())
                        ->setCellValue('K'.($i-$j+$k+2), $article->getGamme())
                        ->setCellValue('L'.($i-$j+$k+2), $article->getUniteAchatComplete())
                        ->setCellValue('M'.($i-$j+$k+2), $article->getUniteVenteComplete())
                        ->setCellValue('N'.($i-$j+$k+2), $article->getNombreUvParUa())
                        ->setCellValue('O'.($i-$j+$k+2), $article->getArticlePlateforme()->first()->getColisage())
                        ->setCellValue('P'.($i-$j+$k+2), ($article->getArticlePlateforme()->first()->getPrixActuels()->first()->getQuantiteMinimum()) ? $article->getArticlePlateforme()->first()->getPrixActuels()->first()->getQuantiteMinimum() : $article->getArticlePlateforme()->first()->getMinimumCommande())
                        ->setCellValue('Q'.($i-$j+$k+2), 0)
                        ->setCellValue('R'.($i-$j+$k+2), 0)
                        ->setCellValue('S'.($i-$j+$k+2), 0)
                        ->setCellValue('T'.($i-$j+$k+2), 0)
                        ->setCellValue('U'.($i-$j+$k+2), 0)
                        ->setCellValue('V'.($i-$j+$k+2), 0)
                        ->setCellValue('W'.($i-$j+$k+2), 'VRAI')
                        ->setCellValue('X'.($i-$j+$k+2), $article->getArticlePlateforme()->first()->getPrixActuels()->first()->getPrixNet())
                        ->setCellValue('Y'.($i-$j+$k+2), round(($article->getPrixVenteConseille()/$article->getArticlePlateforme()->first()->getPrixActuels()->first()->getPrixNet()),2)) //Coefficient : PVTTC/PAHT
                        ->setCellValue('Z'.($i-$j+$k+2), round(((($article->getPrixVenteConseille()/(1+($article->getTauxTva()->getTaux()/100)))-$article->getArticlePlateforme()->first()->getPrixActuels()->first()->getPrixNet())/($article->getPrixVenteConseille()/(1+($article->getTauxTva()->getTaux()/100))))*100,2)) //Taux de marge : (PVHT-PAHT)/PVHT
                        ->setCellValue('AA'.($i-$j+$k+2), $article->getPrixVenteConseille())
                        ->setCellValue('AB'.($i-$j+$k+2), $article->getTauxTva()->getTaux())
                        ->setCellValue('AC'.($i-$j+$k+2), $article->getArticlePlateforme()->first()->getPrixActuels()->first()->getDateDebut()->format('d/m/Y'))
                        ->setCellValue('AD'.($i-$j+$k+2), $article->getArticlePlateforme()->first()->getPrixActuels()->first()->getDateFin()->format('d/m/Y'))
                        ->setCellValue('AE'.($i-$j+$k+2), date_format(new \DateTime(), 'd/m/Y'))
                        ->setCellValue('AF'.($i-$j+$k+2), $this->getUser()->getPlateforme()->getId())//N°Plateforme=N°Fournisseur
                        ->setCellValue('AG'.($i-$j+$k+2), '')
                        ->setCellValue('AH'.($i-$j+$k+2), $article->getMarqueLibelle())
                        ->setCellValue('AI'.($i-$j+$k+2), $article->getEcoContribution())
                        ->setCellValue('AJ'.($i-$j+$k+2), $article->getEcoMobilier())
                        ->setCellValue('AK'.($i-$j+$k+2), $article->getArticlePlateforme()->first()->getStock());
                }
                $j = 0;
                $listeBatch = array();
                $this->getDoctrine()->getManager()->clear();
            }
            $i++;
        }

        // Enregistre le fichier Excel
        $writer = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $writer->save('php://output');
    });

    $response->headers->set('Content-Type', 'application/vnd.ms-excel');
    $response->headers->set('Content-Disposition', 'attachment;filename="ArticlesPlateforme.xls"');
    $response->headers->set('Cache-Control', 'max-age=0');
    // If you're serving to IE 9, then the following may be needed
    $response->headers->set('Cache-Control', 'max-age=1');
    // If you're serving to IE over SSL, then the following may be needed
    $response->headers->set('Expires', 'Mon, 26 Jul 2015 05:00:00 GMT'); // Date in the past
    $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s').' GMT'); // always modified
    $response->headers->set('Cache-Control', 'cache, must-revalidate'); // HTTP/1.1
    $response->headers->set('Pragma', 'public'); // HTTP/1.0

    return $response;
}

如果有人能为我提供完美的东西,我怎么能提高我的代码,使它能够运行更多的线条呢?

度过美好的一天!

0 个答案:

没有答案