kartik-v / yii2-export

A library to export server/db data in various formats (e.g. excel, html, pdf, csv etc.)
http://demos.krajee.com/export
Other
165 stars 126 forks source link

Export not respecting line breaks #295

Closed DPineault closed 5 years ago

DPineault commented 5 years ago

Export not respecting line breaks

In my view I have

        [
            'label'=>'Escort',
            'attribute'=>'ProjLegEmpId',
            'value' => function($model) {
                foreach ($model->projectsLegs as $projectLeg) {
                    if(isset($projectLeg->EmpId)){
                        $LegsEmpIds[] = employees::findOne($projectLeg->EmpId)->employeeFullName;
                    }else{
                        $LegsEmpIds[] = '';
                    }
                }
                if(isset($LegsEmpIds)){
                    return implode(chr(10), $LegsEmpIds);
                    // return implode("\n", $LegsEmpIds);
                    // return implode(PHP_EOL, $LegsEmpIds);
                    // return implode("<br>", $LegsEmpIds); // No good
                }else{
                    return '';
                }
            }
        ],

As you can see, I've tried a variety of line break approaches, but the end result is everything displaying on one line.

In Excel, the odd thing is that if I select the cell and click in the formula bar, then exit (having actually changed nothing at all), then the items display on their own individual lines?! It seems to be an issue with the 'Wrap Text'.

Is there a way to display content on multiple lines properly? Is there a way to apply ...->getAlignment()->setWrapText(true); to an export column?

kartik-v commented 5 years ago

Try using a <BR> tag for line break.

DPineault commented 5 years ago

It is one of the things I tried and it did not work.

It truly seems to be an issue with needing to be able to enable Text Wrapping on a column, Can this be done?

kartik-v commented 5 years ago

You need to check the PHPSpreadsheet documentation to control cell formats.

DPineault commented 5 years ago

I know who to do it ->getAlignment()->setWrapText(true);

that's not the issue, but have no clue how to integrate it into the export widget, column setting, ...?

kartik-v commented 5 years ago

Configure the above within the onRenderCell property of the widget as Closure

DPineault commented 5 years ago

Sorry to bother you, but I just can't get it to work. Any chance you could provide a simple sample or point out what I'm doing wrong.

I've tried

        'onRenderDataCell' => function ($cell) {
            $cell->getAlignment()->setWrapText(true);
        },

(The above generates a 'Call to undefined method PhpOffice\PhpSpreadsheet\Cell\Cell::getAlignment()' error) Normally, I'd do something like

        $sheet->getStyle('A'.$i)->getAlignment()->setWrapText(true);

(where $i is the row number) just not sure how to do the same in the widget onRenderDataCell

With further testing, I managed to get the following to work, but is there a way to dynamically define the last data row?

         'onRenderSheet' => function($sheet){
            $sheet->getStyle('Q2:T1000')->getAlignment()->setWrapText(true);
         },

basically replace the hardcoded 1000 with however many rows are being exported?

and Thank you so very much for taking the time to help me. It is greatly appreciated!!!

ThreepE0 commented 5 years ago

Line breaks work on my exports no problem. Can you open in a plain text editor and see if the breaks are there? This seems like an excel config or version issue more than anything.

On Mar 27, 2019, at 7:57 PM, DPineault notifications@github.com wrote:

Sorry to bother you, but I just can't get it to work. Any chance you could provide a simple sample or point out what I'm doing wrong.

I've tried

    'onRenderDataCell' => function ($cell) {
        $cell->getAlignment()->setWrapText(true);
    },

(The above generates a 'Call to undefined method PhpOffice\PhpSpreadsheet\Cell\Cell::getAlignment()' error) Normally, I'd do something like

    $sheet->getStyle('A'.$i)->getAlignment()->setWrapText(true);

(where $i is the row number) just not sure how to do the same in the widget onRenderDataCell

With further testing, I managed to get the following to work, but is there a way to dynamically define the last data row?

     'onRenderSheet' => function($sheet){
        $sheet->getStyle('Q2:T1000')->getAlignment()->setWrapText(true);
     },

basically replace the hardcoded 1000 with however many rows are being exported?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

temirfe commented 5 years ago

Line breaks work on my exports no problem. Can you open in a plain text editor and see if the breaks are there? This seems like an excel config or version issue more than anything.

@ThreepE0 how do you make it work?

friek108 commented 5 years ago

@DPineault @temirfe

This is how you do it in the settings:

'onRenderSheet'=>function($sheet, $grid){
    $sheet->getStyle('A2:'.$sheet->getHighestColumn().$sheet->getHighestRow())
    ->getAlignment()->setWrapText(true);
}