Generating Excel Using PhpSpreadsheet in PHP

Create Spreadsheet in Core PHP:-

To generate the Excel of your data (in array), firstly you have to install composer in your machine.

After that on the same location where your project is located run the command on that location –

It will create the phpoffice/phpspreadsheet directory inside the vendor directory. Now on the top of your file include the autoloader so define the file –

I have given the full path of autoloader . $libpath – path till my library

It will simply autoload all the files after that to use Classes of xls, xlsx or csv according to your requirement in global scope.

Now create a spreadsheet object & Xls object both –

Now using this spreadsheet object set the cell values and different styling and formatting according to your  need –

Example:-

After setting all values set the header of file and save php output using writer object of xls.

It will create the Excel file of xls format in your downloads directory.

 

Create Spreadsheet in CodeIgniter:-

Step 1.

Repeat the installation of phpspreadsheet in libs folder of codeigniter .

Step 2.

In the same way you have to continue the process of autoloading of files in the global scope of :-

Step 3.

In generation of csv in codeigniter , method is same as above , create object of spreadsheet and then use it to generate each cell step by step. Just one thing you have to keep in mind is –

Clean the object just before save the output in file in codeigniter as codeigniter overwrite the object created. Like –

 

Check out SOLID Principles in PHP

Please follow and like us:
20