Added support for colspan attribute

This commit is contained in:
Mark Baker 2013-01-16 22:11:26 +00:00
parent 092fc7b5f7
commit 8bcfc3bf37
3 changed files with 153 additions and 24 deletions

View File

@ -201,7 +201,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// Simple String content // Simple String content
if (trim($cellContent) > '') { if (trim($cellContent) > '') {
// Only actually write it if there's content in the string // Only actually write it if there's content in the string
// echo 'FLUSH CELL: ' , $column , $row , ' => ' , $cellContent , '<br />'; //echo 'FLUSH CELL: ' , $column , $row , ' => ' , $cellContent , PHP_EOL;
// Write to worksheet to be done here... // Write to worksheet to be done here...
// ... we return the cell so we can mess about with styles more easily // ... we return the cell so we can mess about with styles more easily
$cell = $sheet->setCellValue($column.$row,$cellContent,true); $cell = $sheet->setCellValue($column.$row,$cellContent,true);
@ -215,6 +215,8 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
$cellContent = (string) ''; $cellContent = (string) '';
} }
private $_columnAdd = 1;
private function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent){ private function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent){
foreach($element->childNodes as $child){ foreach($element->childNodes as $child){
if ($child instanceof DOMText) { if ($child instanceof DOMText) {
@ -227,11 +229,11 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// TODO // TODO
} }
} elseif($child instanceof DOMElement) { } elseif($child instanceof DOMElement) {
// echo '<b>DOM ELEMENT: </b>' , strtoupper($child->nodeName) , '<br />'; //echo '**DOM ELEMENT:** ' , strtoupper($child->nodeName) , PHP_EOL;
$attributeArray = array(); $attributeArray = array();
foreach($child->attributes as $attribute) { foreach($child->attributes as $attribute) {
// echo '<b>ATTRIBUTE: </b>' , $attribute->name , ' => ' , $attribute->value , '<br />'; //echo '**ATTRIBUTE:** ' , $attribute->name , ' => ' , $attribute->value , PHP_EOL;
$attributeArray[$attribute->name] = $attribute->value; $attributeArray[$attribute->name] = $attribute->value;
} }
@ -259,13 +261,13 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
case 'em' : case 'em' :
case 'strong': case 'strong':
case 'b' : case 'b' :
// echo 'STYLING, SPAN OR DIV<br />'; //echo 'STYLING, SPAN OR DIV', PHP_EOL;
if ($cellContent > '') if ($cellContent > '')
$cellContent .= ' '; $cellContent .= ' ';
$this->_processDomElement($child,$sheet,$row,$column,$cellContent); $this->_processDomElement($child,$sheet,$row,$column,$cellContent);
if ($cellContent > '') if ($cellContent > '')
$cellContent .= ' '; $cellContent .= ' ';
// echo 'END OF STYLING, SPAN OR DIV<br />'; //echo 'END OF STYLING, SPAN OR DIV', PHP_EOL;
break; break;
case 'hr' : case 'hr' :
$this->_flushCell($sheet,$column,$row,$cellContent); $this->_flushCell($sheet,$column,$row,$cellContent);
@ -286,14 +288,14 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
$this->_flushCell($sheet,$column,$row,$cellContent); $this->_flushCell($sheet,$column,$row,$cellContent);
++$row; ++$row;
} }
// echo 'HARD LINE BREAK: ' , '<br />'; //echo 'HARD LINE BREAK: ' , PHP_EOL;
break; break;
case 'a' : case 'a' :
// echo 'START OF HYPERLINK: ' , '<br />'; //echo 'START OF HYPERLINK: ' , PHP_EOL;
foreach($attributeArray as $attributeName => $attributeValue) { foreach($attributeArray as $attributeName => $attributeValue) {
switch($attributeName) { switch($attributeName) {
case 'href': case 'href':
// echo 'Link to ' , $attributeValue , '<br />'; //echo 'Link to ' , $attributeValue , PHP_EOL;
$sheet->getCell($column.$row)->getHyperlink()->setUrl($attributeValue); $sheet->getCell($column.$row)->getHyperlink()->setUrl($attributeValue);
if (isset($this->_formats[$child->nodeName])) { if (isset($this->_formats[$child->nodeName])) {
$sheet->getStyle($column.$row)->applyFromArray($this->_formats[$child->nodeName]); $sheet->getStyle($column.$row)->applyFromArray($this->_formats[$child->nodeName]);
@ -303,7 +305,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
} }
$cellContent .= ' '; $cellContent .= ' ';
$this->_processDomElement($child,$sheet,$row,$column,$cellContent); $this->_processDomElement($child,$sheet,$row,$column,$cellContent);
// echo 'END OF HYPERLINK:' , '<br />'; //echo 'END OF HYPERLINK:' , PHP_EOL;
break; break;
case 'h1' : case 'h1' :
case 'h2' : case 'h2' :
@ -317,17 +319,17 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
if ($this->_tableLevel > 0) { if ($this->_tableLevel > 0) {
// If we're inside a table, replace with a \n // If we're inside a table, replace with a \n
$cellContent .= "\n"; $cellContent .= "\n";
// echo 'LIST ENTRY: ' , '<br />'; //echo 'LIST ENTRY: ' , PHP_EOL;
$this->_processDomElement($child,$sheet,$row,$column,$cellContent); $this->_processDomElement($child,$sheet,$row,$column,$cellContent);
// echo 'END OF LIST ENTRY:' , '<br />'; //echo 'END OF LIST ENTRY:' , PHP_EOL;
} else { } else {
if ($cellContent > '') { if ($cellContent > '') {
$this->_flushCell($sheet,$column,$row,$cellContent); $this->_flushCell($sheet,$column,$row,$cellContent);
$row += 2; $row += 2;
} }
// echo 'START OF PARAGRAPH: ' , '<br />'; //echo 'START OF PARAGRAPH: ' , PHP_EOL;
$this->_processDomElement($child,$sheet,$row,$column,$cellContent); $this->_processDomElement($child,$sheet,$row,$column,$cellContent);
// echo 'END OF PARAGRAPH:' , '<br />'; //echo 'END OF PARAGRAPH:' , PHP_EOL;
$this->_flushCell($sheet,$column,$row,$cellContent); $this->_flushCell($sheet,$column,$row,$cellContent);
if (isset($this->_formats[$child->nodeName])) { if (isset($this->_formats[$child->nodeName])) {
@ -342,17 +344,17 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
if ($this->_tableLevel > 0) { if ($this->_tableLevel > 0) {
// If we're inside a table, replace with a \n // If we're inside a table, replace with a \n
$cellContent .= "\n"; $cellContent .= "\n";
// echo 'LIST ENTRY: ' , '<br />'; //echo 'LIST ENTRY: ' , PHP_EOL;
$this->_processDomElement($child,$sheet,$row,$column,$cellContent); $this->_processDomElement($child,$sheet,$row,$column,$cellContent);
// echo 'END OF LIST ENTRY:' , '<br />'; //echo 'END OF LIST ENTRY:' , PHP_EOL;
} else { } else {
if ($cellContent > '') { if ($cellContent > '') {
$this->_flushCell($sheet,$column,$row,$cellContent); $this->_flushCell($sheet,$column,$row,$cellContent);
} }
++$row; ++$row;
// echo 'LIST ENTRY: ' , '<br />'; //echo 'LIST ENTRY: ' , PHP_EOL;
$this->_processDomElement($child,$sheet,$row,$column,$cellContent); $this->_processDomElement($child,$sheet,$row,$column,$cellContent);
// echo 'END OF LIST ENTRY:' , '<br />'; //echo 'END OF LIST ENTRY:' , PHP_EOL;
$this->_flushCell($sheet,$column,$row,$cellContent); $this->_flushCell($sheet,$column,$row,$cellContent);
$column = 'A'; $column = 'A';
} }
@ -360,11 +362,11 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
case 'table' : case 'table' :
$this->_flushCell($sheet,$column,$row,$cellContent); $this->_flushCell($sheet,$column,$row,$cellContent);
$column = $this->_setTableStartColumn($column); $column = $this->_setTableStartColumn($column);
// echo 'START OF TABLE LEVEL ' , $this->_tableLevel , '<br />'; //echo 'START OF TABLE LEVEL ' , $this->_tableLevel , PHP_EOL;
if ($this->_tableLevel > 1) if ($this->_tableLevel > 1)
--$row; --$row;
$this->_processDomElement($child,$sheet,$row,$column,$cellContent); $this->_processDomElement($child,$sheet,$row,$column,$cellContent);
// echo 'END OF TABLE LEVEL ' , $this->_tableLevel , '<br />'; //echo 'END OF TABLE LEVEL ' , $this->_tableLevel , PHP_EOL;
$column = $this->_releaseTableStartColumn(); $column = $this->_releaseTableStartColumn();
if ($this->_tableLevel > 1) { if ($this->_tableLevel > 1) {
++$column; ++$column;
@ -380,17 +382,25 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
++$row; ++$row;
$column = $this->_getTableStartColumn(); $column = $this->_getTableStartColumn();
$cellContent = ''; $cellContent = '';
// echo 'START OF TABLE ' , $this->_tableLevel , ' ROW<br />'; //echo 'START OF TABLE ' , $this->_tableLevel , ' ROW', PHP_EOL;
$this->_processDomElement($child,$sheet,$row,$column,$cellContent); $this->_processDomElement($child,$sheet,$row,$column,$cellContent);
// echo 'END OF TABLE ' , $this->_tableLevel , ' ROW<br />'; //echo 'END OF TABLE ' , $this->_tableLevel , ' ROW', PHP_EOL;
break; break;
case 'th' : case 'th' :
case 'td' : case 'td' :
// echo 'START OF TABLE ' , $this->_tableLevel , ' CELL<br />'; //echo 'START OF TABLE ' , $this->_tableLevel , ' CELL', PHP_EOL;
$this->_columnAdd = (isset($attributeArray['colspan'])) ? $attributeArray['colspan'] : 1;
//echo 'COLUMN SPAN IS ',$this->_columnAdd,PHP_EOL;
$this->_processDomElement($child,$sheet,$row,$column,$cellContent); $this->_processDomElement($child,$sheet,$row,$column,$cellContent);
// echo 'END OF TABLE ' , $this->_tableLevel , ' CELL<br />'; //echo 'END OF TABLE ' , $this->_tableLevel , ' CELL', PHP_EOL;
$this->_flushCell($sheet,$column,$row,$cellContent); $this->_flushCell($sheet,$column,$row,$cellContent);
++$column; $startColumn = $column;
$endColumn = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($column) + $this->_columnAdd - 2);
$column = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($column) + $this->_columnAdd - 1);
if ($this->_columnAdd > 1) {
//echo 'Merge Cells ',$startColumn.$row.':'.$endColumn.$row,PHP_EOL;
$sheet->mergeCells($startColumn.$row.':'.$endColumn.$row);
}
break; break;
case 'body' : case 'body' :
$row = 1; $row = 1;

View File

@ -0,0 +1,58 @@
<table>
<tr>
<td rowspan="2">Day</td>
<td colspan="2">Type 1</td>
<td colspan="2">Type 2</td>
<td colspan="2">Type 3</td>
<td>Total Conversions</td>
</tr>
<tr>
<td>Week 1 2013</td>
<td>Week 1 2012</td>
<td>Week 1 2013</td>
<td>Week 1 2012</td>
<td>Week 1 2013</td>
<td>Week 1 2012</td>
<td>(%age)</td>
</tr>
<tr>
<td>Sunday</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
<td>110</td>
</tr>
<tr>
<td>Monday</td>
<td>20</td>
<td>21</td>
<td>22</td>
<td>23</td>
<td>24</td>
<td>25</td>
<td>120</td>
</tr>
<tr>
<td>Tuesday</td>
<td>30</td>
<td>31</td>
<td>32</td>
<td>33</td>
<td>34</td>
<td>35</td>
<td>130</td>
</tr>
<tr>
<td><b>Total</b></td>
<td>30</td>
<td>31</td>
<td>32</td>
<td>33</td>
<td>34</td>
<td>35</td>
<td>130</td>
</tr>
</table>

View File

@ -0,0 +1,61 @@
<?php
/**
* PHPExcel
*
* Copyright (C) 2006 - 2012 PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPExcel
* @package PHPExcel
* @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE##
*/
/** Error reporting */
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';
echo date('H:i:s') , " Load from HTML file" , PHP_EOL;
$callStartTime = microtime(true);
$objReader = PHPExcel_IOFactory::createReader('HTML');
$objPHPExcel = $objReader->load("HTMLReaderTest.html");
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo 'Call time to read Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , PHP_EOL;
// Echo memory usage
echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , PHP_EOL;
echo date('H:i:s') , " Write to Excel5 format" , PHP_EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', __FILE__) , PHP_EOL;
// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , PHP_EOL;
// Echo done
echo date('H:i:s') , " Done writing file" , PHP_EOL;