在PhpSpreadsheet中创建Excel文件时如何计算公式

时间:2019-07-03 10:21:15

标签: excel symfony4 phpspreadsheet

我的代码有问题。我生成一个Excel文件,而不使用PhpSpreadsheet lib。所以我可以写我的数据,并且工作正常。我在使用其他工作表的单元格中添加了数据验证列表,它也起作用。因此,我尝试在单元格中添加一个公式,当有人在我的数据验证列表中选择某项时,该公式可以更改值(使用VLOOKUP),这项工作也可以进行。

但是我的问题是打开文件时未计算公式。当我打开文件时,我只看到公式,并且想查看结果。

这是我的代码:

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

这是我打开文件时所拥有的:

enter image description here

我想将公式保留在单元格中,只在打开文件时显示结果。因此有人对打开文件之前如何计算公式有一个想法?我尝试使用文档,但是它不起作用。

比提前

1 个答案:

答案 0 :(得分:0)

问题是您在{p>中使用了\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING

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

您应该使用TYPE_FORMULA

非计算的另一个常见问题是字符串运算符,而不是公式中的数字类型运算符。如果您使用字符串类型作为操作符,则可以通过打开公式来执行excel拒绝计算。