2
votes

I try to use maatwebsite Laravel excel 2.1 to replace my old phpexcel script.

With php excel, I query my bdd and loop inside.

Each line of data as his own data + parents data so two or more line can have same value.

So I loop through each data and write the value row by row. If parents values are the same as before, I merge the cell.

At the end, I have merge all cells that have the same values.

How can I do something like this on laravel-excel ??

At the moment I gather all the data inside an array and use this code to export :

        Excel::load('excel\recap.xls', function($excel) use($array) {

        $excel->sheet('Copro', function($sheet) use($array) {

            $sheet->fromArray($array, null, 'A9', false);
            foreach ($sheet as $row) {
                $sheet->setCellValue($row, 'some value');
            }
        });

    })->export('xls');

For example, $array can be like this :

array:2 [
  0 => array:8 [
    "Batiment" => "Unique"
    "N° du lot" => 7
    "Étage" => "1e étage"
    "Fraction" => "Parking 0.35"
    "Surface" => "11.500"
    "Coefficient" => "0.350"
    "Surface pondérée" => "4.025"
    "Quote-part" => "635.961"
  ]
  1 => array:8 [
    "Batiment" => "Unique"
    "N° du lot" => 8
    "Étage" => "1e étage"
    "Fraction" => "Principal"
    "Surface" => "65.850"
    "Coefficient" => "0.903"
    "Surface pondérée" => "59.265"
    "Quote-part" => "9364.039"
  ]
]

So here I want to merge "Batiment" and "Etage" columns, but it can be else columns and for a lot of rows.

Someone know how to do this ?

Thank for your help.

1

1 Answers

0
votes

I was also looking solution for this question. where i want to merge cell and put content (value) on that. After few search i got some solution on this. but did not checked because i am using Maatawebsite for get Excel file.

But any one can try thing.. Solution is based on PHPExcel nit sure ,it will work on Maatawebsite.

Source Link

Merge from column A row 1 to column E row 1

$objPHPExcel->getActiveSheet()->mergeCells('A1:E1');

// add some text
$objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.'); 

Merge from column A row 1 to column E row 3

$objPHPExcel->getActiveSheet()->mergeCells('A1:E3');

// add some text
$objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.');

I checked maatawebsite document and they have same method mergeCells. so i think i would be work.

This Solution from Maatawebste.

$sheet->cells('A1:C1', function($cells) {
    $cells->setBorder('thin', 'thin', 'thin', 'thin');
});
$sheet->mergeCells('A1:C1');