Skip to content
This repository was archived by the owner on Jan 2, 2019. It is now read-only.

Decoupling PHPExcel and Writers #402

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
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
35 changes: 35 additions & 0 deletions Classes/PHPExcel/Writer/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ abstract class PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
*/
protected $_diskCachingDirectory = './';

/**
* PHPExcel instance
*
* @var PHPExcel
*/
protected $_phpExcel;

/**
* Write charts in workbook?
* If this is true, then the Writer will write definitions for any charts that exist in the PHPExcel object.
Expand Down Expand Up @@ -155,4 +162,32 @@ public function setUseDiskCaching($pValue = FALSE, $pDirectory = NULL) {
public function getDiskCachingDirectory() {
return $this->_diskCachingDirectory;
}

/**
* Get PHPExcel object
*
* @return PHPExcel
* @throws PHPExcel_Writer_Exception
*/
public function getPHPExcel()
{
if ($this->_phpExcel !== null) {
return $this->_phpExcel;
} else {
throw new PHPExcel_Writer_Exception('No PHPExcel assigned.');
}
}

/**
* Set PHPExcel object
*
* @param PHPExcel $pPHPExcel PHPExcel object
* @throws PHPExcel_Writer_Exception
* @return PHPExcel_Writer_IWriter
*/
public function setPHPExcel(PHPExcel $pPHPExcel = null)
{
$this->_phpExcel = $pPHPExcel;
return $this;
}
}
19 changes: 9 additions & 10 deletions Classes/PHPExcel/Writer/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,8 @@
* @package PHPExcel_Writer_CSV
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Writer_CSV extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter {
/**
* PHPExcel object
*
* @var PHPExcel
*/
private $_phpExcel;

class PHPExcel_Writer_CSV extends PHPExcel_Writer_Abstract
{
/**
* Delimiter
*
Expand Down Expand Up @@ -89,7 +83,7 @@ class PHPExcel_Writer_CSV extends PHPExcel_Writer_Abstract implements PHPExcel_W
* @param PHPExcel $phpExcel PHPExcel object
*/
public function __construct(PHPExcel $phpExcel) {
$this->_phpExcel = $phpExcel;
$this->setPHPExcel($phpExcel);
}

/**
Expand All @@ -98,7 +92,12 @@ public function __construct(PHPExcel $phpExcel) {
* @param string $pFilename
* @throws PHPExcel_Writer_Exception
*/
public function save($pFilename = null) {
public function save($pFilename = null)
{
if (!$this->_phpExcel) {
throw new PHPExcel_Writer_Exception('PHPExcel object unassigned.');
}

// Fetch sheet
$sheet = $this->_phpExcel->getSheet($this->_sheetIndex);

Expand Down
137 changes: 52 additions & 85 deletions Classes/PHPExcel/Writer/Excel2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @package PHPExcel_Writer_2007
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Writer_Excel2007 extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
class PHPExcel_Writer_Excel2007 extends PHPExcel_Writer_Abstract
{
/**
* Pre-calculate formulas
Expand All @@ -60,13 +60,6 @@ class PHPExcel_Writer_Excel2007 extends PHPExcel_Writer_Abstract implements PHPE
*/
private $_writerParts = array();

/**
* Private PHPExcel
*
* @var PHPExcel
*/
private $_spreadSheet;

/**
* Private string table
*
Expand Down Expand Up @@ -187,9 +180,9 @@ public function getWriterPart($pPartName = '') {
*/
public function save($pFilename = null)
{
if ($this->_spreadSheet !== NULL) {
if ($this->_phpExcel !== NULL) {
// garbage collect
$this->_spreadSheet->garbageCollect();
$this->_phpExcel->garbageCollect();

// If $pFilename is php://output or php://stdout, make it a temporary file...
$originalFilename = $pFilename;
Expand All @@ -200,27 +193,27 @@ public function save($pFilename = null)
}
}

$saveDebugLog = PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->getWriteDebugLog();
PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->setWriteDebugLog(FALSE);
$saveDebugLog = PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->getWriteDebugLog();
PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog(FALSE);
$saveDateReturnType = PHPExcel_Calculation_Functions::getReturnDateType();
PHPExcel_Calculation_Functions::setReturnDateType(PHPExcel_Calculation_Functions::RETURNDATE_EXCEL);

// Create string lookup table
$this->_stringTable = array();
for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
$this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_spreadSheet->getSheet($i), $this->_stringTable);
for ($i = 0; $i < $this->_phpExcel->getSheetCount(); ++$i) {
$this->_stringTable = $this->getWriterPart('StringTable')->createStringTable($this->_phpExcel->getSheet($i), $this->_stringTable);
}

// Create styles dictionaries
$this->_styleHashTable->addFromSource( $this->getWriterPart('Style')->allStyles($this->_spreadSheet) );
$this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_spreadSheet) );
$this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_spreadSheet) );
$this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_spreadSheet) );
$this->_bordersHashTable->addFromSource( $this->getWriterPart('Style')->allBorders($this->_spreadSheet) );
$this->_numFmtHashTable->addFromSource( $this->getWriterPart('Style')->allNumberFormats($this->_spreadSheet) );
$this->_styleHashTable->addFromSource( $this->getWriterPart('Style')->allStyles($this->_phpExcel) );
$this->_stylesConditionalHashTable->addFromSource( $this->getWriterPart('Style')->allConditionalStyles($this->_phpExcel) );
$this->_fillHashTable->addFromSource( $this->getWriterPart('Style')->allFills($this->_phpExcel) );
$this->_fontHashTable->addFromSource( $this->getWriterPart('Style')->allFonts($this->_phpExcel) );
$this->_bordersHashTable->addFromSource( $this->getWriterPart('Style')->allBorders($this->_phpExcel) );
$this->_numFmtHashTable->addFromSource( $this->getWriterPart('Style')->allNumberFormats($this->_phpExcel) );

// Create drawing dictionary
$this->_drawingHashTable->addFromSource( $this->getWriterPart('Drawing')->allDrawings($this->_spreadSheet) );
$this->_drawingHashTable->addFromSource( $this->getWriterPart('Drawing')->allDrawings($this->_phpExcel) );

// Create new ZIP file and open it for writing
$zipClass = PHPExcel_Settings::getZipClass();
Expand All @@ -243,67 +236,67 @@ public function save($pFilename = null)
}

// Add [Content_Types].xml to ZIP file
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_spreadSheet, $this->_includeCharts));
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->_phpExcel, $this->_includeCharts));

//if hasMacros, add the vbaProject.bin file, Certificate file(if exists)
if($this->_spreadSheet->hasMacros()){
$macrosCode=$this->_spreadSheet->getMacrosCode();
if($this->_phpExcel->hasMacros()){
$macrosCode=$this->_phpExcel->getMacrosCode();
if(!is_null($macrosCode)){// we have the code ?
$objZip->addFromString('xl/vbaProject.bin', $macrosCode);//allways in 'xl', allways named vbaProject.bin
if($this->_spreadSheet->hasMacrosCertificate()){//signed macros ?
if($this->_phpExcel->hasMacrosCertificate()){//signed macros ?
// Yes : add the certificate file and the related rels file
$objZip->addFromString('xl/vbaProjectSignature.bin', $this->_spreadSheet->getMacrosCertificate());
$objZip->addFromString('xl/vbaProjectSignature.bin', $this->_phpExcel->getMacrosCertificate());
$objZip->addFromString('xl/_rels/vbaProject.bin.rels',
$this->getWriterPart('RelsVBA')->writeVBARelationships($this->_spreadSheet));
$this->getWriterPart('RelsVBA')->writeVBARelationships($this->_phpExcel));
}
}
}
//a custom UI in this workbook ? add it ("base" xml and additional objects (pictures) and rels)
if($this->_spreadSheet->hasRibbon()){
$tmpRibbonTarget=$this->_spreadSheet->getRibbonXMLData('target');
$objZip->addFromString($tmpRibbonTarget, $this->_spreadSheet->getRibbonXMLData('data'));
if($this->_spreadSheet->hasRibbonBinObjects()){
if($this->_phpExcel->hasRibbon()){
$tmpRibbonTarget=$this->_phpExcel->getRibbonXMLData('target');
$objZip->addFromString($tmpRibbonTarget, $this->_phpExcel->getRibbonXMLData('data'));
if($this->_phpExcel->hasRibbonBinObjects()){
$tmpRootPath=dirname($tmpRibbonTarget).'/';
$ribbonBinObjects=$this->_spreadSheet->getRibbonBinObjects('data');//the files to write
$ribbonBinObjects=$this->_phpExcel->getRibbonBinObjects('data');//the files to write
foreach($ribbonBinObjects as $aPath=>$aContent){
$objZip->addFromString($tmpRootPath.$aPath, $aContent);
}
//the rels for files
$objZip->addFromString($tmpRootPath.'_rels/'.basename($tmpRibbonTarget).'.rels',
$this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->_spreadSheet));
$this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->_phpExcel));
}
}

// Add relationships to ZIP file
$objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_spreadSheet));
$objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_spreadSheet));
$objZip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->_phpExcel));
$objZip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->_phpExcel));

// Add document properties to ZIP file
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_spreadSheet));
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_spreadSheet));
$customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->_spreadSheet);
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->_phpExcel));
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->_phpExcel));
$customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->_phpExcel);
if ($customPropertiesPart !== NULL) {
$objZip->addFromString('docProps/custom.xml', $customPropertiesPart);
}

// Add theme to ZIP file
$objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_spreadSheet));
$objZip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->_phpExcel));

// Add string table to ZIP file
$objZip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->_stringTable));

// Add styles to ZIP file
$objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_spreadSheet));
$objZip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->_phpExcel));

// Add workbook to ZIP file
$objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_spreadSheet, $this->_preCalculateFormulas));
$objZip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->_phpExcel, $this->_preCalculateFormulas));

$chartCount = 0;
// Add worksheets
for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
$objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_spreadSheet->getSheet($i), $this->_stringTable, $this->_includeCharts));
for ($i = 0; $i < $this->_phpExcel->getSheetCount(); ++$i) {
$objZip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->_phpExcel->getSheet($i), $this->_stringTable, $this->_includeCharts));
if ($this->_includeCharts) {
$charts = $this->_spreadSheet->getSheet($i)->getChartCollection();
$charts = $this->_phpExcel->getSheet($i)->getChartCollection();
if (count($charts) > 0) {
foreach($charts as $chart) {
$objZip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart));
Expand All @@ -315,45 +308,45 @@ public function save($pFilename = null)

$chartRef1 = $chartRef2 = 0;
// Add worksheet relationships (drawings, ...)
for ($i = 0; $i < $this->_spreadSheet->getSheetCount(); ++$i) {
for ($i = 0; $i < $this->_phpExcel->getSheetCount(); ++$i) {

// Add relationships
$objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_spreadSheet->getSheet($i), ($i + 1), $this->_includeCharts));
$objZip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->_phpExcel->getSheet($i), ($i + 1), $this->_includeCharts));

$drawings = $this->_spreadSheet->getSheet($i)->getDrawingCollection();
$drawings = $this->_phpExcel->getSheet($i)->getDrawingCollection();
$drawingCount = count($drawings);
if ($this->_includeCharts) {
$chartCount = $this->_spreadSheet->getSheet($i)->getChartCount();
$chartCount = $this->_phpExcel->getSheet($i)->getChartCount();
}

