Implements basic writing functionality

This commit is contained in:
Alexander Pervakov 2014-05-30 10:44:22 +04:00
parent 2376bef3c2
commit f51ed2c709

View File

@ -36,6 +36,9 @@
*/
class PHPExcel_Writer_OpenDocument_Content extends PHPExcel_Writer_OpenDocument_WriterPart
{
const NUMBER_COLS_REPEATED_MAX = 1024;
const NUMBER_ROWS_REPEATED_MAX = 1048576;
/**
* Write content.xml to XML format
*
@ -46,7 +49,7 @@ class PHPExcel_Writer_OpenDocument_Content extends PHPExcel_Writer_OpenDocument_
public function write(PHPExcel $pPHPExcel = null)
{
if (!$pPHPExcel) {
$pPHPExcel = $this->getParentWriter()->getPHPExcel();
$pPHPExcel = $this->getParentWriter()->getPHPExcel(); /* @var $pPHPExcel PHPExcel */
}
$objWriter = null;
@ -102,12 +105,7 @@ class PHPExcel_Writer_OpenDocument_Content extends PHPExcel_Writer_OpenDocument_
$objWriter->startElement('office:body');
$objWriter->startElement('office:spreadsheet');
$objWriter->writeElement('table:calculation-settings');
$objWriter->startElement('table:table');
$objWriter->writeAttribute('table:name', '');
$objWriter->writeElement('office:forms');
$objWriter->writeElement('table:table-column');
$objWriter->writeElement('table:table-row');
$objWriter->endElement();
$this->_writeSheets($objWriter);
$objWriter->writeElement('table:named-expressions');
$objWriter->endElement();
$objWriter->endElement();
@ -115,4 +113,155 @@ class PHPExcel_Writer_OpenDocument_Content extends PHPExcel_Writer_OpenDocument_
return $objWriter->getData();
}
/**
* Write sheets
*
* @param PHPExcel_Shared_XMLWriter $objWriter
*/
private function _writeSheets(PHPExcel_Shared_XMLWriter $objWriter)
{
$pPHPExcel = $this->getParentWriter()->getPHPExcel(); /* @var $pPHPExcel PHPExcel */
$sheet_count = $pPHPExcel->getSheetCount();
for ($i = 0; $i < $sheet_count; $i++) {
//$this->getWriterPart('Worksheet')->writeWorksheet());
$objWriter->startElement('table:table');
$objWriter->writeAttribute('table:name', $pPHPExcel->getSheet($i)->getTitle());
$objWriter->writeElement('office:forms');
$objWriter->startElement('table:table-column');
$objWriter->writeAttribute('table:number-columns-repeated', self::NUMBER_COLS_REPEATED_MAX);
$objWriter->endElement();
$this->_writeRows($objWriter, $pPHPExcel->getSheet($i));
$objWriter->endElement();
}
}
/**
* Write rows of the specified sheet
*
* @param PHPExcel_Shared_XMLWriter $objWriter
* @param PHPExcel_Worksheet $sheet
*/
private function _writeRows(PHPExcel_Shared_XMLWriter $objWriter, PHPExcel_Worksheet $sheet)
{
$number_rows_repeated = self::NUMBER_ROWS_REPEATED_MAX;
$span_row = 0;
$rows = $sheet->getRowIterator();
while ($rows->valid()) {
$number_rows_repeated--;
$row = $rows->current();
if ($row->getCellIterator()->valid()) {
if ($span_row) {
$objWriter->startElement('table:table-row');
if ($span_row > 1) {
$objWriter->writeAttribute('table:number-rows-repeated', $span_row);
}
$objWriter->startElement('table:table-cell');
$objWriter->writeAttribute('table:number-columns-repeated', self::NUMBER_COLS_REPEATED_MAX);
$objWriter->endElement();
$objWriter->endElement();
$span_row = 0;
}
$objWriter->startElement('table:table-row');
$this->_writeCells($objWriter, $row);
$objWriter->endElement();
} else {
$span_row++;
}
$rows->next();
}
}
/**
* Write cells of the specified row
*
* @param PHPExcel_Shared_XMLWriter $objWriter
* @param PHPExcel_Worksheet_Row $row
* @throws PHPExcel_Writer_Exception
*/
private function _writeCells(PHPExcel_Shared_XMLWriter $objWriter, PHPExcel_Worksheet_Row $row)
{
$number_cols_repeated = self::NUMBER_COLS_REPEATED_MAX;
$prev_column = -1;
$cells = $row->getCellIterator();
while ($cells->valid()) {
$cell = $cells->current();
$column = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1;
$this->_writeCellSpan($objWriter, $column, $prev_column);
$objWriter->startElement('table:table-cell');
switch ($cell->getDataType()) {
case PHPExcel_Cell_DataType::TYPE_BOOL:
$objWriter->writeAttribute('office:value-type', 'boolean');
$objWriter->writeAttribute('office:value', $cell->getValue());
$objWriter->writeElement('text:p', $cell->getValue());
break;
case PHPExcel_Cell_DataType::TYPE_ERROR:
throw new PHPExcel_Writer_Exception('Writing of error not implemented yet.');
break;
case PHPExcel_Cell_DataType::TYPE_FORMULA:
try {
$formula_value = PHPExcel_Calculation::getInstance()->calculateCellValue($cell);
} catch (Exception $e) {
$formula_value = $cell->getValue();
}
$objWriter->writeAttribute('table:formula', 'of:' . $cell->getValue());
$objWriter->writeAttribute('office:value-type', 'float');
$objWriter->writeAttribute('office:value', $formula_value);
$objWriter->writeElement('text:p', $formula_value);
break;
case PHPExcel_Cell_DataType::TYPE_INLINE:
throw new PHPExcel_Writer_Exception('Writing of inline not implemented yet.');
break;
case PHPExcel_Cell_DataType::TYPE_NUMERIC:
$objWriter->writeAttribute('office:value-type', 'float');
$objWriter->writeAttribute('office:value', $cell->getValue());
$objWriter->writeElement('text:p', $cell->getValue());
break;
case PHPExcel_Cell_DataType::TYPE_STRING:
$objWriter->writeAttribute('office:value-type', 'string');
$objWriter->writeElement('text:p', $cell->getValue());
break;
}
$objWriter->endElement();
$prev_column = $column;
$cells->next();
}
$number_cols_repeated = $number_cols_repeated - $prev_column - 1;
if ($number_cols_repeated > 0) {
if ($number_cols_repeated > 1) {
$objWriter->startElement('table:table-cell');
$objWriter->writeAttribute('table:number-columns-repeated', $number_cols_repeated);
$objWriter->endElement();
} else {
$objWriter->writeElement('table:table-cell');
}
}
}
/**
* Write span
*
* @param PHPExcel_Shared_XMLWriter $objWriter
* @param integer $curColumn
* @param integer $prevColumn
*/
private function _writeCellSpan(PHPExcel_Shared_XMLWriter $objWriter, $curColumn, $prevColumn)
{
$diff = $curColumn - $prevColumn - 1;
if (1 === $diff) {
$objWriter->writeElement('table:table-cell');
} elseif ($diff > 1) {
$objWriter->startElement('table:table-cell');
$objWriter->writeAttribute('table:number-columns-repeated', $diff);
$objWriter->endElement();
}
}
}