How can insert formula in excel sheet using phpexcel

前端 未结 2 500
盖世英雄少女心
盖世英雄少女心 2020-12-15 04:23

I have checked many sites for solution of my problem but not found proper solution. I want to insert formula which will calculate the total of marks obtained by students. Ma

相关标签:
2条回答
  • 2020-12-15 04:58

    @Mark Baker Thank you for show the way I tried and done like this -

    $calculatRow='';
        for($ctr=0;$ctr < count($marksColumnArr);$ctr++)
        {
        $calculatRow.='(($'.$marksColumnArr[$ctr].'##/$'.$maXmarksColumnArr[$ctr].'$##)*'.$divisionVal[$ctr].')+';      
    }
    $calculateArrbyRow=substr($calculatRow,0,-1);    
    // Replicate formula by row
    
     for($j=5;$j<count($totalStudents)+5;$j++){
       $formula=str_replace('##',$j,$calculateArrbyRow);
       $cell=PHPExcel_Cell::stringFromColumnIndex($coumnStart+12);
      $objPHPExcel->getActiveSheet()->setCellValue($cell.$j, '='.$formula);
     }    
    $coumnStart=($coumnStart-1)+13; $subjectCtr++;
    }
    
    0 讨论(0)
  • 2020-12-15 05:06

    Read section 4.6.4 of the developer documentation, and examples like 03formulas.php.

    You can write a formula as you'd write it in Excel itself, simply store the formula prefixed with = in the cell

    e.g.

    $objPHPExcel->getActiveSheet()
        ->setCellValue(
            'E10',
            '=SUM(A10:E9)'
        );
    

    Will write the formula =SUM(A10:E9) in cell E10

    When writing a formula to a cell, remember that:

    • Decimal separator is . (period)
    • Function argument separator is , (comma)
    • Matrix row separator is ; (semicolon)
    • English function names must be used

    EDIT

    for ($row = 1; $row <= 10; $row++) {
        $objPHPExcel->getActiveSheet()
            ->setCellValue(
                'J' . $row,
                '=SUM(A'.$row.':C'.$row.')/10 + SUM(D'.$row.':F'.$row.')/20 + SUM(G'.$row.':I'.$row.')/60'
            );
    }
    
    0 讨论(0)
提交回复
热议问题