PHP code example of nealyip / spreadsheet

1. Go to this page and download the library: Download nealyip/spreadsheet library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nealyip / spreadsheet example snippets


\Nealyip\Spreadsheet\SpreadsheetServiceProvider::class,

use Nealyip\Spreadsheet\Reader;
class Sth{
    protected $_reader;

    public function __construct(Reader $reader) {
        $this->_reader = $reader;
    }

    public function readFile($filename){
        $data = $this->_reader->toKeyValueArray($filename);

    }

use Nealyip\Spreadsheet\Reader;
class Sth{
    protected $_reader;

    public function __construct(Reader $reader) {
        $this->_reader = $reader;
    }

    public function readFile($filename){
        $data = $this->_reader->toKeyValueArray($filename);
        foreach ($this->_reader->read($filename) as $item){
            // $item is a row in array form        
        } 
    }


use Nealyip\Spreadsheet\Writer;
class Sth{
    protected $_writer;

    public function __construct(Writer $writer) {
        $this->_writer = $writer;
    }

    public function writeFile($filename){

        $headers = ['Name', 'Gender', 'Age'];

        $this->_writer
            ->setup("report.xlsx")
            ->useSheet('Report')
            ->writeArray([['Tom','M','20'], ['Ann','F','24']], $headers)
            ->save();

    }

use Nealyip\Spreadsheet\Writer;
class Sth{
    protected $_writer;

    public function __construct(Writer $writer) {
        $this->_writer = $writer;
    }
    
    /**    
     * Data source from DB/API etc
     * 
     * @return \Generator
     */
    protected function _data(){
        $data = [['Tom','M','20'], ['Ann','F','24']];
        foreach ($data as $d) {
            yield $d;
        }
    }


    public function writeFile($filename){

        $headers = ['Name', 'Gender', 'Age'];

        $this->_writer
            ->setup("report.xlsx")
            ->useSheet('Report')
            ->write($this->_data(), $headers)
            ->save();
    }



use Nealyip\Spreadsheet\Writer;
class Sth{
    protected $_writer;

    public function __construct(Writer $writer) {
        $this->_writer = $writer;
    }
    
    /**    
     * Data source from DB/API etc
     * 
     * @return \Generator
     */
    protected function _data(){
        $data = [['Tom','M','20'], ['Ann','F','24']];
        foreach ($data as $d) {
            yield $d;
        }
    }


    public function writeFile($filename){

        $headers = ['Name', 'Gender', 'Age'];

        $this->_writer
            ->setup("report.xlsx", false)
            ->useSheet('Report')
            ->write($this->_data(), $headers)
            ->save();
    }


ini_set('memory_limit', '1000M');

ini_set('max_execution_time', 300);

php artisan vendor:publish --provider="Nealyip\Spreadsheet\SpreadsheetServiceProvider"