Skip to content

Commit

Permalink
Excel workbook creation moved to static method
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Aug 3, 2020
1 parent 2201787 commit 8dd5304
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ $headStyle = [
'border' => 'thin',
];

$excel = new Excel(['Sheet1']);
$excel = Excel::create(['Sheet1']);
$sheet = $excel->getSheet();

$sheet->writeRow($head, $headStyle);
Expand All @@ -84,7 +84,7 @@ require 'vendor/autoload.php';

use \avadim\FastExcelWriter\Excel;

$excel = new Excel(['Formulas']);
$excel = Excel::create(['Formulas']);
$sheet = $excel->getSheet();

// Set Russian locale
Expand Down
4 changes: 3 additions & 1 deletion demo/demo-01-simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
];

$timer = microtime(true);
$excel = new Excel();
$excel = Excel::create();
$sheet = $excel->getSheet();

// The fastest way to write data is row by row
Expand All @@ -40,3 +40,5 @@
$excel->save($outFileName);

echo 'elapsed time: ', round(microtime(true) - $timer, 3), ' sec';

// EOF
4 changes: 3 additions & 1 deletion demo/demo-02-multiple-sheets.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use \avadim\FastExcelWriter\Excel;

$timer = microtime(true);
$excel = new Excel(['Total']);
$excel = Excel::create(['Total']);

// make new sheet
$sheet = $excel->makeSheet('Jan');
Expand Down Expand Up @@ -38,3 +38,5 @@
$excel->save($outFileName);

echo 'elapsed time: ', round(microtime(true) - $timer, 3), ' sec';

// EOF
4 changes: 3 additions & 1 deletion demo/demo-03-widths-heights.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use \avadim\FastExcelWriter\Excel;

$timer = microtime(true);
$excel = new Excel();
$excel = Excel::create();
$sheet = $excel->getSheet();

$sheet->setColWidths([10, 20, 30, 40]);
Expand All @@ -18,3 +18,5 @@
$excel->save($outFileName);

echo 'elapsed time: ', round(microtime(true) - $timer, 3), ' sec';

// EOF
4 changes: 3 additions & 1 deletion demo/demo-04-formulas.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use \avadim\FastExcelWriter\Style;

$timer = microtime(true);
$excel = new Excel(['Sheet1']);
$excel = Excel::create(['Sheet1']);
$sheet = $excel->getSheet();

$row = ['#', 'Number', '\=RC[-1]*0.1'];
Expand Down Expand Up @@ -42,3 +42,5 @@
$excel->save($outFileName);

echo 'elapsed time: ', round(microtime(true) - $timer, 3), ' sec';

// EOF
4 changes: 3 additions & 1 deletion demo/demo-05-borders.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use \avadim\FastExcelWriter\Style;

$timer = microtime(true);
$excel = new Excel();
$excel = Excel::create();
$sheet = $excel->getSheet();

$sheet->setColWidth([1, 2, 3, 4, 5, 6, 7], 4);
Expand Down Expand Up @@ -54,3 +54,5 @@
$excel->save($outFileName);

echo 'elapsed time: ', round(microtime(true) - $timer, 3), ' sec';

// EOF
4 changes: 3 additions & 1 deletion demo/demo-06-autofilter-freeze.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

$timer = microtime(true);
$excel = new Excel();
$excel = Excel::create();
$sheet = $excel->getSheet();

$sheet->setColWidths([8, 36, 8, 18]);
Expand All @@ -33,3 +33,5 @@
$excel->save($outFileName);

echo 'elapsed time: ', round(microtime(true) - $timer, 3), ' sec';

// EOF
16 changes: 1 addition & 15 deletions demo/demo-07-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
$timer = microtime(true);

// Create new Excel book
$excel = new Excel(['Demo']);
$excel = Excel::create(['Demo']);

// Set locale - In most cases, the locale is automatically set correctly,
// but sometimes you need to do it manually
Expand Down Expand Up @@ -115,9 +115,6 @@

$sheet->writeAreas();

//$excel->save($outFileName);exit;
//$sheet->setAutofilter(true);

/*
* You can set three levels for cell style^
* 1. Default style for sheet via setDefaultStyle()
Expand Down Expand Up @@ -149,19 +146,8 @@
$sheet->writeRow($row, $rowOptions);
}

//$sheet->setFreeze(3, 3);
//$sheet->setFreeze(0, 3);
//$sheet->setAutofilter(true);
//$sheet->writeRow($data);


$excel->save($outFileName);

echo 'elapsed time: ', round(microtime(true) - $timer, 3), ' sec';

/*
header('Content-Type', 'application/vnd.ms-excel');
header('Content-Disposition', 'attachment; filename=demo.xlsx');
readfile($fileName);
*/
// EOF
4 changes: 3 additions & 1 deletion demo/demo-08-right-to-left.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
];

$timer = microtime(true);
$excel = new Excel();
$excel = Excel::create();
$excel->setRightToLeft(true);

$sheet = $excel->getSheet();
Expand All @@ -42,3 +42,5 @@
$excel->save($outFileName);

echo 'elapsed time: ', round(microtime(true) - $timer, 3), ' sec';

// EOF
5 changes: 2 additions & 3 deletions demo/demo-250k-rows.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@
$row = [$s1, $s2, $s3, $s4];



$timer = microtime(true);
$excel = new Excel(['250K']);
$excel = Excel::create(['250K']);
$sheet = $excel->getSheet();

$sheet->setColFormats(['string', 'string', 'string', 'string']);

$rowCount = 250000;
for($i = 0; $i < $rowCount; $i++) {
$sheet->writeRow($row, $rowOptions);
$sheet->writeRow($row);
}

$excel->save($outFileName);
Expand Down
34 changes: 23 additions & 11 deletions src/FastExcelWriter/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,37 @@ class Excel
/**
* Excel constructor
*
* @param Writer $writer
*/
public function __construct($writer = null)
{
if (null === $writer) {
$writer = new Writer($this);
}
$this->writer = $writer;
$this->setDefaultLocale();
Style::setDefaultFont(['name' => 'Arial', 'size' => 10]);
}

/**
* @param array|string $sheets
* @param Writer $writer
*
* @return Excel
*/
public function __construct($sheets = null, $writer = null)
public static function create($sheets = null, $writer = null)
{
$excel = new self($writer);
if (empty($sheets)) {
$sheets = ['Sheet1'];
} else {
$sheets = (array)$sheets;
}
foreach ($sheets as $sheetName) {
$sheet = $this->makeSheet($sheetName);
if (count($this->sheets) === 1) {
$sheet->active = true;
}
}
if (null === $writer) {
$writer = new Writer($this);
$sheet = $excel->makeSheet($sheetName);
}
$this->writer = $writer;
$this->setDefaultLocale();
Style::setDefaultFont(['name' => 'Arial', 'size' => 10]);

return $excel;
}

/**
Expand Down Expand Up @@ -596,6 +605,9 @@ public function makeSheet($sheetName)
$this->sheets[$key]->key = $key;
$this->sheets[$key]->index = count($this->sheets);
$this->sheets[$key]->xmlName = 'sheet' . $this->sheets[$key]->index . '.xml';
if (count($this->sheets) === 1) {
$this->sheets[$key]->active = true;
}
}
return $this->sheets[$key];
}
Expand Down

0 comments on commit 8dd5304

Please sign in to comment.