Skip to content

Commit

Permalink
ExcelWriter: Added option to prepend header row with column names
Browse files Browse the repository at this point in the history
  • Loading branch information
igormukhingmailcom authored and ddeboer committed May 30, 2015
1 parent fb9a47c commit 9a8fef6
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Writer/ExcelWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class ExcelWriter implements Writer
*/
protected $type;

/**
* @var boolean
*/
protected $prependHeaderRow;

/**
* @var PHPExcel
*/
Expand All @@ -42,12 +47,14 @@ class ExcelWriter implements Writer
* @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')
public function __construct(\SplFileObject $file, $sheet = null, $type = 'Excel2007', $prependHeaderRow = false)
{
$this->filename = $file->getPathname();
$this->sheet = $sheet;
$this->type = $type;
$this->prependHeaderRow = $prependHeaderRow;
}

/**
Expand Down Expand Up @@ -75,6 +82,17 @@ public function prepare()
*/
public function writeItem(array $item)
{
if ($this->prependHeaderRow && 1 == $this->row) {
$headers = array_keys($item);
$count = count($headers);
$values = array_values($headers);

for ($i = 0; $i < $count; $i++) {
$this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $values[$i]);
}
$this->row++;
}

$count = count($item);
$values = array_values($item);

Expand Down

0 comments on commit 9a8fef6

Please sign in to comment.