8

I am using PhpSpreasdheet php library. I have done almost everything i want to sum the particular column and want to show total of that column.. See My output is coming below:- enter image description here

My expected output like below:- enter image description here

I have tried below code:-

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

I want the total of amount column. I want to make dynamic. if in future it will be 10 rows so it will calculate 10 rows of amount column count.

Sarah Trees
  • 822
  • 12
  • 28
kunal
  • 4,122
  • 12
  • 40
  • 75

3 Answers3

8

In phpspreadsheet you can use Excel formulas. All you need is the range of numbers you want to sum.

$SUMRANGE = 'D2:D'.$i;
$activeSheet->setCellValue('D'.$i , '=SUM($SUMRANGE)');
Sarah Trees
  • 822
  • 12
  • 28
1

you have to take total 1st :

1st solution:

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

2nd solution:

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

I was refer : https://phpspreadsheet.readthedocs.io/en/stable/topics/calculation-engine/

Ashu
  • 1,320
  • 2
  • 10
  • 24
1

It will help;

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