简体   繁体   中英

How to calculate formula when I create excel file in PhpSpreadsheet

I have a problem with my code. I generate an excel file unsing PhpSpreadsheet lib. So i can write my data and it worked fine. I add a data validation list in a cell using an other worksheet and it work too. So I try to add in a cell a formula which can change the value (using VLOOKUP) when someone select something in my data validation list and this work too.

But my problem is my formula is not calculated when I open my file. When I open the file I just see the formula and I wanted to see the result.

This is my code:

$i=2;
    $j=6;
   $filename = "export-excel.xlsx";
    $spreadsheet = new Spreadsheet();
    $dataSheet = $spreadsheet->getSheet(0);

    $dataSheet1 = $spreadsheet->createSheet(1);
    $dataSheet1->setTitle("Materiel");
    $dataSheet1->setCellValue('A1','Modèle');
    $dataSheet1->setCellValue('B1','Marque');
    $dataSheet1->setCellValue('C1','Calibre');
    $dataSheet1->setCellValue('D1','ITH');
    $dataSheet1->setCellValue('E1','IMG');
    $dataSheet1->setCellValue('F1','Type Matériel');

    //remplissage matériel
    $listMateriel = $this->entityManager->getRepository(Materiel::class)->findAll();
    $nbrMateriel = count($listMateriel) +1;
    foreach($listMateriel as $materiel){
        $dataSheet1->setCellValue('A'.$i,$materiel->getModele());
        $dataSheet1->setCellValue('B'.$i,$materiel->getMarque());
        $dataSheet1->setCellValue('C'.$i,$materiel->getCalibre());
        $dataSheet1->setCellValue('D'.$i,$materiel->getIth());
        $dataSheet1->setCellValue('E'.$i,$materiel->getImg());
        $dataSheet1->setCellValue('F'.$i,$materiel->getTypeMateriel()->getLibelle());

        $i++;
    }

    //remplissage enveloppe
   $enveloppe = $appareil->getEnveloppe();
    $dataSheet->setCellValue('A5',1);
    $dataSheet->setCellValue('B5',1);
    $dataSheet->setCellValue('C5',$enveloppe->getLieu()->getLibelle());
    $dataSheet->setCellValue('D5',$enveloppe->getNom());
    $dataSheet->setCellValue('E5',$enveloppe->getNumeroschema());
    $dataSheet->setCellValue('F5',$enveloppe->getCodegmao());
    $dataSheet->setCellValue('G5',$enveloppe->getCodemire());
    $dataSheet->setCellValue('H5',$enveloppe->getIk());
    $dataSheet->setCellValue('I5',$enveloppe->getResponsable());
    $dataSheet->setCellValue('J5',$enveloppe->getUnitegeo());
    $dataSheet->setCellValue('K5',$enveloppe->getCentreMainteneur());

    //remplissage appareil
   foreach($enveloppe->getAppareils() as $app){
       $dataSheet->setCellValue('A'.$j,1);
       $dataSheet->setCellValue('B'.$j,2);
       $dataSheet->setCellValue('C'.$j,$app->getRepere());
       $dataSheet->setCellValue('D'.$j,$app->getNom());
       $dataSheet->setCellValue('E'.$j,$app->getOrdreDansArmoire());
       $dataSheet->setCellValue('F'.$j,$app->getMonotri());
       $dataSheet->setCellValue('G'.$j,$app->getDepart());
       $dataSheet->setCellValue('H'.$j,$app->getDdr());
       $dataSheet->setCellValue('I'.$j,$app->getCalibreddr());
       $dataSheet->setCellValue('J'.$j,$app->getMotorise());
       $dataSheet->setCellValue('K'.$j,$app->getAlarme());
       $dataSheet->setCellValue('L'.$j,(null === $app->getNaturealimentation()) ? '' : $app->getNaturealimentation()->getLibLong());
       $dataSheet->setCellValue('M'.$j,$app->getSimultaneite());
       $dataSheet->setCellValue('N'.$j,$app->getCompteur());
       $dataSheet->setCellValue('O'.$j,(null === $app->getTypeconsommateur()) ? '' : $app->getTypeconsommateur()->getLibelle());
       $dataSheet->setCellValue('P'.$j,$app->getTypecable());
       $dataSheet->setCellValue('Q'.$j,$app->getNumcable());
       $dataSheet->setCellValue('R'.$j,$app->getLongueurcable());
       $dataSheet->setCellValue('S'.$j,$app->getSectioncable());
       $dataSheet->setCellValue('T'.$j,$app->getMateriel()->getModele());
       $this->dataValidation('T'.$j, $spreadsheet, $nbrMateriel);
       $dataSheet->setCellValue('U'.$j,$app->getReglageIth());
       $dataSheet->setCellValue('V'.$j,$app->getReglageImag());
       $dataSheet->setCellValue('W'.$j,$app->getObservation());
    //This is my formula who work fine      
$dataSheet->setCellValueExplicit('X'.$j,'=SI(T'.$j.'<>"";RECHERCHEV(T'.$j.';Materiel!$A$2:$F$'.$nbrMateriel.';4;FAUX);"SO")', \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
           $spreadsheet->getActiveSheet()->getCell('X'.$j)->getCalculatedValue();
           Calculation::getInstance($spreadsheet)->disableCalculationCache();
           $dataSheet->setCellValue('Y'.$j,$app->getMateriel()->getImg());

           $j++;
       }

        $tmpPath = $this->getTmpFile2();
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->setPreCalculateFormulas(true);
        $writer->save($tmpPath);

This is what I have when I open my file:

在此处输入图片说明

I want to keep my formula in the cell and just show the result when I open my file. So someone have an Idea to how I can calculate my formula before to open my file ? I try with the documentation but it doesn't work.

Than in advance

The problem is that you use \\PhpOffice\\PhpSpreadsheet\\Cell\\DataType::TYPE_STRING in

$dataSheet->setCellValueExplicit('X'.$j,'=SI(T'.$j.'<>"";RECHERCHEV(T'.$j.';
Materiel!$A$2:$F$'.$nbrMateriel.';4;FAUX);"SO")', \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);

you should use TYPE_FORMULA .

A other common problem for non calculate are string operants instead of number typ operants in the formula. If you have string typs as operant the calculation excel refuse executed by opening the formula.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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