R
R
Rishat Sultanov2017-09-11 18:56:43
Laravel
Rishat Sultanov, 2017-09-11 18:56:43

How to create an excel file based on a controller?

Hello!
Now I'm trying to create an Excel file on Laravel 3.
And display it to the user.
I use: Laravel 3 + PHPExcel
I have such a controller:

<?php

use Laravel\Auth;
require_once (dirname(__FILE__).'\PHPExcel.php');

class Excel_Admin_Default_Controller extends Base_Controller {



    public function get_index($format = 'html')
    {
        if(Auth::check()){
            return View::make('excel::index');
        }
        else{
            return Event::first('404');
        }

    }
    public function generate_file()
    {
        //set the desired name of the excel file
        $fileName = 'create-an-excel-file-in-php';

    //prepare the records to be added on the excel file in an array
        $excelData = array(
            0 => array('Jackson','Barbara','27','F','Florida'),
            1 => array('Kimball','Andrew','25','M','Texas'),
            2 => array('Baker','John','28','M','Arkansas'),
            3 => array('Gamble','Edward','29','M','Virginia'),
            4 => array('Anderson','Kimberly','23','F','Tennessee'),
            5 => array('Houston','Franchine','25','F','Idaho'),
            6 => array('Franklin','Howard','24','M','California'),
            7 => array('Chen','Dan','26','M','Washington'),
            8 => array('Daniel','Carolyn','27','F','North Carolina'),
            9 => array('Englert','Grant','25','M','Delaware')
        );

    // Create new PHPExcel object
        $objPHPExcel = new PHPExcel();

    // Set document properties
        $objPHPExcel->getProperties()->setCreator("Me")->setLastModifiedBy("Me")->setTitle("My Excel Sheet")->setSubject("My Excel Sheet")->setDescription("Excel Sheet")->setKeywords("Excel Sheet")->setCategory("Me");

    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

    // Add column headers
        $objPHPExcel->getActiveSheet()
            ->setCellValue('A1', 'Last Name')
            ->setCellValue('B1', 'First Name')
            ->setCellValue('C1', 'Age')
            ->setCellValue('D1', 'Sex')
            ->setCellValue('E1', 'Location')
        ;

    //Put each record in a new cell
        for($i=0; $i<count($excelData); $i++){
            $ii = $i+2;
            $objPHPExcel->getActiveSheet()->setCellValue('A'.$ii, $excelData[$i][0]);
            $objPHPExcel->getActiveSheet()->setCellValue('B'.$ii, $excelData[$i][1]);
            $objPHPExcel->getActiveSheet()->setCellValue('C'.$ii, $excelData[$i][2]);
            $objPHPExcel->getActiveSheet()->setCellValue('D'.$ii, $excelData[$i][3]);
            $objPHPExcel->getActiveSheet()->setCellValue('E'.$ii, $excelData[$i][4]);
        }

    // Set worksheet title
        $objPHPExcel->getActiveSheet()->setTitle($fileName);
    }


}

Now I can’t figure out how to display this matter in view.
Help me please :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vism, 2017-09-11
@rishatss

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, Excel5);

        $outputFileName = "file.xls";
        //$objWriter->save($outputFileName);

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $outputFileName . '"');
        header('Cache-Control: max-age=0');

        $objWriter->save('php://output');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question