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/ */
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();
}