-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ability to prepend header in csv und excel outputs
xmlwriter writes rootnode on prepare fixed some tests
- Loading branch information
Showing
5 changed files
with
213 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Mathielen\DataImport\Writer; | ||
use Ddeboer\DataImport\Writer\AbstractStreamWriter; | ||
|
||
/** | ||
* Writes to a CSV file | ||
* | ||
* @author David de Boer <[email protected]> | ||
*/ | ||
class CsvWriter extends AbstractStreamWriter | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $delimiter = ';'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $enclosure = '"'; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
private $utf8Encoding = false; | ||
private $row = 1; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
protected $prependHeaderRow; | ||
|
||
/** | ||
* @param string $delimiter The delimiter | ||
* @param string $enclosure The enclosure | ||
* @param resource $stream | ||
* @param boolean $utf8Encoding | ||
*/ | ||
public function __construct($delimiter = ';', $enclosure = '"', $stream = null, $utf8Encoding = false, $prependHeaderRow = false) | ||
{ | ||
parent::__construct($stream); | ||
|
||
$this->delimiter = $delimiter; | ||
$this->enclosure = $enclosure; | ||
$this->utf8Encoding = $utf8Encoding; | ||
|
||
$this->prependHeaderRow = $prependHeaderRow; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function prepare() | ||
{ | ||
if ($this->utf8Encoding) { | ||
fprintf($this->getStream(), chr(0xEF) . chr(0xBB) . chr(0xBF)); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function writeItem(array $item) | ||
{ | ||
if ($this->prependHeaderRow && 1 == $this->row++) { | ||
$headers = array_keys($item); | ||
fputcsv($this->getStream(), $headers, $this->delimiter, $this->enclosure); | ||
} | ||
|
||
fputcsv($this->getStream(), $item, $this->delimiter, $this->enclosure); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace Mathielen\DataImport\Writer; | ||
|
||
use Ddeboer\DataImport\Writer\AbstractWriter; | ||
use PHPExcel; | ||
use PHPExcel_IOFactory; | ||
|
||
/** | ||
* Writes to an Excel file | ||
* | ||
* @author David de Boer <[email protected]> | ||
*/ | ||
class ExcelWriter extends AbstractWriter | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $filename; | ||
|
||
/** | ||
* @var null|string | ||
*/ | ||
protected $sheet; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $type; | ||
|
||
/** | ||
* @var boolean | ||
*/ | ||
protected $prependHeaderRow; | ||
|
||
/** | ||
* @var PHPExcel | ||
*/ | ||
protected $excel; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
protected $row = 1; | ||
|
||
/** | ||
* @param \SplFileObject $file File | ||
* @param string $sheet Sheet title (optional) | ||
* @param string $type Excel file type (defaults to Excel2007) | ||
* @param boolean $prependHeaderRow | ||
*/ | ||
public function __construct(\SplFileObject $file, $sheet = null, $type = 'Excel2007', $prependHeaderRow = false) | ||
{ | ||
$this->filename = $file->getPathname(); | ||
$this->sheet = $sheet; | ||
$this->type = $type; | ||
$this->prependHeaderRow = $prependHeaderRow; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function prepare() | ||
{ | ||
$reader = PHPExcel_IOFactory::createReader($this->type); | ||
if ($reader->canRead($this->filename)) { | ||
$this->excel = $reader->load($this->filename); | ||
} else { | ||
$this->excel = new PHPExcel(); | ||
} | ||
|
||
if (null !== $this->sheet) { | ||
if (!$this->excel->sheetNameExists($this->sheet)) { | ||
$this->excel->createSheet()->setTitle($this->sheet); | ||
} | ||
$this->excel->setActiveSheetIndexByName($this->sheet); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function writeItem(array $item) | ||
{ | ||
$count = count($item); | ||
|
||
if ($this->prependHeaderRow && 1 == $this->row) { | ||
$headers = array_keys($item); | ||
|
||
for ($i = 0; $i < $count; $i++) { | ||
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $headers[$i]); | ||
} | ||
$this->row++; | ||
} | ||
|
||
$values = array_values($item); | ||
|
||
for ($i = 0; $i < $count; $i++) { | ||
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $values[$i]); | ||
} | ||
|
||
$this->row++; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function finish() | ||
{ | ||
$writer = \PHPExcel_IOFactory::createWriter($this->excel, $this->type); | ||
$writer->save($this->filename); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters