简体   繁体   中英

How I can install the PHPExcel library in laravel?

I was considering to download the library from its home page ( http://phpexcel.codeplex.com/wikipage?title=Examples ) but also do not know what folder should I place it. How I can install?

You should use composer: Add "phpexcel/phpexcel": "dev-master" to your composer.json

"require": {
    "phpexcel/phpexcel": "dev-master"
}

Then execute composer update . So you can use it as normal:

public function import($path){

    $objPHPExcel = PHPExcel_IOFactory::load($path);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $highestRow = $objWorksheet->getHighestRow();
    for ($row = 1; $row <= $highestRow; ++$row) {
         var_dump($objWorksheet->getCellByColumnAndRow(1, $row));
    }

}

For install PhpExcel in laravel 5.

Please visit this link for pakage - https://packagist.org/packages/phpoffice/phpexcel .

Please follow the instruction --

1:- Add "phpoffice/phpexcel": "dev-master" to your composer.json.

2:- execute "composer update" on terminal.

3:- Open the file "/vendor/composer/autoload_namespaces.php". Paste the below line in the file.

'PHPExcel' => array($vendorDir . '/phpoffice/phpexcel/Classes'),

4:- Now you can use PHPEXCEL library in your controllers or middleware or library.

use PHPExcel; 
use PHPExcel_IOFactory;

There's actually a neat new PHPExcel library specifically made for Laravel. Easy installation and it looks easy to use (I'm unaffiliated). https://laravel-excel.com/

If you are using Laravel 5 . It is very easy.

check this link for configuration

you may need to type in the following command to proceed with the package download

composer require maatwebsite/excel

check this link for usage

my might want to look at my example code:

public function testexcel(){

    Excel::create('testfile', function($excel) {
        // Set the title
        $excel->setTitle('no title');
        $excel->setCreator('no no creator')->setCompany('no company');
        $excel->setDescription('report file');

        $excel->sheet('sheet1', function($sheet) {
            $data = array(
                array('header1', 'header2','header3','header4','header5','header6','header7'),
                array('data1', 'data2', 300, 400, 500, 0, 100),
                array('data1', 'data2', 300, 400, 500, 0, 100),
                array('data1', 'data2', 300, 400, 500, 0, 100),
                array('data1', 'data2', 300, 400, 500, 0, 100),
                array('data1', 'data2', 300, 400, 500, 0, 100),
                array('data1', 'data2', 300, 400, 500, 0, 100)
            );
            $sheet->fromArray($data, null, 'A1', false, false);
            $sheet->cells('A1:G1', function($cells) {
            $cells->setBackground('#AAAAFF');

            });
        });
    })->download('xlsx');
}

For future readers:

PHPExcell is no more maintained . Rather use:

https://github.com/PHPOffice/PhpSpreadsheet http://phpspreadsheet.readthedocs.io/en/develop/#installation

Because all efforts have shifted to PhpSpreadsheet, PHPExcel will no longer be maintained. All contributions for PHPExcel, patches and new features, should target PhpSpreadsheet develop branch.

  1. first

    composer require phpexcel/phpexcel

  2. Use it your controller

    use PHPExcel;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM