/**
  * Attach the data table to an existing xlsx file. The file location is given via the
  * first parameter. If a second parameter is given the source file will not be overwritten
  * and a new file will be created.
  *
  * @param string srcFilename
  * @param string|null targetFilename
  * @return $this
  */
 public function attachToFile($srcFilename, $targetFilename = null)
 {
     $xlsx = new ExcelWorkbook($srcFilename);
     $worksheet = new ExcelWorksheet();
     if (!is_null($targetFilename)) {
         $xlsx->setFilename($targetFilename);
     }
     $worksheet->addRows($this->toArray());
     $xlsx->addWorksheet($worksheet, $this->sheetId, $this->sheetName)->save();
     unset($xlsx);
     return $this;
 }
Esempio n. 2
0
 /**
  * Add a worksheet into the workbook with id $id and name $name. If $id is null the last
  * worksheet is replaced. If $name is empty, its default value is 'Data'.
  *
  * Currently this replaces an existing worksheet. Adding new worksheets is not yet supported
  *
  * @param ExcelWorksheet $worksheet
  * @param int $id
  * @param string $name
  */
 public function addWorksheet(ExcelWorksheet $worksheet, $id = null, $name = 'Data')
 {
     if (is_null($id) || $id <= 0) {
         $lastId = 0;
         while ($this->getXLSX()->statName('xl/worksheets/sheet' . ($lastId + 1) . '.xml') !== false) {
             $lastId++;
         }
         $id = $lastId + $id;
     }
     $old = $this->getXLSX()->getFromName('xl/worksheets/sheet' . $id . '.xml');
     if ($old === false) {
         throw new \Exception('Appending new sheets is not yet implemented: ' . $id . ', ' . $this->srcFilename . ', ' . $this->targetFilename);
     } else {
         $document = new \DOMDocument();
         $document->loadXML($old);
         $oldSheetData = $document->getElementsByTagName('sheetData')->item(0);
         $worksheet->setDateTimeFormatId($this->dateTimeFormatId());
         $newSheetData = $document->importNode($worksheet->getDocument()->getElementsByTagName('sheetData')->item(0), true);
         $oldSheetData->parentNode->replaceChild($newSheetData, $oldSheetData);
         $xml = $document->saveXML();
         $this->getXLSX()->addFromString('xl/worksheets/sheet' . $id . '.xml', $xml);
     }
     if ($this->isAutoSaveEnabled()) {
         $this->save();
     }
     return $this;
 }
Esempio n. 3
0
 /**
  * Attach the data table to an existing xlsx file. The file location is given via the
  * first parameter. If a second parameter is given the source file will not be overwritten
  * and a new file will be created. The third parameter can be used to force updating the
  * auto calculation in the excel workbook.
  *
  * @param string srcFilename
  * @param string|null targetFilename
  * @param bool|null forceAutoCalculation
  * @return $this
  */
 public function attachToFile($srcFilename, $targetFilename = null, $forceAutoCalculation = false)
 {
     $calculatedColumns = null;
     if ($this->preserveFormulas) {
         $temp_xlsx = new ExcelWorkbook($srcFilename);
         $calculatedColumns = $temp_xlsx->getCalculatedColumns($this->preserveFormulas);
         unset($temp_xlsx);
     }
     $xlsx = new ExcelWorkbook($srcFilename);
     $worksheet = new ExcelWorksheet();
     if (!is_null($targetFilename)) {
         $xlsx->setFilename($targetFilename);
     }
     $worksheet->addRows($this->toArray(), $calculatedColumns);
     $xlsx->addWorksheet($worksheet, $this->sheetId, $this->sheetName);
     if ($forceAutoCalculation) {
         $xlsx->enableAutoCalculation();
     }
     if ($this->refreshTableRange) {
         $xlsx->refreshTableRange($this->refreshTableRange, count($this->data) + 1);
     }
     $xlsx->save();
     unset($xlsx);
     return $this;
 }