// Add drawing and image relationship parts
if (($drawingCount > 0) || ($chartCount > 0)) {
// Drawing relationships
$objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_spreadSheet->getSheet($i),$chartRef1, $this->_includeCharts));
$objZip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->_phpExcel->getSheet($i),$chartRef1, $this->_includeCharts));

// Drawings
$objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_spreadSheet->getSheet($i),$chartRef2,$this->_includeCharts));
$objZip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->_phpExcel->getSheet($i),$chartRef2,$this->_includeCharts));
}

// Add comment relationship parts
if (count($this->_spreadSheet->getSheet($i)->getComments()) > 0) {
if (count($this->_phpExcel->getSheet($i)->getComments()) > 0) {
// VML Comments
$objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_spreadSheet->getSheet($i)));
$objZip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->_phpExcel->getSheet($i)));

// Comments
$objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_spreadSheet->getSheet($i)));
$objZip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->_phpExcel->getSheet($i)));
}

// Add header/footer relationship parts
if (count($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
if (count($this->_phpExcel->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
// VML Drawings
$objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_spreadSheet->getSheet($i)));
$objZip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->_phpExcel->getSheet($i)));

// VML Drawing relationships
$objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_spreadSheet->getSheet($i)));
$objZip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->_phpExcel->getSheet($i)));

// Media
foreach ($this->_spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
foreach ($this->_phpExcel->getSheet($i)->getHeaderFooter()->getImages() as $image) {
$objZip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
}
}
Expand Down Expand Up @@ -392,7 +385,7 @@ public function save($pFilename = null)
}

PHPExcel_Calculation_Functions::setReturnDateType($saveDateReturnType);
PHPExcel_Calculation::getInstance($this->_spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
PHPExcel_Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog);

// Close file
if ($objZip->close() === false) {
Expand All @@ -411,32 +404,6 @@ public function save($pFilename = null)
}
}

/**
* Get PHPExcel object
*
* @return PHPExcel
* @throws PHPExcel_Writer_Exception
*/
public function getPHPExcel() {
if ($this->_spreadSheet !== null) {
return $this->_spreadSheet;
} else {
throw new PHPExcel_Writer_Exception("No PHPExcel assigned.");
}
}

/**
* Set PHPExcel object
*
* @param PHPExcel $pPHPExcel PHPExcel object
* @throws PHPExcel_Writer_Exception
* @return PHPExcel_Writer_Excel2007
*/
public function setPHPExcel(PHPExcel $pPHPExcel = null) {
$this->_spreadSheet = $pPHPExcel;
return $this;
}

/**
* Get string table
*
Expand Down
17 changes: 7 additions & 10 deletions Classes/PHPExcel/Writer/Excel5.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,8 @@
* @package PHPExcel_Writer_Excel5
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Writer_Excel5 extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
class PHPExcel_Writer_Excel5 extends PHPExcel_Writer_Abstract
{
/**
* PHPExcel object
*
* @var PHPExcel
*/
private $_phpExcel;

/**
* Total number of shared strings in workbook
*
Expand Down Expand Up @@ -104,7 +97,7 @@ class PHPExcel_Writer_Excel5 extends PHPExcel_Writer_Abstract implements PHPExce
* @param PHPExcel $phpExcel PHPExcel object
*/
public function __construct(PHPExcel $phpExcel) {
$this->_phpExcel = $phpExcel;
$this->setPHPExcel($phpExcel);

$this->_parser = new PHPExcel_Writer_Excel5_Parser();
}
Expand All @@ -115,7 +108,11 @@ public function __construct(PHPExcel $phpExcel) {
* @param string $pFilename
* @throws PHPExcel_Writer_Exception
*/
public function save($pFilename = null) {
public function save($pFilename = null)
{
if (!$this->_phpExcel) {
throw new PHPExcel_Writer_Exception('PHPExcel object unassigned.');
}

// garbage collect
$this->_phpExcel->garbageCollect();
Expand Down
Loading