Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Excel.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class Excel
14 {
20  protected $_iterator = null;
21 
27  protected $_rowCallback = [];
28 
34  protected $_dataHeader = [];
35 
41  protected $_dataFooter = [];
42 
49  public function __construct(\Iterator $iterator, $rowCallback = [])
50  {
51  $this->_iterator = $iterator;
52  $this->_rowCallback = $rowCallback;
53  }
54 
62  protected function _getXmlHeader($sheetName = '')
63  {
64  if (empty($sheetName)) {
65  $sheetName = 'Sheet 1';
66  }
67 
68  $sheetName = htmlspecialchars($sheetName);
69 
70  $xmlHeader = '<' .
71  '?xml version="1.0"?' .
72  '><' .
73  '?mso-application progid="Excel.Sheet"?' .
74  '><Workbook' .
75  ' xmlns="urn:schemas-microsoft-com:office:spreadsheet"' .
76  ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' .
77  ' xmlns:x="urn:schemas-microsoft-com:office:excel"' .
78  ' xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml"' .
79  ' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"' .
80  ' xmlns:o="urn:schemas-microsoft-com:office:office"' .
81  ' xmlns:html="http://www.w3.org/TR/REC-html40"' .
82  ' xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">' .
83  '<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">' .
84  '</OfficeDocumentSettings>' .
85  '<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">' .
86  '</ExcelWorkbook>' .
87  '<Worksheet ss:Name="' .
88  $sheetName .
89  '">' .
90  '<Table>';
91 
92  if ($this->_dataHeader) {
93  $xmlHeader .= $this->_getXmlRow($this->_dataHeader, false);
94  }
95 
96  return $xmlHeader;
97  }
98 
105  protected function _getXmlFooter()
106  {
107  $xmlFooter = '';
108 
109  if ($this->_dataFooter) {
110  $xmlFooter = $this->_getXmlRow($this->_dataFooter, false);
111  }
112 
113  $xmlFooter .= '</Table></Worksheet></Workbook>';
114 
115  return $xmlFooter;
116  }
117 
127  protected function _getXmlRow($row, $useCallback)
128  {
129  if ($useCallback && $this->_rowCallback) {
130  $row = call_user_func($this->_rowCallback, $row);
131  }
132  $xmlData = [];
133  $xmlData[] = '<Row>';
134 
135  foreach ($row as $value) {
136  $value = htmlspecialchars($value);
137  $dataType = is_numeric($value) && $value[0] !== '+' && $value[0] !== '0' ? 'Number' : 'String';
138 
145  if (!is_string($value)) {
146  $value = (string)$value;
147  }
148  if (isset($value[0]) && in_array($value[0], ['=', '+', '-'])) {
149  $value = ' ' . $value;
150  $dataType = 'String';
151  }
152 
153  $value = str_replace("\r\n", '&#10;', $value);
154  $value = str_replace("\r", '&#10;', $value);
155  $value = str_replace("\n", '&#10;', $value);
156 
157  $xmlData[] = '<Cell><Data ss:Type="' . $dataType . '">' . $value . '</Data></Cell>';
158  }
159  $xmlData[] = '</Row>';
160 
161  return join('', $xmlData);
162  }
163 
170  public function setDataHeader($data)
171  {
172  $this->_dataHeader = $data;
173  }
174 
181  public function setDataFooter($data)
182  {
183  $this->_dataFooter = $data;
184  }
185 
192  public function convert($sheetName = '')
193  {
194  $xml = $this->_getXmlHeader($sheetName);
195 
196  foreach ($this->_iterator as $dataRow) {
197  $xml .= $this->_getXmlRow($dataRow, true);
198  }
199  $xml .= $this->_getXmlFooter();
200 
201  return $xml;
202  }
203 
211  public function write(WriteInterface $stream, $sheetName = '')
212  {
213  $stream->write($this->_getXmlHeader($sheetName));
214 
215  foreach ($this->_iterator as $dataRow) {
216  $stream->write($this->_getXmlRow($dataRow, true));
217  }
218  $stream->write($this->_getXmlFooter());
219  }
220 }
write(WriteInterface $stream, $sheetName='')
Definition: Excel.php:211
__construct(\Iterator $iterator, $rowCallback=[])
Definition: Excel.php:49
_getXmlHeader($sheetName='')
Definition: Excel.php:62
$value
Definition: gender.phtml:16