ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Excel5.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
34  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
35 }
36 
38 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/IWriter.php';
39 
41 require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
42 
44 require_once PHPEXCEL_ROOT . 'PHPExcel/HashTable.php';
45 
47 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/OLE/OLE_Root.php';
48 
50 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/OLE/OLE_File.php';
51 
53 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel5/Parser.php';
54 
56 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel5/Workbook.php';
57 
58 
67 {
73  private $_phpExcel;
74 
80  private $_BIFF_version;
81 
87  private $_tempDir = '';
88 
94  private $_str_total;
95 
101  private $_str_unique;
102 
108  private $_str_table;
109 
115  private $_parser;
116 
117 
123  public function __construct(PHPExcel $phpExcel) {
124  $this->_phpExcel = $phpExcel;
125  $this->_BIFF_version = 0x0600;
126  $this->_tempDir = '';
127 
128  $this->_str_total = 0;
129  $this->_str_unique = 0;
130  $this->_str_table = array();
131  $this->_parser = new PHPExcel_Writer_Excel5_Parser($this->_BIFF_version);
132 
133  }
134 
141  public function save($pFilename = null) {
142 
143  // check mbstring.func_overload
144  if (ini_get('mbstring.func_overload') != 0) {
145  throw new Exception('Multibyte string function overloading in PHP must be disabled.');
146  }
147 
148  // garbage collect
149  $this->_phpExcel->garbageCollect();
150 
153 
154  // Initialise workbook writer
155  $this->_writerWorkbook = new PHPExcel_Writer_Excel5_Workbook($this->_phpExcel, $this->_BIFF_version,
156  $this->_str_total, $this->_str_unique, $this->_str_table, $this->_parser, $this->_tempDir);
157 
158  // Initialise worksheet writers
159  $countSheets = count($this->_phpExcel->getAllSheets());
160  for ($i = 0; $i < $countSheets; ++$i) {
161  $phpSheet = $this->_phpExcel->getSheet($i);
162 
163  $writerWorksheet = new PHPExcel_Writer_Excel5_Worksheet($this->_BIFF_version,
164  $this->_str_total, $this->_str_unique,
165  $this->_str_table,
166  $this->_parser, $this->_tempDir,
167  $phpSheet);
168  $this->_writerWorksheets[$i] = $writerWorksheet;
169  }
170 
171  // add 15 identical cell style Xfs
172  // for now, we use the first cellXf instead of cellStyleXf
173  $cellXfCollection = $this->_phpExcel->getCellXfCollection();
174  for ($i = 0; $i < 15; ++$i) {
175  $this->_writerWorkbook->addXfWriter($cellXfCollection[0], true);
176  }
177 
178  // add all the cell Xfs
179  foreach ($this->_phpExcel->getCellXfCollection() as $style) {
180  $this->_writerWorkbook->addXfWriter($style, false);
181  }
182 
183  // initialize OLE file
184  $workbookStreamName = ($this->_BIFF_version == 0x0600) ? 'Workbook' : 'Book';
185  $OLE = new PHPExcel_Shared_OLE_PPS_File(PHPExcel_Shared_OLE::Asc2Ucs($workbookStreamName));
186 
187  if ($this->_tempDir != '') {
188  $OLE->setTempDir($this->_tempDir);
189  }
190  $res = $OLE->init();
191 
192  // Write the worksheet streams before the global workbook stream,
193  // because the byte sizes of these are needed in the global workbook stream
194  $worksheetSizes = array();
195  for ($i = 0; $i < $countSheets; ++$i) {
196  $this->_writerWorksheets[$i]->close();
197  $worksheetSizes[] = $this->_writerWorksheets[$i]->_datasize;
198  }
199 
200  // add binary data for global workbook stream
201  $OLE->append( $this->_writerWorkbook->writeWorkbook($worksheetSizes) );
202 
203  // add binary data for sheet streams
204  for ($i = 0; $i < $countSheets; ++$i) {
205  while ( ($tmp = $this->_writerWorksheets[$i]->getData()) !== false ) {
206  $OLE->append($tmp);
207  }
208  }
209 
210  $root = new PHPExcel_Shared_OLE_PPS_Root(time(), time(), array($OLE));
211  if ($this->_tempDir != '') {
212  $root->setTempDir($this->_tempDir);
213  }
214 
215  // save the OLE file
216  $res = $root->save($pFilename);
217 
219 
220  // clean up
221  foreach ($this->_writerWorksheets as $sheet) {
222  $sheet->cleanup();
223  }
224  }
225 
231  public function getTempDir() {
232  return $this->_tempDir;
233  }
234 
242  public function setTempDir($pValue = '') {
243  if (is_dir($pValue)) {
244  $this->_tempDir = $pValue;
245  } else {
246  throw new Exception("Directory does not exist: $pValue");
247  }
248  return $this;
249  }
250 
251 }