Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ EEDataExporter
-------------
[![Build Status](https://api.travis-ci.org/EE/DataExporter.png?branch=master)](http://travis-ci.org/EE/DataExporter)

Easy export data to CSV, XML, JSON, Excel, HTML file or into memory.
Easy export data to CSV, XML, JSON, Excel, HTML, DOCX(with image) file or into memory.

[Read the Documentation and see examples](https://github.com/EE/DataExporter/blob/master/Resources/doc/index.md)

Expand Down
3 changes: 1 addition & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ parameters:
services:
ee.dataexporter:
class: %ee.dataexporter.class%
public: true
scope: prototype
public: true
89 changes: 87 additions & 2 deletions Service/DataExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ class DataExporter
/**
* @var array
*/
protected $supportedFormat = array('csv', 'xls', 'html', 'xml', 'json');
protected $supportedFormat = array('csv', 'xls', 'html', 'xml', 'json', 'docx');

/**
* @var array
*/
protected $hooks = array();
/**
*
* @var type
*/
protected $images = array('.jpeg', '.png', '.jpg');

/**
* @param $format
Expand Down Expand Up @@ -83,6 +89,9 @@ public function setOptions($format, $options = array())
} elseif ('html' === $this->format) {
//options for html
$this->openHTML();
} elseif ('docx' === $this->format) {
//options for html
$this->openDOCX();
} elseif ('xml' === $this->format) {
//options for xml
$this->openXML();
Expand Down Expand Up @@ -177,6 +186,27 @@ public function closeHTML()

return $this;
}

/**
* @return $this
*/
public function openDOCX()
{
$this->data = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=" . $this->charset . "\" /><meta name=\"Generator\" content=\"https://github.com/EE/DataExporter\">";
$this->data .= "<style type=\"text/css\"> table.CSSTable { border-width: 1px; border-spacing: 0px; border-style: solid; border-color: black; border-collapse: collapse; } table.CSSTable th { border-width: 1px; padding: 0px; border-style: solid; border-color: black; } table.CSSTable td { border-width: 1px; padding: 0px; border-style: solid; border-color: black; } </style>";
$this->data .= "<body><table class=\"CSSTable\" style=\"width:100%\">";
return $this;

}
/**
* @return $this
*/
public function closeDOCX()
{
$this->data .= "</table></body></html>";

return $this;
}

/**
* @param $data
Expand Down Expand Up @@ -298,6 +328,11 @@ public function setData($rows)
break;
case 'xls':
case 'html':
case 'docx':
if(in_array('skip', $this->columns)){
$key = array_search('skip', $this->columns);
unset($this->columns[$key]);
}
case 'xml':
$tempRow = '';
break;
Expand Down Expand Up @@ -329,6 +364,17 @@ function ($column) use ($row, $accessor, $separator, $escape, $hooks, $format) {
$this->data .= '<tr>';
foreach ($tempRow as $val) {
$this->data .= '<td>' . $val . '</td>';
}
$this->data .= '</tr>';
break;
case 'docx':
$this->data .= '<tr>';
foreach ($tempRow as $val) {
if (is_int($this->strposArray($val, $this->images))) {
$this->data .= '<td><img src="' . $val . '"/></td>';
} else {
$this->data .= '<td>' . $val . '</td>';
}
}
$this->data .= '</tr>';
break;
Expand Down Expand Up @@ -381,6 +427,21 @@ public function setColumns(Array $columns)
} else {
$this->data[] = $column . $this->separator;
}
} elseif ('docx' === $this->format) {
if (!in_array('skip', $columns)) {
//first item
reset($columns);
if ($key === key($columns)) {
$this->data .= '<tr>';
}

$this->data .= sprintf('<td>%s</td>', $column);
//last item
end($columns);
if ($key === key($columns)) {
$this->data .= '</tr>';
}
}
} elseif ('xls' === $this->format || 'html' === $this->format) {
//first item
reset($columns);
Expand Down Expand Up @@ -450,6 +511,12 @@ public function render()
$response->headers->set('Content-Type', 'application/xml');
$response->setContent($this->data);
break;
case 'docx':
//close tags
$this->closeDOCX();
$response->headers->set('Content-Type', 'application/vnd.ms-word');
$response->setContent($this->data);
break;
}

if ($this->memory) {
Expand All @@ -460,6 +527,24 @@ public function render()
$response->headers->set('Content-Disposition', 'attachment; filename="' . $this->fileName . '"');

return $response;

}

private function strposArray($haystack, $needles)
{
if ( is_array($needles) ) {
foreach ($needles as $str) {
if ( is_array($str) ) {
$pos = $this->strposArray($haystack, $str);
} else {
$pos = strpos($haystack, $str);
}
if (false !== $pos) {
return $pos;
}
}
} else {
return strpos($haystack, $needles);
}
}

}
18 changes: 18 additions & 0 deletions Tests/DataExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,25 @@ public function testHTMLExport()

$this->assertEquals($result, $exporter->render()->getContent());
}
public function testDOCXExport()
{
$exporter = new DataExporter();
$exporter->setOptions('docx', array('fileName' => 'file'));
$exporter->setColumns(array('[col1]' => 'Column 1', '[col2]' => 'Column 2', '[col3]' => 'Column 3'));
$exporter->setData(
array(
array('col1' => '1a', 'col2' => '1b', 'col3' => '1c'),
array('col1' => '2a', 'col2' => '2b'),
)
);

$result = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="Generator" content="https://github.com/EE/DataExporter">'
. '<style type="text/css"> table.CSSTable { border-width: 1px; border-spacing: 0px; border-style: solid; border-color: black; border-collapse: collapse; } table.CSSTable th { border-width: 1px; padding: 0px; border-style: solid; border-color: black; } table.CSSTable td { border-width: 1px; padding: 0px; border-style: solid; border-color: black; } </style>'
. '<body><table class="CSSTable" style="width:100%"><tr><td>Column 1</td><td>Column 2</td><td>Column 3</td></tr><tr><td>1a</td><td>1b</td><td>1c</td></tr><tr><td>2a</td><td>2b</td><td></td></tr>'
. '</table></body></html>';

$this->assertEquals($result, $exporter->render()->getContent());
}
public function testXMLExport()
{
$exporter = new DataExporter();
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "ee/dataexporter-bundle",
"type": "symfony-bundle",
"description": "Easy export data to CSV, XML, HTML, JSON or XLS",
"keywords": ["exporter","csv","xls", "xml", "json"],
"description": "Easy export data to CSV, XML, HTML, JSON, DOCX or XLS",
"keywords": ["exporter","csv","xls", "xml", "json", "docx"],
"homepage": "http://laboratorium.ee",
"license": "MIT",
"authors": [
Expand Down