mirror of
https://github.com/retailcrm/PHPExcel.git
synced 2024-11-25 14:56:04 +03:00
Feature: (cifren/MBaker) Work Item GH-205 - Handling merge cells in HTML Reader
This commit is contained in:
parent
b2e82a0e11
commit
d7ea3e2ab0
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
@ -24,8 +25,6 @@
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/** PHPExcel root directory */
|
||||
if (!defined('PHPEXCEL_ROOT')) {
|
||||
/**
|
||||
@ -44,26 +43,28 @@ if (!defined('PHPEXCEL_ROOT')) {
|
||||
*/
|
||||
class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
|
||||
{
|
||||
|
||||
/**
|
||||
* Input encoding
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_inputEncoding = 'ANSI';
|
||||
protected $_inputEncoding = 'ANSI';
|
||||
|
||||
/**
|
||||
* Sheet index to read
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $_sheetIndex = 0;
|
||||
protected $_sheetIndex = 0;
|
||||
|
||||
/**
|
||||
* Formats
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_formats = array( 'h1' => array( 'font' => array( 'bold' => true,
|
||||
protected $_formats = array(
|
||||
'h1' => array('font' => array('bold' => true,
|
||||
'size' => 24,
|
||||
),
|
||||
), // Bold, 24pt
|
||||
@ -93,18 +94,20 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
),
|
||||
), // Blue underlined
|
||||
'hr' => array('borders' => array('bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN,
|
||||
'color' => array( PHPExcel_Style_Color::COLOR_BLACK,
|
||||
'color' => array(\PHPExcel_Style_Color::COLOR_BLACK,
|
||||
),
|
||||
),
|
||||
),
|
||||
), // Bottom border
|
||||
);
|
||||
|
||||
protected $rowspan = array();
|
||||
|
||||
/**
|
||||
* Create a new PHPExcel_Reader_HTML
|
||||
*/
|
||||
public function __construct() {
|
||||
public function __construct()
|
||||
{
|
||||
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
|
||||
}
|
||||
|
||||
@ -149,6 +152,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
public function setInputEncoding($pValue = 'ANSI')
|
||||
{
|
||||
$this->_inputEncoding = $pValue;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -163,12 +167,12 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
}
|
||||
|
||||
// Data Array used for testing only, should write to PHPExcel object on completion of tests
|
||||
private $_dataArray = array();
|
||||
protected $_dataArray = array();
|
||||
protected $_tableLevel = 0;
|
||||
protected $_nestedColumn = array('A');
|
||||
|
||||
private $_tableLevel = 0;
|
||||
private $_nestedColumn = array('A');
|
||||
|
||||
private function _setTableStartColumn($column) {
|
||||
protected function _setTableStartColumn($column)
|
||||
{
|
||||
if ($this->_tableLevel == 0)
|
||||
$column = 'A';
|
||||
++$this->_tableLevel;
|
||||
@ -177,16 +181,20 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
return $this->_nestedColumn[$this->_tableLevel];
|
||||
}
|
||||
|
||||
private function _getTableStartColumn() {
|
||||
protected function _getTableStartColumn()
|
||||
{
|
||||
return $this->_nestedColumn[$this->_tableLevel];
|
||||
}
|
||||
|
||||
private function _releaseTableStartColumn() {
|
||||
protected function _releaseTableStartColumn()
|
||||
{
|
||||
--$this->_tableLevel;
|
||||
|
||||
return array_pop($this->_nestedColumn);
|
||||
}
|
||||
|
||||
private function _flushCell($sheet,$column,$row,&$cellContent) {
|
||||
protected function _flushCell($sheet, $column, $row, &$cellContent)
|
||||
{
|
||||
if (is_string($cellContent)) {
|
||||
// Simple String content
|
||||
if (trim($cellContent) > '') {
|
||||
@ -194,7 +202,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
// echo 'FLUSH CELL: ' , $column , $row , ' => ' , $cellContent , '<br />';
|
||||
// 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);
|
||||
$sheet->setCellValue($column . $row, $cellContent, true);
|
||||
$this->_dataArray[$row][$column] = $cellContent;
|
||||
}
|
||||
} else {
|
||||
@ -205,7 +213,8 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
$cellContent = (string) '';
|
||||
}
|
||||
|
||||
private function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent){
|
||||
protected function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent, $format = null)
|
||||
{
|
||||
foreach ($element->childNodes as $child) {
|
||||
if ($child instanceof DOMText) {
|
||||
$domText = preg_replace('/\s+/', ' ', trim($child->nodeValue));
|
||||
@ -313,7 +322,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
} else {
|
||||
if ($cellContent > '') {
|
||||
$this->_flushCell($sheet, $column, $row, $cellContent);
|
||||
$row += 2;
|
||||
$row++;
|
||||
}
|
||||
// echo 'START OF PARAGRAPH: ' , '<br />';
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
@ -324,7 +333,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
$sheet->getStyle($column . $row)->applyFromArray($this->_formats[$child->nodeName]);
|
||||
}
|
||||
|
||||
$row += 2;
|
||||
$row++;
|
||||
$column = 'A';
|
||||
}
|
||||
break;
|
||||
@ -367,11 +376,11 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
break;
|
||||
case 'tr' :
|
||||
++$row;
|
||||
$column = $this->_getTableStartColumn();
|
||||
$cellContent = '';
|
||||
// echo 'START OF TABLE ' , $this->_tableLevel , ' ROW<br />';
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
++$row;
|
||||
// echo 'END OF TABLE ' , $this->_tableLevel , ' ROW<br />';
|
||||
break;
|
||||
case 'th' :
|
||||
@ -379,7 +388,46 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
// echo 'START OF TABLE ' , $this->_tableLevel , ' CELL<br />';
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
// echo 'END OF TABLE ' , $this->_tableLevel , ' CELL<br />';
|
||||
|
||||
while (isset($this->rowspan[$column . $row])) {
|
||||
++$column;
|
||||
}
|
||||
|
||||
$this->_flushCell($sheet, $column, $row, $cellContent);
|
||||
|
||||
if (isset($attributeArray['style']) && !empty($attributeArray['style'])) {
|
||||
$styleAry = $this->getPhpExcelStyleArray($attributeArray['style']);
|
||||
|
||||
if (!empty($styleAry)) {
|
||||
$sheet->getStyle($column . $row)->applyFromArray($styleAry);
|
||||
}
|
||||
}
|
||||
|
||||
//create merging rowspan
|
||||
if (isset($attributeArray['rowspan']) && isset($attributeArray['colspan'])) {
|
||||
$columnTo = $column;
|
||||
for ($i = 0; $i < $attributeArray['colspan'] - 1; $i++) {
|
||||
++$columnTo;
|
||||
}
|
||||
$range = $column . $row . ':' . $columnTo . ($row + $attributeArray['rowspan'] - 1);
|
||||
foreach (\PHPExcel_Cell::extractAllCellReferencesInRange($range) as $value) {
|
||||
$this->rowspan[$value] = true;
|
||||
}
|
||||
$sheet->mergeCells($range);
|
||||
} elseif (isset($attributeArray['rowspan'])) {
|
||||
$range = $column . $row . ':' . $column . ($row + $attributeArray['rowspan'] - 1);
|
||||
foreach (\PHPExcel_Cell::extractAllCellReferencesInRange($range) as $value) {
|
||||
$this->rowspan[$value] = true;
|
||||
}
|
||||
$sheet->mergeCells($range);
|
||||
} elseif (isset($attributeArray['colspan'])) {
|
||||
$columnTo = $column;
|
||||
for ($i = 0; $i < $attributeArray['colspan'] - 1; $i++) {
|
||||
++$columnTo;
|
||||
}
|
||||
$sheet->mergeCells($column . $row . ':' . $columnTo . $row);
|
||||
$column = $columnTo;
|
||||
}
|
||||
++$column;
|
||||
break;
|
||||
case 'body' :
|
||||
@ -422,21 +470,16 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
$objPHPExcel->setActiveSheetIndex($this->_sheetIndex);
|
||||
|
||||
// Create a new DOM object
|
||||
$dom = new DOMDocument;
|
||||
$dom = new domDocument;
|
||||
// Reload the HTML file into the DOM object
|
||||
if ((version_compare(PHP_VERSION, '5.4.0') >= 0) && defined(LIBXML_DTDLOAD)) {
|
||||
$loaded = $dom->loadHTMLFile($pFilename, PHPExcel_Settings::getLibXmlLoaderOptions());
|
||||
} else {
|
||||
$loaded = $dom->loadHTMLFile($pFilename);
|
||||
}
|
||||
if ($loaded === FALSE) {
|
||||
throw new PHPExcel_Reader_Exception('Failed to load '. $pFilename. ' as a DOM Document');
|
||||
throw new PHPExcel_Reader_Exception('Failed to load ', $pFilename, ' as a DOM Document');
|
||||
}
|
||||
|
||||
// Discard white space
|
||||
$dom->preserveWhiteSpace = false;
|
||||
|
||||
|
||||
$row = 0;
|
||||
$column = 'A';
|
||||
$content = '';
|
||||
@ -451,7 +494,8 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSheetIndex() {
|
||||
public function getSheetIndex()
|
||||
{
|
||||
return $this->_sheetIndex;
|
||||
}
|
||||
|
||||
@ -461,9 +505,12 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
|
||||
* @param int $pValue Sheet index
|
||||
* @return PHPExcel_Reader_HTML
|
||||
*/
|
||||
public function setSheetIndex($pValue = 0) {
|
||||
public function setSheetIndex($pValue = 0)
|
||||
{
|
||||
$this->_sheetIndex = $pValue;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -49,6 +49,7 @@ Planned for v1.8.1
|
||||
- Feature: (CQD) Work Item GH-389 - Additional Mac CJK codepage definitions
|
||||
- Feature: (bolovincev) Work Item GH-269 - Update Worksheet.php getStyleByColumnAndRow() to allow a range of cells rather than just a single cell
|
||||
- Feature: (MBaker) - New methods added for testing cell status within merge groups
|
||||
- Feature: (cifren/MBaker) Work Item GH-205 - Handling merge cells in HTML Reader
|
||||
|
||||
|
||||
2014-03-02 (v1.8.0):
|
||||
|
Loading…
Reference in New Issue
Block a user