Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented start row and start column in excelDataWriter #6

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
36 changes: 30 additions & 6 deletions ExcelDataWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,36 @@ class ExcelDataWriter extends \yii\base\Object
public $defaultDateFormat = \PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY;

/**
* @var int Default value is `1`
* @var int Start row Default value is `1`
*/
protected $j = 1;
protected $j = 11;

/**
* @var int start column
*/
protected $startColumn = 0;

/**
* @param int $startRow
*/
public function setStartRow($startRow){
$this->j = $startRow;
}

/**
* @param int $startColumn
*/
public function setStartColumn($startColumn){
$this->startColumn = $startColumn;
}

public function write()
{
if (!is_array($this->data) || !is_array($this->columns)) {
return;
}

$this->j = 1;
//$this->j = 1;

$this->writeHeaderRow();
$this->writeDataRows();
Expand All @@ -51,7 +70,7 @@ public function write()

protected function writeHeaderRow()
{
$i = 0;
$i = $this->startColumn;
foreach ($this->columns as $column) {
if (isset($column['header'])) {
$this->sheet->setCellValueByColumnAndRow($i, $this->j, $column['header']);
Expand All @@ -70,7 +89,7 @@ protected function writeHeaderRow()
protected function writeDataRows()
{
foreach ($this->data as $key => $row) {
$i = 0;
$i = $this->startColumn;
if (isset($this->options['rowOptions']) && $this->options['rowOptions'] instanceof \Closure) {
$rowOptions = call_user_func($this->options['rowOptions'], $row, $key);
}
Expand All @@ -87,16 +106,21 @@ protected function writeDataRows()
} elseif (isset($column['attribute']) && isset($row[$column['attribute']])) {
$value = $row[$column['attribute']];
}

// dump($column['attribute']);
$this->writeCell($value, $i, $this->j, $column);
++$i;
}
if($this->j >100) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

// return;
}
++$this->j;
}
}

protected function writeFooterRow()
{
$i = 0;
$i = $this->startColumn;
foreach ($this->columns as $column) {
// footer config
$config = [];
Expand Down
66 changes: 64 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,71 @@ TDB

Usage
-----
```
```php

use alexgx\phpexcel\PhpExcel;

$phpExcel = new PhpExcel();
$objPHPExcel = $phpExcel->create();

$objPHPExcel->getProperties()->setCreator("Traiding")
->setLastModifiedBy("Uldis Nelsons")
->setTitle("Packing list")
->setSubject("Packing list");

$activeSheet = $objPHPExcel->setActiveSheetIndex(0);
$activeSheet->setTitle('Packing List')
->setCellValue('A1', 'PACKING LIST')
->setCellValue('A3', 'VESSEL:')
->setCellValue('A4', 'B/L date:')
->setCellValue('A5', 'B/L No.:');



$activeSheet->getStyle('A1')->getFont()->setBold(true)->setSize(14);
$activeSheet->getStyle('A3:c9')->getFont()->setBold(true)->setSize(11);

$writer = new ExcelDataWriter();
$writer->setStartRow(11);
$writer->sheet = $activeSheet;
$writer->data = $packingList;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add array


$headerStyles = [
'font' => [
'bold' => true
]
];
$writer->columns = [
[
'attribute' => 'departure_date',
'header' => 'Departure Date',
'headerStyles' => $headerStyles,
],
[
'attribute' => 'car_number',
'header' => 'Car Number',
'headerStyles' => $headerStyles,
],
[
'attribute' => 'delivery_note',
'header' => 'Delivery Note',
'headerStyles' => $headerStyles,
],
[
'attribute' => 'weight',
'header' => 'Weight',
'headerStyles' => $headerStyles,
],
[
'attribute' => 'gtd',
'header' => 'Gtd',
'headerStyles' => $headerStyles,
]
];

$writer->write();
$phpExcel->responseFile($objPHPExcel, 'packing.xls');

use alexgx\phpexcel;

```
TBD
Expand Down