简体   繁体   English

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

[英]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. 我生成一个Excel文件,而不使用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. 因此,我尝试在单元格中添加一个公式,当有人在我的数据验证列表中选择某项时,该公式可以更改值(使用VLOOKUP),这项工作也可以进行。

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 问题是您在\\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);

you should use TYPE_FORMULA . 您应该使用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. 如果您使用字符串类型作为运算符,则通过打开公式来执行excel拒绝计算。

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

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