1

I want to make export data into excel in my project, i have made it, but after I check the results, the results doesnt like what I want. here I would like to make a result there is a title table.

This code my controller:

public function getExport(){
        Excel::create('Export data', function($excel) {

        $excel->sheet('Sheet 1', function($sheet) {

            $products=DB::table('log_patrols')
            ->join("cms_companies","cms_companies.id","=","log_patrols.id_cms_companies")
            ->join("securities","securities.id","=","log_patrols.id_securities")
            ->select("log_patrols.*","cms_companies.name as nama_companies","securities.name as nama_security")
            ->get();
                foreach($products as $product) {
                 $data[] = array(
                    $product->date_start,
                    $product->date_end,
                    $product->condition_status,
                    $product->nama_security,
                    $product->nama_companies,
                );
            }
            $sheet->fromArray($data);
        });
    })->export('xls');
    }

this my problem result : result

and it should be :

should

my problem is how to change the number into text what i want in the header table.

what improvements do i have to make to the code to achieve my goal?

NB : i use maatwebsite/excel

2 Answers 2

5

From the official docs:

By default the export will use the keys of your array (or model attribute names) as first row (header column). To change this behaviour you can edit the default config setting (excel::export.generate_heading_by_indices) or pass false as 5th parameter:

Change:

$sheet->fromArray($data); to $sheet->fromArray($data, null, 'A1', false, false);

how to change the number into text what i want in the header table.

Then you can define your own heading and prepend it to the first row of the sheet.

$headings = array('date start', 'date end', 'status condition', 'security', 'company');

$sheet->prependRow(1, $headings);

That should make it work.

Sign up to request clarification or add additional context in comments.

4 Comments

ahh oke, that help me. maybe can i make style in thats result like i want to make border table ?
For border styling, please refer to the sheet styling guide and the reference guide to see the available border styles.
Also, if this solved your question, can you mark this as the answer..thanks.:)
only first cell works but after that give me number ?
0

Try this in your controller

$data=Insertion::get()->toArray();
    return Excel::create('yourfilename', function($excel) use ($data) {
        $excel->sheet('mySheet', function($sheet) use ($data)
        {
            $sheet->cell('A1:C1',function($cell)
            {
                $cell->setAlignment('center');
                $cell->setFontWeight('bold');
            });

            $sheet->cell('A:C',function($cell)
            {
                $cell->setAlignment('center');
            });

            $sheet->cell('A1', function($cell) 
            {
                $cell->setValue('S.No');  

            });
            $sheet->cell('B1', function($cell) 
            {
                $cell->setValue('Name'); 
            });
            $sheet->cell('C1', function($cell) 
            {
                $cell->setValue('Father Name');    
            });
            if (!empty($data)) {
                $sno=1;
                foreach ($data as $key => $value) 
                {
                    $i= $key+2;
                    $sheet->cell('A'.$i, $sno); 
                    $sheet->cell('B'.$i, $value['name']); 
                    $sheet->cell('C'.$i, $value['fathername']);  
                    $sno++;
                }
            }
        });
    })->download(xlsx);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.