From 8bcfc3bf379eeadd28dbcdbbce3662c3527c1806 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Wed, 16 Jan 2013 22:11:26 +0000 Subject: [PATCH] Added support for colspan attribute --- Classes/PHPExcel/Reader/HTML.php | 58 +++++++++++++++++------------- Examples/HTMLReaderTest.html | 58 ++++++++++++++++++++++++++++++ Examples/HTMLReaderTest.php | 61 ++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 24 deletions(-) create mode 100644 Examples/HTMLReaderTest.html create mode 100644 Examples/HTMLReaderTest.php diff --git a/Classes/PHPExcel/Reader/HTML.php b/Classes/PHPExcel/Reader/HTML.php index 4e8b8e9..26351b7 100644 --- a/Classes/PHPExcel/Reader/HTML.php +++ b/Classes/PHPExcel/Reader/HTML.php @@ -201,7 +201,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_ // Simple String content if (trim($cellContent) > '') { // Only actually write it if there's content in the string -// echo 'FLUSH CELL: ' , $column , $row , ' => ' , $cellContent , '
'; +//echo 'FLUSH CELL: ' , $column , $row , ' => ' , $cellContent , PHP_EOL; // Write to worksheet to be done here... // ... we return the cell so we can mess about with styles more easily $cell = $sheet->setCellValue($column.$row,$cellContent,true); @@ -215,6 +215,8 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_ $cellContent = (string) ''; } + private $_columnAdd = 1; + private function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent){ foreach($element->childNodes as $child){ if ($child instanceof DOMText) { @@ -227,11 +229,11 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_ // TODO } } elseif($child instanceof DOMElement) { -// echo 'DOM ELEMENT: ' , strtoupper($child->nodeName) , '
'; +//echo '**DOM ELEMENT:** ' , strtoupper($child->nodeName) , PHP_EOL; $attributeArray = array(); foreach($child->attributes as $attribute) { -// echo 'ATTRIBUTE: ' , $attribute->name , ' => ' , $attribute->value , '
'; +//echo '**ATTRIBUTE:** ' , $attribute->name , ' => ' , $attribute->value , PHP_EOL; $attributeArray[$attribute->name] = $attribute->value; } @@ -259,13 +261,13 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_ case 'em' : case 'strong': case 'b' : -// echo 'STYLING, SPAN OR DIV
'; +//echo 'STYLING, SPAN OR DIV', PHP_EOL; if ($cellContent > '') $cellContent .= ' '; $this->_processDomElement($child,$sheet,$row,$column,$cellContent); if ($cellContent > '') $cellContent .= ' '; -// echo 'END OF STYLING, SPAN OR DIV
'; +//echo 'END OF STYLING, SPAN OR DIV', PHP_EOL; break; case 'hr' : $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); ++$row; } -// echo 'HARD LINE BREAK: ' , '
'; +//echo 'HARD LINE BREAK: ' , PHP_EOL; break; case 'a' : -// echo 'START OF HYPERLINK: ' , '
'; +//echo 'START OF HYPERLINK: ' , PHP_EOL; foreach($attributeArray as $attributeName => $attributeValue) { switch($attributeName) { case 'href': -// echo 'Link to ' , $attributeValue , '
'; +//echo 'Link to ' , $attributeValue , PHP_EOL; $sheet->getCell($column.$row)->getHyperlink()->setUrl($attributeValue); if (isset($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 .= ' '; $this->_processDomElement($child,$sheet,$row,$column,$cellContent); -// echo 'END OF HYPERLINK:' , '
'; +//echo 'END OF HYPERLINK:' , PHP_EOL; break; case 'h1' : case 'h2' : @@ -317,17 +319,17 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_ if ($this->_tableLevel > 0) { // If we're inside a table, replace with a \n $cellContent .= "\n"; -// echo 'LIST ENTRY: ' , '
'; +//echo 'LIST ENTRY: ' , PHP_EOL; $this->_processDomElement($child,$sheet,$row,$column,$cellContent); -// echo 'END OF LIST ENTRY:' , '
'; +//echo 'END OF LIST ENTRY:' , PHP_EOL; } else { if ($cellContent > '') { $this->_flushCell($sheet,$column,$row,$cellContent); $row += 2; } -// echo 'START OF PARAGRAPH: ' , '
'; +//echo 'START OF PARAGRAPH: ' , PHP_EOL; $this->_processDomElement($child,$sheet,$row,$column,$cellContent); -// echo 'END OF PARAGRAPH:' , '
'; +//echo 'END OF PARAGRAPH:' , PHP_EOL; $this->_flushCell($sheet,$column,$row,$cellContent); 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 we're inside a table, replace with a \n $cellContent .= "\n"; -// echo 'LIST ENTRY: ' , '
'; +//echo 'LIST ENTRY: ' , PHP_EOL; $this->_processDomElement($child,$sheet,$row,$column,$cellContent); -// echo 'END OF LIST ENTRY:' , '
'; +//echo 'END OF LIST ENTRY:' , PHP_EOL; } else { if ($cellContent > '') { $this->_flushCell($sheet,$column,$row,$cellContent); } ++$row; -// echo 'LIST ENTRY: ' , '
'; +//echo 'LIST ENTRY: ' , PHP_EOL; $this->_processDomElement($child,$sheet,$row,$column,$cellContent); -// echo 'END OF LIST ENTRY:' , '
'; +//echo 'END OF LIST ENTRY:' , PHP_EOL; $this->_flushCell($sheet,$column,$row,$cellContent); $column = 'A'; } @@ -360,11 +362,11 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_ case 'table' : $this->_flushCell($sheet,$column,$row,$cellContent); $column = $this->_setTableStartColumn($column); -// echo 'START OF TABLE LEVEL ' , $this->_tableLevel , '
'; +//echo 'START OF TABLE LEVEL ' , $this->_tableLevel , PHP_EOL; if ($this->_tableLevel > 1) --$row; $this->_processDomElement($child,$sheet,$row,$column,$cellContent); -// echo 'END OF TABLE LEVEL ' , $this->_tableLevel , '
'; +//echo 'END OF TABLE LEVEL ' , $this->_tableLevel , PHP_EOL; $column = $this->_releaseTableStartColumn(); if ($this->_tableLevel > 1) { ++$column; @@ -380,17 +382,25 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_ ++$row; $column = $this->_getTableStartColumn(); $cellContent = ''; -// echo 'START OF TABLE ' , $this->_tableLevel , ' ROW
'; +//echo 'START OF TABLE ' , $this->_tableLevel , ' ROW', PHP_EOL; $this->_processDomElement($child,$sheet,$row,$column,$cellContent); -// echo 'END OF TABLE ' , $this->_tableLevel , ' ROW
'; +//echo 'END OF TABLE ' , $this->_tableLevel , ' ROW', PHP_EOL; break; case 'th' : case 'td' : -// echo 'START OF TABLE ' , $this->_tableLevel , ' CELL
'; +//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); -// echo 'END OF TABLE ' , $this->_tableLevel , ' CELL
'; +//echo 'END OF TABLE ' , $this->_tableLevel , ' CELL', PHP_EOL; $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; case 'body' : $row = 1; diff --git a/Examples/HTMLReaderTest.html b/Examples/HTMLReaderTest.html new file mode 100644 index 0000000..1c711ce --- /dev/null +++ b/Examples/HTMLReaderTest.html @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DayType 1Type 2Type 3Total Conversions
Week 1 2013Week 1 2012Week 1 2013Week 1 2012Week 1 2013Week 1 2012(%age)
Sunday101112131415110
Monday202122232425120
Tuesday303132333435130
Total303132333435130
diff --git a/Examples/HTMLReaderTest.php b/Examples/HTMLReaderTest.php new file mode 100644 index 0000000..428c592 --- /dev/null +++ b/Examples/HTMLReaderTest.php @@ -0,0 +1,61 @@ +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;