【问题标题】:how to use excel sum formula in php phpspreadsheet?如何在php phpspreadsheet中使用excel sum公式?
【发布时间】:2019-07-23 04:10:21
【问题描述】:

我正在使用 PhpSpreasdheet php 库。我已经完成了几乎所有我想要对特定列求和并想要显示该列的总数的所有内容。请参阅我的输出如下:-

我的预期输出如下:-

我试过下面的代码:-

$spreadsheet = new Spreadsheet();
$Excel_writer = new Xlsx($spreadsheet);
$spreadsheet->setActiveSheetIndex(0);
$activeSheet = $spreadsheet->getActiveSheet();

$activeSheet->setCellValue('A1', 'Location');
$activeSheet->setCellValue('B1', 'Media Vehicle');
$activeSheet->setCellValue('C1', 'Dimension');
$activeSheet->setCellValue('D1', 'Amount');

$spreadsheet->getActiveSheet()->setAutoFilter('A1:D1');
    $locations = DB::table('locations')->get();
    $locations = json_decode(json_encode($locations),true);
    $i = 2;
    foreach($locations as $location){
        $activeSheet->setCellValue('A'.$i , $location['location']);
        $activeSheet->setCellValue('B'.$i , $location['media_vehicle']);
        $activeSheet->setCellValue('C'.$i , $location['dimension']);
        $activeSheet->setCellValue('D'.$i , $location['amount']);
        $i++;
    }

    $samplepath = storage_path('/excels/sampleExcel'.str_random(5).'.xlsx');
    $Excel_writer->save($samplepath);
    echo 'saved'; die;

我想要总金额列。我想让动态。如果将来它将是 10 行,那么它将计算 10 行的数量列数。

【问题讨论】:

    标签: php excel phpspreadsheet phpoffice


    【解决方案1】:

    会有帮助的;

    $i = 2;
    $first_i = $i;
    foreach($locations as $location){
        $activeSheet->setCellValue('A'.$i , $location['location']);
        $activeSheet->setCellValue('B'.$i , $location['media_vehicle']);
        $activeSheet->setCellValue('C'.$i , $location['dimension']);
        $activeSheet->setCellValue('D'.$i , $location['amount']);
        $i++;
    }
    $last_i = $i - 1;
    $sumrange = 'D' . $first_i . ':D' . $last_i;
    $activeSheet->setCellValue('C' . $i, 'Total:');
    $activeSheet->setCellValue('D' . $i, '=SUM(' . $sumrange . ')');
    

    【讨论】:

      【解决方案2】:

      在 phpspreadsheet 中,您可以使用 Excel 公式。 您只需要求和的数字范围即可。

      $SUMRANGE = 'D2:D'.$i;
      $activeSheet->setCellValue('D'.$i , '=SUM($SUMRANGE)');
      

      【讨论】:

        【解决方案3】:

        你必须拿总第一:

        第一种解决方案:

         $total = 0;
         foreach($locations as $location){
            $activeSheet->setCellValue('A'.$i , $location['location']);
            $activeSheet->setCellValue('B'.$i , $location['media_vehicle']);
            $activeSheet->setCellValue('C'.$i , $location['dimension']);
            $activeSheet->setCellValue('D'.$i , $location['amount']);
            $total = $total+$location['amount'];
            $i++;
        }
        //here your $i val already incremented in foreach() loop
        $activeSheet->setCellValue('C'.$i , "Total");
        $activeSheet->setCellValue('D'.$i , $total);
        

        第二个解决方案:

        $activeSheet->setCellValue('C'.$i , "Total");
        $spreadsheet->getActiveSheet()->getCell('D'.$i)->getCalculatedValue();
        

        我是推荐人:https://phpspreadsheet.readthedocs.io/en/stable/topics/calculation-engine/

        【讨论】:

        • 我知道这是另一种选择,但我想在其中使用 excel 函数 Sum
        • 第二个给出错误调用未定义方法 PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::getActiveSheet()
        • 是的,我也尝试过,但总金额栏为空
        • 我们刚刚计算了需要插入的值
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-15
        • 2018-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多