Laravel-excel: Laravel 5.4/Can't format cells using setColumnFormat

Created on 10 Nov 2017  路  4Comments  路  Source: Maatwebsite/Laravel-Excel

I want to export php web page to excel using maatwebsite. While exporting I need to add a column where only integer values should be updated, don't want to add any characters. And in another column only want to update Text, where do't want to add integer

here is my code:

```
public function downloadExcel1($type,Request $request)
{
$data = $request->input();
return Excel::create('Appraisal', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->getCell('A1')->setValueExplicit('ID');
$sheet->getCell('B1')->setValue('Name');
$sheet->getCell('C1')->setValue('Email');
$sheet->getCell('D1')->setValue('Telephone Number');
$sheet->getCell('E1')->setValue('Date');
$sheet->getStyle('B1')->getFont()->setSize(20);
$sheet->setColumnFormat(array(
'B' => \PHPExcel_Style_NumberFormat::FORMAT_TEXT,
'D' => '0.00',
'E' => 'yyyy-mm-dd',
));

```
but bad luck this code is not working.I don't know whether it's a good way or not.Any help would be Appreciated.

Most helpful comment

Try calling $sheet->setColumnFormat as the very first line of the closure:

public function downloadExcel1($type,Request $request)
{
    $data = $request->input();
    return Excel::create('Appraisal', function($excel) use ($data) {
        $excel->sheet('mySheet', function($sheet) use ($data) {
            // Call setColumnFormat first
            $sheet->setColumnFormat(array(
                'B' =>  \PHPExcel_Style_NumberFormat::FORMAT_TEXT,
                'D' => '0.00',
                'E' => 'yyyy-mm-dd',
            ));
            $sheet->getCell('A1')->setValueExplicit('ID');
            $sheet->getCell('B1')->setValue('Name');
            $sheet->getCell('C1')->setValue('Email');
            $sheet->getCell('D1')->setValue('Telephone Number');
            $sheet->getCell('E1')->setValue('Date');
            $sheet->getStyle('B1')->getFont()->setSize(20);
        });
    });
}

All 4 comments

Try calling $sheet->setColumnFormat as the very first line of the closure:

public function downloadExcel1($type,Request $request)
{
    $data = $request->input();
    return Excel::create('Appraisal', function($excel) use ($data) {
        $excel->sheet('mySheet', function($sheet) use ($data) {
            // Call setColumnFormat first
            $sheet->setColumnFormat(array(
                'B' =>  \PHPExcel_Style_NumberFormat::FORMAT_TEXT,
                'D' => '0.00',
                'E' => 'yyyy-mm-dd',
            ));
            $sheet->getCell('A1')->setValueExplicit('ID');
            $sheet->getCell('B1')->setValue('Name');
            $sheet->getCell('C1')->setValue('Email');
            $sheet->getCell('D1')->setValue('Telephone Number');
            $sheet->getCell('E1')->setValue('Date');
            $sheet->getStyle('B1')->getFont()->setSize(20);
        });
    });
}

@stephanecoinon thanks for your command ...but bad luck same as before.is there any mistake with my code?sometimes yyyy-mm-dd is working but which support text too and float also support characters.

@mjshanu you'll probably need to convert the dates to an Excel timestamp using PHPExcel_Shared_Date::PHPToExcel depending on the date type in $data which by the way you're not using in the code sample you provided.

Which format do the dates have in $data: Carbon, unix timestamp, string?

I also has an issue with exporting numeric values that displayed as text in excel. setColumnFormat() did not do the job. What I did to solve it was to do convert the variable I was exporting to an integer using the PHP command intval($var) ..... Hope this helps

Was this page helpful?
0 / 5 - 0 ratings