Dateien nach "includes/PHPExcel/Classes/PHPExcel/Reader" hochladen
This commit is contained in:
parent
bcb1d85d86
commit
99b34ce5be
|
@ -0,0 +1,255 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_Abstract
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
|
||||
{
|
||||
/**
|
||||
* Read data only?
|
||||
* Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
|
||||
* or whether it should read both data and formatting
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_readDataOnly = FALSE;
|
||||
|
||||
/**
|
||||
* Read charts that are defined in the workbook?
|
||||
* Identifies whether the Reader should read the definitions for any charts that exist in the workbook;
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $_includeCharts = FALSE;
|
||||
|
||||
/**
|
||||
* Restrict which sheets should be loaded?
|
||||
* This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
|
||||
*
|
||||
* @var array of string
|
||||
*/
|
||||
protected $_loadSheetsOnly = NULL;
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_IReadFilter instance
|
||||
*
|
||||
* @var PHPExcel_Reader_IReadFilter
|
||||
*/
|
||||
protected $_readFilter = NULL;
|
||||
|
||||
protected $_fileHandle = NULL;
|
||||
|
||||
|
||||
/**
|
||||
* Read data only?
|
||||
* If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
|
||||
* If false (the default) it will read data and formatting.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getReadDataOnly() {
|
||||
return $this->_readDataOnly;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set read data only
|
||||
* Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
|
||||
* Set to false (the default) to advise the Reader to read both data and formatting for cells.
|
||||
*
|
||||
* @param boolean $pValue
|
||||
*
|
||||
* @return PHPExcel_Reader_IReader
|
||||
*/
|
||||
public function setReadDataOnly($pValue = FALSE) {
|
||||
$this->_readDataOnly = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read charts in workbook?
|
||||
* If this is true, then the Reader will include any charts that exist in the workbook.
|
||||
* Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
|
||||
* If false (the default) it will ignore any charts defined in the workbook file.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getIncludeCharts() {
|
||||
return $this->_includeCharts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set read charts in workbook
|
||||
* Set to true, to advise the Reader to include any charts that exist in the workbook.
|
||||
* Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
|
||||
* Set to false (the default) to discard charts.
|
||||
*
|
||||
* @param boolean $pValue
|
||||
*
|
||||
* @return PHPExcel_Reader_IReader
|
||||
*/
|
||||
public function setIncludeCharts($pValue = FALSE) {
|
||||
$this->_includeCharts = (boolean) $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get which sheets to load
|
||||
* Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
|
||||
* indicating that all worksheets in the workbook should be loaded.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLoadSheetsOnly()
|
||||
{
|
||||
return $this->_loadSheetsOnly;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set which sheets to load
|
||||
*
|
||||
* @param mixed $value
|
||||
* This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
|
||||
* If NULL, then it tells the Reader to read all worksheets in the workbook
|
||||
*
|
||||
* @return PHPExcel_Reader_IReader
|
||||
*/
|
||||
public function setLoadSheetsOnly($value = NULL)
|
||||
{
|
||||
if ($value === NULL)
|
||||
return $this->setLoadAllSheets();
|
||||
|
||||
$this->_loadSheetsOnly = is_array($value) ?
|
||||
$value : array($value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all sheets to load
|
||||
* Tells the Reader to load all worksheets from the workbook.
|
||||
*
|
||||
* @return PHPExcel_Reader_IReader
|
||||
*/
|
||||
public function setLoadAllSheets()
|
||||
{
|
||||
$this->_loadSheetsOnly = NULL;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read filter
|
||||
*
|
||||
* @return PHPExcel_Reader_IReadFilter
|
||||
*/
|
||||
public function getReadFilter() {
|
||||
return $this->_readFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set read filter
|
||||
*
|
||||
* @param PHPExcel_Reader_IReadFilter $pValue
|
||||
* @return PHPExcel_Reader_IReader
|
||||
*/
|
||||
public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
|
||||
$this->_readFilter = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open file for reading
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
* @return resource
|
||||
*/
|
||||
protected function _openFile($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename) || !is_readable($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
// Open file
|
||||
$this->_fileHandle = fopen($pFilename, 'r');
|
||||
if ($this->_fileHandle === FALSE) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open file " . $pFilename . " for reading.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the current PHPExcel_Reader_IReader read the file?
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return boolean
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function canRead($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
try {
|
||||
$this->_openFile($pFilename);
|
||||
} catch (Exception $e) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$readable = $this->_isValidFormat();
|
||||
fclose ($this->_fileHandle);
|
||||
return $readable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
|
||||
*
|
||||
* @param string $xml
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function securityScan($xml)
|
||||
{
|
||||
$pattern = '/\\0?' . implode('\\0?', str_split('<!DOCTYPE')) . '\\0?/';
|
||||
if (preg_match($pattern, $xml)) {
|
||||
throw new PHPExcel_Reader_Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
|
||||
}
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
|
||||
*
|
||||
* @param string $filestream
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function securityScanFile($filestream)
|
||||
{
|
||||
return $this->securityScan(file_get_contents($filestream));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,387 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/** PHPExcel root directory */
|
||||
if (!defined('PHPEXCEL_ROOT')) {
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
|
||||
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_CSV
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
|
||||
{
|
||||
/**
|
||||
* Input encoding
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $_inputEncoding = 'UTF-8';
|
||||
|
||||
/**
|
||||
* Delimiter
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $_delimiter = ',';
|
||||
|
||||
/**
|
||||
* Enclosure
|
||||
*
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
private $_enclosure = '"';
|
||||
|
||||
/**
|
||||
* Sheet index to read
|
||||
*
|
||||
* @access private
|
||||
* @var int
|
||||
*/
|
||||
private $_sheetIndex = 0;
|
||||
|
||||
/**
|
||||
* Load rows contiguously
|
||||
*
|
||||
* @access private
|
||||
* @var int
|
||||
*/
|
||||
private $_contiguous = false;
|
||||
|
||||
/**
|
||||
* Row counter for loading rows contiguously
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $_contiguousRow = -1;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new PHPExcel_Reader_CSV
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the current file is a CSV file
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _isValidFormat()
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set input encoding
|
||||
*
|
||||
* @param string $pValue Input encoding
|
||||
*/
|
||||
public function setInputEncoding($pValue = 'UTF-8')
|
||||
{
|
||||
$this->_inputEncoding = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get input encoding
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getInputEncoding()
|
||||
{
|
||||
return $this->_inputEncoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move filepointer past any BOM marker
|
||||
*
|
||||
*/
|
||||
protected function _skipBOM()
|
||||
{
|
||||
rewind($this->_fileHandle);
|
||||
|
||||
switch ($this->_inputEncoding) {
|
||||
case 'UTF-8':
|
||||
fgets($this->_fileHandle, 4) == "\xEF\xBB\xBF" ?
|
||||
fseek($this->_fileHandle, 3) : fseek($this->_fileHandle, 0);
|
||||
break;
|
||||
case 'UTF-16LE':
|
||||
fgets($this->_fileHandle, 3) == "\xFF\xFE" ?
|
||||
fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
|
||||
break;
|
||||
case 'UTF-16BE':
|
||||
fgets($this->_fileHandle, 3) == "\xFE\xFF" ?
|
||||
fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
|
||||
break;
|
||||
case 'UTF-32LE':
|
||||
fgets($this->_fileHandle, 5) == "\xFF\xFE\x00\x00" ?
|
||||
fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
|
||||
break;
|
||||
case 'UTF-32BE':
|
||||
fgets($this->_fileHandle, 5) == "\x00\x00\xFE\xFF" ?
|
||||
fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function listWorksheetInfo($pFilename)
|
||||
{
|
||||
// Open file
|
||||
$this->_openFile($pFilename);
|
||||
if (!$this->_isValidFormat()) {
|
||||
fclose ($this->_fileHandle);
|
||||
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
|
||||
}
|
||||
$fileHandle = $this->_fileHandle;
|
||||
|
||||
// Skip BOM, if any
|
||||
$this->_skipBOM();
|
||||
|
||||
$escapeEnclosures = array( "\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure );
|
||||
|
||||
$worksheetInfo = array();
|
||||
$worksheetInfo[0]['worksheetName'] = 'Worksheet';
|
||||
$worksheetInfo[0]['lastColumnLetter'] = 'A';
|
||||
$worksheetInfo[0]['lastColumnIndex'] = 0;
|
||||
$worksheetInfo[0]['totalRows'] = 0;
|
||||
$worksheetInfo[0]['totalColumns'] = 0;
|
||||
|
||||
// Loop through each line of the file in turn
|
||||
while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
|
||||
$worksheetInfo[0]['totalRows']++;
|
||||
$worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
|
||||
}
|
||||
|
||||
$worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
|
||||
$worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
|
||||
|
||||
// Close file
|
||||
fclose($fileHandle);
|
||||
|
||||
return $worksheetInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function load($pFilename)
|
||||
{
|
||||
// Create new PHPExcel
|
||||
$objPHPExcel = new PHPExcel();
|
||||
|
||||
// Load into this instance
|
||||
return $this->loadIntoExisting($pFilename, $objPHPExcel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file into PHPExcel instance
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @param PHPExcel $objPHPExcel
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
|
||||
{
|
||||
$lineEnding = ini_get('auto_detect_line_endings');
|
||||
ini_set('auto_detect_line_endings', true);
|
||||
|
||||
// Open file
|
||||
$this->_openFile($pFilename);
|
||||
if (!$this->_isValidFormat()) {
|
||||
fclose ($this->_fileHandle);
|
||||
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
|
||||
}
|
||||
$fileHandle = $this->_fileHandle;
|
||||
|
||||
// Skip BOM, if any
|
||||
$this->_skipBOM();
|
||||
|
||||
// Create new PHPExcel object
|
||||
while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
|
||||
$objPHPExcel->createSheet();
|
||||
}
|
||||
$sheet = $objPHPExcel->setActiveSheetIndex($this->_sheetIndex);
|
||||
|
||||
$escapeEnclosures = array( "\\" . $this->_enclosure,
|
||||
$this->_enclosure . $this->_enclosure
|
||||
);
|
||||
|
||||
// Set our starting row based on whether we're in contiguous mode or not
|
||||
$currentRow = 1;
|
||||
if ($this->_contiguous) {
|
||||
$currentRow = ($this->_contiguousRow == -1) ? $sheet->getHighestRow(): $this->_contiguousRow;
|
||||
}
|
||||
|
||||
// Loop through each line of the file in turn
|
||||
while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
|
||||
$columnLetter = 'A';
|
||||
foreach($rowData as $rowDatum) {
|
||||
if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
|
||||
// Unescape enclosures
|
||||
$rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum);
|
||||
|
||||
// Convert encoding if necessary
|
||||
if ($this->_inputEncoding !== 'UTF-8') {
|
||||
$rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding);
|
||||
}
|
||||
|
||||
// Set cell value
|
||||
$sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum);
|
||||
}
|
||||
++$columnLetter;
|
||||
}
|
||||
++$currentRow;
|
||||
}
|
||||
|
||||
// Close file
|
||||
fclose($fileHandle);
|
||||
|
||||
if ($this->_contiguous) {
|
||||
$this->_contiguousRow = $currentRow;
|
||||
}
|
||||
|
||||
ini_set('auto_detect_line_endings', $lineEnding);
|
||||
|
||||
// Return
|
||||
return $objPHPExcel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get delimiter
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDelimiter() {
|
||||
return $this->_delimiter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set delimiter
|
||||
*
|
||||
* @param string $pValue Delimiter, defaults to ,
|
||||
* @return PHPExcel_Reader_CSV
|
||||
*/
|
||||
public function setDelimiter($pValue = ',') {
|
||||
$this->_delimiter = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get enclosure
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEnclosure() {
|
||||
return $this->_enclosure;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set enclosure
|
||||
*
|
||||
* @param string $pValue Enclosure, defaults to "
|
||||
* @return PHPExcel_Reader_CSV
|
||||
*/
|
||||
public function setEnclosure($pValue = '"') {
|
||||
if ($pValue == '') {
|
||||
$pValue = '"';
|
||||
}
|
||||
$this->_enclosure = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sheet index
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getSheetIndex() {
|
||||
return $this->_sheetIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set sheet index
|
||||
*
|
||||
* @param integer $pValue Sheet index
|
||||
* @return PHPExcel_Reader_CSV
|
||||
*/
|
||||
public function setSheetIndex($pValue = 0) {
|
||||
$this->_sheetIndex = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Contiguous
|
||||
*
|
||||
* @param boolean $contiguous
|
||||
*/
|
||||
public function setContiguous($contiguous = FALSE)
|
||||
{
|
||||
$this->_contiguous = (bool) $contiguous;
|
||||
if (!$contiguous) {
|
||||
$this->_contiguousRow = -1;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Contiguous
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getContiguous() {
|
||||
return $this->_contiguous;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/** PHPExcel root directory */
|
||||
if (!defined('PHPEXCEL_ROOT')) {
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
|
||||
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_DefaultReadFilter
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
class PHPExcel_Reader_DefaultReadFilter implements PHPExcel_Reader_IReadFilter
|
||||
{
|
||||
/**
|
||||
* Should this cell be read?
|
||||
*
|
||||
* @param $column String column index
|
||||
* @param $row Row index
|
||||
* @param $worksheetName Optional worksheet name
|
||||
* @return boolean
|
||||
*/
|
||||
public function readCell($column, $row, $worksheetName = '') {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,809 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/** PHPExcel root directory */
|
||||
if (!defined('PHPEXCEL_ROOT')) {
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
|
||||
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_Excel2003XML
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
class PHPExcel_Reader_Excel2003XML extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
|
||||
{
|
||||
/**
|
||||
* Formats
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_styles = array();
|
||||
|
||||
/**
|
||||
* Character set used in the file
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_charSet = 'UTF-8';
|
||||
|
||||
|
||||
/**
|
||||
* Create a new PHPExcel_Reader_Excel2003XML
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Can the current PHPExcel_Reader_IReader read the file?
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return boolean
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function canRead($pFilename)
|
||||
{
|
||||
|
||||
// Office xmlns:o="urn:schemas-microsoft-com:office:office"
|
||||
// Excel xmlns:x="urn:schemas-microsoft-com:office:excel"
|
||||
// XML Spreadsheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
|
||||
// Spreadsheet component xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet"
|
||||
// XML schema xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
|
||||
// XML data type xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
|
||||
// MS-persist recordset xmlns:rs="urn:schemas-microsoft-com:rowset"
|
||||
// Rowset xmlns:z="#RowsetSchema"
|
||||
//
|
||||
|
||||
$signature = array(
|
||||
'<?xml version="1.0"',
|
||||
'<?mso-application progid="Excel.Sheet"?>'
|
||||
);
|
||||
|
||||
// Open file
|
||||
$this->_openFile($pFilename);
|
||||
$fileHandle = $this->_fileHandle;
|
||||
|
||||
// Read sample data (first 2 KB will do)
|
||||
$data = fread($fileHandle, 2048);
|
||||
fclose($fileHandle);
|
||||
|
||||
$valid = true;
|
||||
foreach($signature as $match) {
|
||||
// every part of the signature must be present
|
||||
if (strpos($data, $match) === false) {
|
||||
$valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve charset encoding
|
||||
if(preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/um',$data,$matches)) {
|
||||
$this->_charSet = strtoupper($matches[1]);
|
||||
}
|
||||
// echo 'Character Set is ',$this->_charSet,'<br />';
|
||||
|
||||
return $valid;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function listWorksheetNames($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
if (!$this->canRead($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
|
||||
}
|
||||
|
||||
$worksheetNames = array();
|
||||
|
||||
$xml = simplexml_load_string($this->securityScan(file_get_contents($pFilename)), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());
|
||||
$namespaces = $xml->getNamespaces(true);
|
||||
|
||||
$xml_ss = $xml->children($namespaces['ss']);
|
||||
foreach($xml_ss->Worksheet as $worksheet) {
|
||||
$worksheet_ss = $worksheet->attributes($namespaces['ss']);
|
||||
$worksheetNames[] = self::_convertStringEncoding((string) $worksheet_ss['Name'],$this->_charSet);
|
||||
}
|
||||
|
||||
return $worksheetNames;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function listWorksheetInfo($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
$worksheetInfo = array();
|
||||
|
||||
$xml = simplexml_load_string($this->securityScan(file_get_contents($pFilename)), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());
|
||||
$namespaces = $xml->getNamespaces(true);
|
||||
|
||||
$worksheetID = 1;
|
||||
$xml_ss = $xml->children($namespaces['ss']);
|
||||
foreach($xml_ss->Worksheet as $worksheet) {
|
||||
$worksheet_ss = $worksheet->attributes($namespaces['ss']);
|
||||
|
||||
$tmpInfo = array();
|
||||
$tmpInfo['worksheetName'] = '';
|
||||
$tmpInfo['lastColumnLetter'] = 'A';
|
||||
$tmpInfo['lastColumnIndex'] = 0;
|
||||
$tmpInfo['totalRows'] = 0;
|
||||
$tmpInfo['totalColumns'] = 0;
|
||||
|
||||
if (isset($worksheet_ss['Name'])) {
|
||||
$tmpInfo['worksheetName'] = (string) $worksheet_ss['Name'];
|
||||
} else {
|
||||
$tmpInfo['worksheetName'] = "Worksheet_{$worksheetID}";
|
||||
}
|
||||
|
||||
if (isset($worksheet->Table->Row)) {
|
||||
$rowIndex = 0;
|
||||
|
||||
foreach($worksheet->Table->Row as $rowData) {
|
||||
$columnIndex = 0;
|
||||
$rowHasData = false;
|
||||
|
||||
foreach($rowData->Cell as $cell) {
|
||||
if (isset($cell->Data)) {
|
||||
$tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
|
||||
$rowHasData = true;
|
||||
}
|
||||
|
||||
++$columnIndex;
|
||||
}
|
||||
|
||||
++$rowIndex;
|
||||
|
||||
if ($rowHasData) {
|
||||
$tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
|
||||
$tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
|
||||
|
||||
$worksheetInfo[] = $tmpInfo;
|
||||
++$worksheetID;
|
||||
}
|
||||
|
||||
return $worksheetInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function load($pFilename)
|
||||
{
|
||||
// Create new PHPExcel
|
||||
$objPHPExcel = new PHPExcel();
|
||||
$objPHPExcel->removeSheetByIndex(0);
|
||||
|
||||
// Load into this instance
|
||||
return $this->loadIntoExisting($pFilename, $objPHPExcel);
|
||||
}
|
||||
|
||||
|
||||
protected static function identifyFixedStyleValue($styleList,&$styleAttributeValue) {
|
||||
$styleAttributeValue = strtolower($styleAttributeValue);
|
||||
foreach($styleList as $style) {
|
||||
if ($styleAttributeValue == strtolower($style)) {
|
||||
$styleAttributeValue = $style;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* pixel units to excel width units(units of 1/256th of a character width)
|
||||
* @param pxs
|
||||
* @return
|
||||
*/
|
||||
protected static function _pixel2WidthUnits($pxs) {
|
||||
$UNIT_OFFSET_MAP = array(0, 36, 73, 109, 146, 182, 219);
|
||||
|
||||
$widthUnits = 256 * ($pxs / 7);
|
||||
$widthUnits += $UNIT_OFFSET_MAP[($pxs % 7)];
|
||||
return $widthUnits;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* excel width units(units of 1/256th of a character width) to pixel units
|
||||
* @param widthUnits
|
||||
* @return
|
||||
*/
|
||||
protected static function _widthUnits2Pixel($widthUnits) {
|
||||
$pixels = ($widthUnits / 256) * 7;
|
||||
$offsetWidthUnits = $widthUnits % 256;
|
||||
$pixels += round($offsetWidthUnits / (256 / 7));
|
||||
return $pixels;
|
||||
}
|
||||
|
||||
|
||||
protected static function _hex2str($hex) {
|
||||
return chr(hexdec($hex[1]));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file into PHPExcel instance
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @param PHPExcel $objPHPExcel
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
|
||||
{
|
||||
$fromFormats = array('\-', '\ ');
|
||||
$toFormats = array('-', ' ');
|
||||
|
||||
$underlineStyles = array (
|
||||
PHPExcel_Style_Font::UNDERLINE_NONE,
|
||||
PHPExcel_Style_Font::UNDERLINE_DOUBLE,
|
||||
PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING,
|
||||
PHPExcel_Style_Font::UNDERLINE_SINGLE,
|
||||
PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING
|
||||
);
|
||||
$verticalAlignmentStyles = array (
|
||||
PHPExcel_Style_Alignment::VERTICAL_BOTTOM,
|
||||
PHPExcel_Style_Alignment::VERTICAL_TOP,
|
||||
PHPExcel_Style_Alignment::VERTICAL_CENTER,
|
||||
PHPExcel_Style_Alignment::VERTICAL_JUSTIFY
|
||||
);
|
||||
$horizontalAlignmentStyles = array (
|
||||
PHPExcel_Style_Alignment::HORIZONTAL_GENERAL,
|
||||
PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
|
||||
PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
|
||||
PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
|
||||
PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS,
|
||||
PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY
|
||||
);
|
||||
|
||||
$timezoneObj = new DateTimeZone('Europe/London');
|
||||
$GMT = new DateTimeZone('UTC');
|
||||
|
||||
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
if (!$this->canRead($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
|
||||
}
|
||||
|
||||
$xml = simplexml_load_string($this->securityScan(file_get_contents($pFilename)), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());
|
||||
$namespaces = $xml->getNamespaces(true);
|
||||
|
||||
$docProps = $objPHPExcel->getProperties();
|
||||
if (isset($xml->DocumentProperties[0])) {
|
||||
foreach($xml->DocumentProperties[0] as $propertyName => $propertyValue) {
|
||||
switch ($propertyName) {
|
||||
case 'Title' :
|
||||
$docProps->setTitle(self::_convertStringEncoding($propertyValue,$this->_charSet));
|
||||
break;
|
||||
case 'Subject' :
|
||||
$docProps->setSubject(self::_convertStringEncoding($propertyValue,$this->_charSet));
|
||||
break;
|
||||
case 'Author' :
|
||||
$docProps->setCreator(self::_convertStringEncoding($propertyValue,$this->_charSet));
|
||||
break;
|
||||
case 'Created' :
|
||||
$creationDate = strtotime($propertyValue);
|
||||
$docProps->setCreated($creationDate);
|
||||
break;
|
||||
case 'LastAuthor' :
|
||||
$docProps->setLastModifiedBy(self::_convertStringEncoding($propertyValue,$this->_charSet));
|
||||
break;
|
||||
case 'LastSaved' :
|
||||
$lastSaveDate = strtotime($propertyValue);
|
||||
$docProps->setModified($lastSaveDate);
|
||||
break;
|
||||
case 'Company' :
|
||||
$docProps->setCompany(self::_convertStringEncoding($propertyValue,$this->_charSet));
|
||||
break;
|
||||
case 'Category' :
|
||||
$docProps->setCategory(self::_convertStringEncoding($propertyValue,$this->_charSet));
|
||||
break;
|
||||
case 'Manager' :
|
||||
$docProps->setManager(self::_convertStringEncoding($propertyValue,$this->_charSet));
|
||||
break;
|
||||
case 'Keywords' :
|
||||
$docProps->setKeywords(self::_convertStringEncoding($propertyValue,$this->_charSet));
|
||||
break;
|
||||
case 'Description' :
|
||||
$docProps->setDescription(self::_convertStringEncoding($propertyValue,$this->_charSet));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isset($xml->CustomDocumentProperties)) {
|
||||
foreach($xml->CustomDocumentProperties[0] as $propertyName => $propertyValue) {
|
||||
$propertyAttributes = $propertyValue->attributes($namespaces['dt']);
|
||||
$propertyName = preg_replace_callback('/_x([0-9a-z]{4})_/','PHPExcel_Reader_Excel2003XML::_hex2str',$propertyName);
|
||||
$propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_UNKNOWN;
|
||||
switch((string) $propertyAttributes) {
|
||||
case 'string' :
|
||||
$propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_STRING;
|
||||
$propertyValue = trim($propertyValue);
|
||||
break;
|
||||
case 'boolean' :
|
||||
$propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_BOOLEAN;
|
||||
$propertyValue = (bool) $propertyValue;
|
||||
break;
|
||||
case 'integer' :
|
||||
$propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_INTEGER;
|
||||
$propertyValue = intval($propertyValue);
|
||||
break;
|
||||
case 'float' :
|
||||
$propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_FLOAT;
|
||||
$propertyValue = floatval($propertyValue);
|
||||
break;
|
||||
case 'dateTime.tz' :
|
||||
$propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_DATE;
|
||||
$propertyValue = strtotime(trim($propertyValue));
|
||||
break;
|
||||
}
|
||||
$docProps->setCustomProperty($propertyName,$propertyValue,$propertyType);
|
||||
}
|
||||
}
|
||||
|
||||
foreach($xml->Styles[0] as $style) {
|
||||
$style_ss = $style->attributes($namespaces['ss']);
|
||||
$styleID = (string) $style_ss['ID'];
|
||||
// echo 'Style ID = '.$styleID.'<br />';
|
||||
if ($styleID == 'Default') {
|
||||
$this->_styles['Default'] = array();
|
||||
} else {
|
||||
$this->_styles[$styleID] = $this->_styles['Default'];
|
||||
}
|
||||
foreach ($style as $styleType => $styleData) {
|
||||
$styleAttributes = $styleData->attributes($namespaces['ss']);
|
||||
// echo $styleType.'<br />';
|
||||
switch ($styleType) {
|
||||
case 'Alignment' :
|
||||
foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
|
||||
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
|
||||
$styleAttributeValue = (string) $styleAttributeValue;
|
||||
switch ($styleAttributeKey) {
|
||||
case 'Vertical' :
|
||||
if (self::identifyFixedStyleValue($verticalAlignmentStyles,$styleAttributeValue)) {
|
||||
$this->_styles[$styleID]['alignment']['vertical'] = $styleAttributeValue;
|
||||
}
|
||||
break;
|
||||
case 'Horizontal' :
|
||||
if (self::identifyFixedStyleValue($horizontalAlignmentStyles,$styleAttributeValue)) {
|
||||
$this->_styles[$styleID]['alignment']['horizontal'] = $styleAttributeValue;
|
||||
}
|
||||
break;
|
||||
case 'WrapText' :
|
||||
$this->_styles[$styleID]['alignment']['wrap'] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'Borders' :
|
||||
foreach($styleData->Border as $borderStyle) {
|
||||
$borderAttributes = $borderStyle->attributes($namespaces['ss']);
|
||||
$thisBorder = array();
|
||||
foreach($borderAttributes as $borderStyleKey => $borderStyleValue) {
|
||||
// echo $borderStyleKey.' = '.$borderStyleValue.'<br />';
|
||||
switch ($borderStyleKey) {
|
||||
case 'LineStyle' :
|
||||
$thisBorder['style'] = PHPExcel_Style_Border::BORDER_MEDIUM;
|
||||
// $thisBorder['style'] = $borderStyleValue;
|
||||
break;
|
||||
case 'Weight' :
|
||||
// $thisBorder['style'] = $borderStyleValue;
|
||||
break;
|
||||
case 'Position' :
|
||||
$borderPosition = strtolower($borderStyleValue);
|
||||
break;
|
||||
case 'Color' :
|
||||
$borderColour = substr($borderStyleValue,1);
|
||||
$thisBorder['color']['rgb'] = $borderColour;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!empty($thisBorder)) {
|
||||
if (($borderPosition == 'left') || ($borderPosition == 'right') || ($borderPosition == 'top') || ($borderPosition == 'bottom')) {
|
||||
$this->_styles[$styleID]['borders'][$borderPosition] = $thisBorder;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'Font' :
|
||||
foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
|
||||
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
|
||||
$styleAttributeValue = (string) $styleAttributeValue;
|
||||
switch ($styleAttributeKey) {
|
||||
case 'FontName' :
|
||||
$this->_styles[$styleID]['font']['name'] = $styleAttributeValue;
|
||||
break;
|
||||
case 'Size' :
|
||||
$this->_styles[$styleID]['font']['size'] = $styleAttributeValue;
|
||||
break;
|
||||
case 'Color' :
|
||||
$this->_styles[$styleID]['font']['color']['rgb'] = substr($styleAttributeValue,1);
|
||||
break;
|
||||
case 'Bold' :
|
||||
$this->_styles[$styleID]['font']['bold'] = true;
|
||||
break;
|
||||
case 'Italic' :
|
||||
$this->_styles[$styleID]['font']['italic'] = true;
|
||||
break;
|
||||
case 'Underline' :
|
||||
if (self::identifyFixedStyleValue($underlineStyles,$styleAttributeValue)) {
|
||||
$this->_styles[$styleID]['font']['underline'] = $styleAttributeValue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'Interior' :
|
||||
foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
|
||||
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
|
||||
switch ($styleAttributeKey) {
|
||||
case 'Color' :
|
||||
$this->_styles[$styleID]['fill']['color']['rgb'] = substr($styleAttributeValue,1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'NumberFormat' :
|
||||
foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
|
||||
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
|
||||
$styleAttributeValue = str_replace($fromFormats,$toFormats,$styleAttributeValue);
|
||||
switch ($styleAttributeValue) {
|
||||
case 'Short Date' :
|
||||
$styleAttributeValue = 'dd/mm/yyyy';
|
||||
break;
|
||||
}
|
||||
if ($styleAttributeValue > '') {
|
||||
$this->_styles[$styleID]['numberformat']['code'] = $styleAttributeValue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'Protection' :
|
||||
foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
|
||||
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
// print_r($this->_styles[$styleID]);
|
||||
// echo '<hr />';
|
||||
}
|
||||
// echo '<hr />';
|
||||
|
||||
$worksheetID = 0;
|
||||
$xml_ss = $xml->children($namespaces['ss']);
|
||||
|
||||
foreach($xml_ss->Worksheet as $worksheet) {
|
||||
$worksheet_ss = $worksheet->attributes($namespaces['ss']);
|
||||
|
||||
if ((isset($this->_loadSheetsOnly)) && (isset($worksheet_ss['Name'])) &&
|
||||
(!in_array($worksheet_ss['Name'], $this->_loadSheetsOnly))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// echo '<h3>Worksheet: ',$worksheet_ss['Name'],'<h3>';
|
||||
//
|
||||
// Create new Worksheet
|
||||
$objPHPExcel->createSheet();
|
||||
$objPHPExcel->setActiveSheetIndex($worksheetID);
|
||||
if (isset($worksheet_ss['Name'])) {
|
||||
$worksheetName = self::_convertStringEncoding((string) $worksheet_ss['Name'],$this->_charSet);
|
||||
// Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
|
||||
// formula cells... during the load, all formulae should be correct, and we're simply bringing
|
||||
// the worksheet name in line with the formula, not the reverse
|
||||
$objPHPExcel->getActiveSheet()->setTitle($worksheetName,false);
|
||||
}
|
||||
|
||||
$columnID = 'A';
|
||||
if (isset($worksheet->Table->Column)) {
|
||||
foreach($worksheet->Table->Column as $columnData) {
|
||||
$columnData_ss = $columnData->attributes($namespaces['ss']);
|
||||
if (isset($columnData_ss['Index'])) {
|
||||
$columnID = PHPExcel_Cell::stringFromColumnIndex($columnData_ss['Index']-1);
|
||||
}
|
||||
if (isset($columnData_ss['Width'])) {
|
||||
$columnWidth = $columnData_ss['Width'];
|
||||
// echo '<b>Setting column width for '.$columnID.' to '.$columnWidth.'</b><br />';
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setWidth($columnWidth / 5.4);
|
||||
}
|
||||
++$columnID;
|
||||
}
|
||||
}
|
||||
|
||||
$rowID = 1;
|
||||
if (isset($worksheet->Table->Row)) {
|
||||
$additionalMergedCells = 0;
|
||||
foreach($worksheet->Table->Row as $rowData) {
|
||||
$rowHasData = false;
|
||||
$row_ss = $rowData->attributes($namespaces['ss']);
|
||||
if (isset($row_ss['Index'])) {
|
||||
$rowID = (integer) $row_ss['Index'];
|
||||
}
|
||||
// echo '<b>Row '.$rowID.'</b><br />';
|
||||
|
||||
$columnID = 'A';
|
||||
foreach($rowData->Cell as $cell) {
|
||||
|
||||
$cell_ss = $cell->attributes($namespaces['ss']);
|
||||
if (isset($cell_ss['Index'])) {
|
||||
$columnID = PHPExcel_Cell::stringFromColumnIndex($cell_ss['Index']-1);
|
||||
}
|
||||
$cellRange = $columnID.$rowID;
|
||||
|
||||
if ($this->getReadFilter() !== NULL) {
|
||||
if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if ((isset($cell_ss['MergeAcross'])) || (isset($cell_ss['MergeDown']))) {
|
||||
$columnTo = $columnID;
|
||||
if (isset($cell_ss['MergeAcross'])) {
|
||||
$additionalMergedCells += (int)$cell_ss['MergeAcross'];
|
||||
$columnTo = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($columnID) + $cell_ss['MergeAcross'] -1);
|
||||
}
|
||||
$rowTo = $rowID;
|
||||
if (isset($cell_ss['MergeDown'])) {
|
||||
$rowTo = $rowTo + $cell_ss['MergeDown'];
|
||||
}
|
||||
$cellRange .= ':'.$columnTo.$rowTo;
|
||||
$objPHPExcel->getActiveSheet()->mergeCells($cellRange);
|
||||
}
|
||||
|
||||
$cellIsSet = $hasCalculatedValue = false;
|
||||
$cellDataFormula = '';
|
||||
if (isset($cell_ss['Formula'])) {
|
||||
$cellDataFormula = $cell_ss['Formula'];
|
||||
// added this as a check for array formulas
|
||||
if (isset($cell_ss['ArrayRange'])) {
|
||||
$cellDataCSEFormula = $cell_ss['ArrayRange'];
|
||||
// echo "found an array formula at ".$columnID.$rowID."<br />";
|
||||
}
|
||||
$hasCalculatedValue = true;
|
||||
}
|
||||
if (isset($cell->Data)) {
|
||||
$cellValue = $cellData = $cell->Data;
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NULL;
|
||||
$cellData_ss = $cellData->attributes($namespaces['ss']);
|
||||
if (isset($cellData_ss['Type'])) {
|
||||
$cellDataType = $cellData_ss['Type'];
|
||||
switch ($cellDataType) {
|
||||
/*
|
||||
const TYPE_STRING = 's';
|
||||
const TYPE_FORMULA = 'f';
|
||||
const TYPE_NUMERIC = 'n';
|
||||
const TYPE_BOOL = 'b';
|
||||
const TYPE_NULL = 'null';
|
||||
const TYPE_INLINE = 'inlineStr';
|
||||
const TYPE_ERROR = 'e';
|
||||
*/
|
||||
case 'String' :
|
||||
$cellValue = self::_convertStringEncoding($cellValue,$this->_charSet);
|
||||
$type = PHPExcel_Cell_DataType::TYPE_STRING;
|
||||
break;
|
||||
case 'Number' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
|
||||
$cellValue = (float) $cellValue;
|
||||
if (floor($cellValue) == $cellValue) {
|
||||
$cellValue = (integer) $cellValue;
|
||||
}
|
||||
break;
|
||||
case 'Boolean' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_BOOL;
|
||||
$cellValue = ($cellValue != 0);
|
||||
break;
|
||||
case 'DateTime' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
|
||||
$cellValue = PHPExcel_Shared_Date::PHPToExcel(strtotime($cellValue));
|
||||
break;
|
||||
case 'Error' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($hasCalculatedValue) {
|
||||
// echo 'FORMULA<br />';
|
||||
$type = PHPExcel_Cell_DataType::TYPE_FORMULA;
|
||||
$columnNumber = PHPExcel_Cell::columnIndexFromString($columnID);
|
||||
if (substr($cellDataFormula,0,3) == 'of:') {
|
||||
$cellDataFormula = substr($cellDataFormula,3);
|
||||
// echo 'Before: ',$cellDataFormula,'<br />';
|
||||
$temp = explode('"',$cellDataFormula);
|
||||
$key = false;
|
||||
foreach($temp as &$value) {
|
||||
// Only replace in alternate array entries (i.e. non-quoted blocks)
|
||||
if ($key = !$key) {
|
||||
$value = str_replace(array('[.','.',']'),'',$value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Convert R1C1 style references to A1 style references (but only when not quoted)
|
||||
// echo 'Before: ',$cellDataFormula,'<br />';
|
||||
$temp = explode('"',$cellDataFormula);
|
||||
$key = false;
|
||||
foreach($temp as &$value) {
|
||||
// Only replace in alternate array entries (i.e. non-quoted blocks)
|
||||
if ($key = !$key) {
|
||||
preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
|
||||
// Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
|
||||
// through the formula from left to right. Reversing means that we work right to left.through
|
||||
// the formula
|
||||
$cellReferences = array_reverse($cellReferences);
|
||||
// Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
|
||||
// then modify the formula to use that new reference
|
||||
foreach($cellReferences as $cellReference) {
|
||||
$rowReference = $cellReference[2][0];
|
||||
// Empty R reference is the current row
|
||||
if ($rowReference == '') $rowReference = $rowID;
|
||||
// Bracketed R references are relative to the current row
|
||||
if ($rowReference{0} == '[') $rowReference = $rowID + trim($rowReference,'[]');
|
||||
$columnReference = $cellReference[4][0];
|
||||
// Empty C reference is the current column
|
||||
if ($columnReference == '') $columnReference = $columnNumber;
|
||||
// Bracketed C references are relative to the current column
|
||||
if ($columnReference{0} == '[') $columnReference = $columnNumber + trim($columnReference,'[]');
|
||||
$A1CellReference = PHPExcel_Cell::stringFromColumnIndex($columnReference-1).$rowReference;
|
||||
$value = substr_replace($value,$A1CellReference,$cellReference[0][1],strlen($cellReference[0][0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($value);
|
||||
// Then rebuild the formula string
|
||||
$cellDataFormula = implode('"',$temp);
|
||||
// echo 'After: ',$cellDataFormula,'<br />';
|
||||
}
|
||||
|
||||
// echo 'Cell '.$columnID.$rowID.' is a '.$type.' with a value of '.(($hasCalculatedValue) ? $cellDataFormula : $cellValue).'<br />';
|
||||
//
|
||||
$objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $cellValue),$type);
|
||||
if ($hasCalculatedValue) {
|
||||
// echo 'Formula result is '.$cellValue.'<br />';
|
||||
$objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setCalculatedValue($cellValue);
|
||||
}
|
||||
$cellIsSet = $rowHasData = true;
|
||||
}
|
||||
|
||||
if (isset($cell->Comment)) {
|
||||
// echo '<b>comment found</b><br />';
|
||||
$commentAttributes = $cell->Comment->attributes($namespaces['ss']);
|
||||
$author = 'unknown';
|
||||
if (isset($commentAttributes->Author)) {
|
||||
$author = (string)$commentAttributes->Author;
|
||||
// echo 'Author: ',$author,'<br />';
|
||||
}
|
||||
$node = $cell->Comment->Data->asXML();
|
||||
// $annotation = str_replace('html:','',substr($node,49,-10));
|
||||
// echo $annotation,'<br />';
|
||||
$annotation = strip_tags($node);
|
||||
// echo 'Annotation: ',$annotation,'<br />';
|
||||
$objPHPExcel->getActiveSheet()->getComment( $columnID.$rowID )
|
||||
->setAuthor(self::_convertStringEncoding($author ,$this->_charSet))
|
||||
->setText($this->_parseRichText($annotation) );
|
||||
}
|
||||
|
||||
if (($cellIsSet) && (isset($cell_ss['StyleID']))) {
|
||||
$style = (string) $cell_ss['StyleID'];
|
||||
// echo 'Cell style for '.$columnID.$rowID.' is '.$style.'<br />';
|
||||
if ((isset($this->_styles[$style])) && (!empty($this->_styles[$style]))) {
|
||||
// echo 'Cell '.$columnID.$rowID.'<br />';
|
||||
// print_r($this->_styles[$style]);
|
||||
// echo '<br />';
|
||||
if (!$objPHPExcel->getActiveSheet()->cellExists($columnID.$rowID)) {
|
||||
$objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setValue(NULL);
|
||||
}
|
||||
$objPHPExcel->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->_styles[$style]);
|
||||
}
|
||||
}
|
||||
++$columnID;
|
||||
while ($additionalMergedCells > 0) {
|
||||
++$columnID;
|
||||
$additionalMergedCells--;
|
||||
}
|
||||
}
|
||||
|
||||
if ($rowHasData) {
|
||||
if (isset($row_ss['StyleID'])) {
|
||||
$rowStyle = $row_ss['StyleID'];
|
||||
}
|
||||
if (isset($row_ss['Height'])) {
|
||||
$rowHeight = $row_ss['Height'];
|
||||
// echo '<b>Setting row height to '.$rowHeight.'</b><br />';
|
||||
$objPHPExcel->getActiveSheet()->getRowDimension($rowID)->setRowHeight($rowHeight);
|
||||
}
|
||||
}
|
||||
|
||||
++$rowID;
|
||||
}
|
||||
}
|
||||
++$worksheetID;
|
||||
}
|
||||
|
||||
// Return
|
||||
return $objPHPExcel;
|
||||
}
|
||||
|
||||
|
||||
protected static function _convertStringEncoding($string,$charset) {
|
||||
if ($charset != 'UTF-8') {
|
||||
return PHPExcel_Shared_String::ConvertEncoding($string,'UTF-8',$charset);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
|
||||
protected function _parseRichText($is = '') {
|
||||
$value = new PHPExcel_RichText();
|
||||
|
||||
$value->createText(self::_convertStringEncoding($is,$this->_charSet));
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_Exception
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
class PHPExcel_Reader_Exception extends PHPExcel_Exception {
|
||||
/**
|
||||
* Error handler callback
|
||||
*
|
||||
* @param mixed $code
|
||||
* @param mixed $string
|
||||
* @param mixed $file
|
||||
* @param mixed $line
|
||||
* @param mixed $context
|
||||
*/
|
||||
public static function errorHandlerCallback($code, $string, $file, $line, $context) {
|
||||
$e = new self($string, $code);
|
||||
$e->line = $line;
|
||||
$e->file = $file;
|
||||
throw $e;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,873 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/** PHPExcel root directory */
|
||||
if (!defined('PHPEXCEL_ROOT')) {
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
|
||||
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_Gnumeric
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
class PHPExcel_Reader_Gnumeric extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
|
||||
{
|
||||
/**
|
||||
* Formats
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_styles = array();
|
||||
|
||||
/**
|
||||
* Shared Expressions
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_expressions = array();
|
||||
|
||||
private $_referenceHelper = null;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new PHPExcel_Reader_Gnumeric
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
|
||||
$this->_referenceHelper = PHPExcel_ReferenceHelper::getInstance();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Can the current PHPExcel_Reader_IReader read the file?
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return boolean
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function canRead($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
// Check if gzlib functions are available
|
||||
if (!function_exists('gzread')) {
|
||||
throw new PHPExcel_Reader_Exception("gzlib library is not enabled");
|
||||
}
|
||||
|
||||
// Read signature data (first 3 bytes)
|
||||
$fh = fopen($pFilename, 'r');
|
||||
$data = fread($fh, 2);
|
||||
fclose($fh);
|
||||
|
||||
if ($data != chr(0x1F).chr(0x8B)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function listWorksheetNames($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
$xml = new XMLReader();
|
||||
$xml->xml(
|
||||
$this->securityScanFile('compress.zlib://'.realpath($pFilename)), null, PHPExcel_Settings::getLibXmlLoaderOptions()
|
||||
);
|
||||
$xml->setParserProperty(2,true);
|
||||
|
||||
$worksheetNames = array();
|
||||
while ($xml->read()) {
|
||||
if ($xml->name == 'gnm:SheetName' && $xml->nodeType == XMLReader::ELEMENT) {
|
||||
$xml->read(); // Move onto the value node
|
||||
$worksheetNames[] = (string) $xml->value;
|
||||
} elseif ($xml->name == 'gnm:Sheets') {
|
||||
// break out of the loop once we've got our sheet names rather than parse the entire file
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $worksheetNames;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function listWorksheetInfo($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
$xml = new XMLReader();
|
||||
$xml->xml(
|
||||
$this->securityScanFile('compress.zlib://'.realpath($pFilename)), null, PHPExcel_Settings::getLibXmlLoaderOptions()
|
||||
);
|
||||
$xml->setParserProperty(2,true);
|
||||
|
||||
$worksheetInfo = array();
|
||||
while ($xml->read()) {
|
||||
if ($xml->name == 'gnm:Sheet' && $xml->nodeType == XMLReader::ELEMENT) {
|
||||
$tmpInfo = array(
|
||||
'worksheetName' => '',
|
||||
'lastColumnLetter' => 'A',
|
||||
'lastColumnIndex' => 0,
|
||||
'totalRows' => 0,
|
||||
'totalColumns' => 0,
|
||||
);
|
||||
|
||||
while ($xml->read()) {
|
||||
if ($xml->name == 'gnm:Name' && $xml->nodeType == XMLReader::ELEMENT) {
|
||||
$xml->read(); // Move onto the value node
|
||||
$tmpInfo['worksheetName'] = (string) $xml->value;
|
||||
} elseif ($xml->name == 'gnm:MaxCol' && $xml->nodeType == XMLReader::ELEMENT) {
|
||||
$xml->read(); // Move onto the value node
|
||||
$tmpInfo['lastColumnIndex'] = (int) $xml->value;
|
||||
$tmpInfo['totalColumns'] = (int) $xml->value + 1;
|
||||
} elseif ($xml->name == 'gnm:MaxRow' && $xml->nodeType == XMLReader::ELEMENT) {
|
||||
$xml->read(); // Move onto the value node
|
||||
$tmpInfo['totalRows'] = (int) $xml->value + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
|
||||
$worksheetInfo[] = $tmpInfo;
|
||||
}
|
||||
}
|
||||
|
||||
return $worksheetInfo;
|
||||
}
|
||||
|
||||
|
||||
private function _gzfileGetContents($filename) {
|
||||
$file = @gzopen($filename, 'rb');
|
||||
if ($file !== false) {
|
||||
$data = '';
|
||||
while (!gzeof($file)) {
|
||||
$data .= gzread($file, 1024);
|
||||
}
|
||||
gzclose($file);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function load($pFilename)
|
||||
{
|
||||
// Create new PHPExcel
|
||||
$objPHPExcel = new PHPExcel();
|
||||
|
||||
// Load into this instance
|
||||
return $this->loadIntoExisting($pFilename, $objPHPExcel);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file into PHPExcel instance
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @param PHPExcel $objPHPExcel
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
$timezoneObj = new DateTimeZone('Europe/London');
|
||||
$GMT = new DateTimeZone('UTC');
|
||||
|
||||
$gFileData = $this->_gzfileGetContents($pFilename);
|
||||
|
||||
// echo '<pre>';
|
||||
// echo htmlentities($gFileData,ENT_QUOTES,'UTF-8');
|
||||
// echo '</pre><hr />';
|
||||
//
|
||||
$xml = simplexml_load_string($this->securityScan($gFileData), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());
|
||||
$namespacesMeta = $xml->getNamespaces(true);
|
||||
|
||||
// var_dump($namespacesMeta);
|
||||
//
|
||||
$gnmXML = $xml->children($namespacesMeta['gnm']);
|
||||
|
||||
$docProps = $objPHPExcel->getProperties();
|
||||
// Document Properties are held differently, depending on the version of Gnumeric
|
||||
if (isset($namespacesMeta['office'])) {
|
||||
$officeXML = $xml->children($namespacesMeta['office']);
|
||||
$officeDocXML = $officeXML->{'document-meta'};
|
||||
$officeDocMetaXML = $officeDocXML->meta;
|
||||
|
||||
foreach($officeDocMetaXML as $officePropertyData) {
|
||||
|
||||
$officePropertyDC = array();
|
||||
if (isset($namespacesMeta['dc'])) {
|
||||
$officePropertyDC = $officePropertyData->children($namespacesMeta['dc']);
|
||||
}
|
||||
foreach($officePropertyDC as $propertyName => $propertyValue) {
|
||||
$propertyValue = (string) $propertyValue;
|
||||
switch ($propertyName) {
|
||||
case 'title' :
|
||||
$docProps->setTitle(trim($propertyValue));
|
||||
break;
|
||||
case 'subject' :
|
||||
$docProps->setSubject(trim($propertyValue));
|
||||
break;
|
||||
case 'creator' :
|
||||
$docProps->setCreator(trim($propertyValue));
|
||||
$docProps->setLastModifiedBy(trim($propertyValue));
|
||||
break;
|
||||
case 'date' :
|
||||
$creationDate = strtotime(trim($propertyValue));
|
||||
$docProps->setCreated($creationDate);
|
||||
$docProps->setModified($creationDate);
|
||||
break;
|
||||
case 'description' :
|
||||
$docProps->setDescription(trim($propertyValue));
|
||||
break;
|
||||
}
|
||||
}
|
||||
$officePropertyMeta = array();
|
||||
if (isset($namespacesMeta['meta'])) {
|
||||
$officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
|
||||
}
|
||||
foreach($officePropertyMeta as $propertyName => $propertyValue) {
|
||||
$attributes = $propertyValue->attributes($namespacesMeta['meta']);
|
||||
$propertyValue = (string) $propertyValue;
|
||||
switch ($propertyName) {
|
||||
case 'keyword' :
|
||||
$docProps->setKeywords(trim($propertyValue));
|
||||
break;
|
||||
case 'initial-creator' :
|
||||
$docProps->setCreator(trim($propertyValue));
|
||||
$docProps->setLastModifiedBy(trim($propertyValue));
|
||||
break;
|
||||
case 'creation-date' :
|
||||
$creationDate = strtotime(trim($propertyValue));
|
||||
$docProps->setCreated($creationDate);
|
||||
$docProps->setModified($creationDate);
|
||||
break;
|
||||
case 'user-defined' :
|
||||
list(,$attrName) = explode(':',$attributes['name']);
|
||||
switch ($attrName) {
|
||||
case 'publisher' :
|
||||
$docProps->setCompany(trim($propertyValue));
|
||||
break;
|
||||
case 'category' :
|
||||
$docProps->setCategory(trim($propertyValue));
|
||||
break;
|
||||
case 'manager' :
|
||||
$docProps->setManager(trim($propertyValue));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif (isset($gnmXML->Summary)) {
|
||||
foreach($gnmXML->Summary->Item as $summaryItem) {
|
||||
$propertyName = $summaryItem->name;
|
||||
$propertyValue = $summaryItem->{'val-string'};
|
||||
switch ($propertyName) {
|
||||
case 'title' :
|
||||
$docProps->setTitle(trim($propertyValue));
|
||||
break;
|
||||
case 'comments' :
|
||||
$docProps->setDescription(trim($propertyValue));
|
||||
break;
|
||||
case 'keywords' :
|
||||
$docProps->setKeywords(trim($propertyValue));
|
||||
break;
|
||||
case 'category' :
|
||||
$docProps->setCategory(trim($propertyValue));
|
||||
break;
|
||||
case 'manager' :
|
||||
$docProps->setManager(trim($propertyValue));
|
||||
break;
|
||||
case 'author' :
|
||||
$docProps->setCreator(trim($propertyValue));
|
||||
$docProps->setLastModifiedBy(trim($propertyValue));
|
||||
break;
|
||||
case 'company' :
|
||||
$docProps->setCompany(trim($propertyValue));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$worksheetID = 0;
|
||||
foreach($gnmXML->Sheets->Sheet as $sheet) {
|
||||
$worksheetName = (string) $sheet->Name;
|
||||
// echo '<b>Worksheet: ',$worksheetName,'</b><br />';
|
||||
if ((isset($this->_loadSheetsOnly)) && (!in_array($worksheetName, $this->_loadSheetsOnly))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$maxRow = $maxCol = 0;
|
||||
|
||||
// Create new Worksheet
|
||||
$objPHPExcel->createSheet();
|
||||
$objPHPExcel->setActiveSheetIndex($worksheetID);
|
||||
// Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula
|
||||
// cells... during the load, all formulae should be correct, and we're simply bringing the worksheet
|
||||
// name in line with the formula, not the reverse
|
||||
$objPHPExcel->getActiveSheet()->setTitle($worksheetName,false);
|
||||
|
||||
if ((!$this->_readDataOnly) && (isset($sheet->PrintInformation))) {
|
||||
if (isset($sheet->PrintInformation->Margins)) {
|
||||
foreach($sheet->PrintInformation->Margins->children('gnm',TRUE) as $key => $margin) {
|
||||
$marginAttributes = $margin->attributes();
|
||||
$marginSize = 72 / 100; // Default
|
||||
switch($marginAttributes['PrefUnit']) {
|
||||
case 'mm' :
|
||||
$marginSize = intval($marginAttributes['Points']) / 100;
|
||||
break;
|
||||
}
|
||||
switch($key) {
|
||||
case 'top' :
|
||||
$objPHPExcel->getActiveSheet()->getPageMargins()->setTop($marginSize);
|
||||
break;
|
||||
case 'bottom' :
|
||||
$objPHPExcel->getActiveSheet()->getPageMargins()->setBottom($marginSize);
|
||||
break;
|
||||
case 'left' :
|
||||
$objPHPExcel->getActiveSheet()->getPageMargins()->setLeft($marginSize);
|
||||
break;
|
||||
case 'right' :
|
||||
$objPHPExcel->getActiveSheet()->getPageMargins()->setRight($marginSize);
|
||||
break;
|
||||
case 'header' :
|
||||
$objPHPExcel->getActiveSheet()->getPageMargins()->setHeader($marginSize);
|
||||
break;
|
||||
case 'footer' :
|
||||
$objPHPExcel->getActiveSheet()->getPageMargins()->setFooter($marginSize);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach($sheet->Cells->Cell as $cell) {
|
||||
$cellAttributes = $cell->attributes();
|
||||
$row = (int) $cellAttributes->Row + 1;
|
||||
$column = (int) $cellAttributes->Col;
|
||||
|
||||
if ($row > $maxRow) $maxRow = $row;
|
||||
if ($column > $maxCol) $maxCol = $column;
|
||||
|
||||
$column = PHPExcel_Cell::stringFromColumnIndex($column);
|
||||
|
||||
// Read cell?
|
||||
if ($this->getReadFilter() !== NULL) {
|
||||
if (!$this->getReadFilter()->readCell($column, $row, $worksheetName)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$ValueType = $cellAttributes->ValueType;
|
||||
$ExprID = (string) $cellAttributes->ExprID;
|
||||
// echo 'Cell ',$column,$row,'<br />';
|
||||
// echo 'Type is ',$ValueType,'<br />';
|
||||
// echo 'Value is ',$cell,'<br />';
|
||||
$type = PHPExcel_Cell_DataType::TYPE_FORMULA;
|
||||
if ($ExprID > '') {
|
||||
if (((string) $cell) > '') {
|
||||
|
||||
$this->_expressions[$ExprID] = array( 'column' => $cellAttributes->Col,
|
||||
'row' => $cellAttributes->Row,
|
||||
'formula' => (string) $cell
|
||||
);
|
||||
// echo 'NEW EXPRESSION ',$ExprID,'<br />';
|
||||
} else {
|
||||
$expression = $this->_expressions[$ExprID];
|
||||
|
||||
$cell = $this->_referenceHelper->updateFormulaReferences( $expression['formula'],
|
||||
'A1',
|
||||
$cellAttributes->Col - $expression['column'],
|
||||
$cellAttributes->Row - $expression['row'],
|
||||
$worksheetName
|
||||
);
|
||||
// echo 'SHARED EXPRESSION ',$ExprID,'<br />';
|
||||
// echo 'New Value is ',$cell,'<br />';
|
||||
}
|
||||
$type = PHPExcel_Cell_DataType::TYPE_FORMULA;
|
||||
} else {
|
||||
switch($ValueType) {
|
||||
case '10' : // NULL
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NULL;
|
||||
break;
|
||||
case '20' : // Boolean
|
||||
$type = PHPExcel_Cell_DataType::TYPE_BOOL;
|
||||
$cell = ($cell == 'TRUE') ? True : False;
|
||||
break;
|
||||
case '30' : // Integer
|
||||
$cell = intval($cell);
|
||||
case '40' : // Float
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
|
||||
break;
|
||||
case '50' : // Error
|
||||
$type = PHPExcel_Cell_DataType::TYPE_ERROR;
|
||||
break;
|
||||
case '60' : // String
|
||||
$type = PHPExcel_Cell_DataType::TYPE_STRING;
|
||||
break;
|
||||
case '70' : // Cell Range
|
||||
case '80' : // Array
|
||||
}
|
||||
}
|
||||
$objPHPExcel->getActiveSheet()->getCell($column.$row)->setValueExplicit($cell,$type);
|
||||
}
|
||||
|
||||
if ((!$this->_readDataOnly) && (isset($sheet->Objects))) {
|
||||
foreach($sheet->Objects->children('gnm',TRUE) as $key => $comment) {
|
||||
$commentAttributes = $comment->attributes();
|
||||
// Only comment objects are handled at the moment
|
||||
if ($commentAttributes->Text) {
|
||||
$objPHPExcel->getActiveSheet()->getComment( (string)$commentAttributes->ObjectBound )
|
||||
->setAuthor( (string)$commentAttributes->Author )
|
||||
->setText($this->_parseRichText((string)$commentAttributes->Text) );
|
||||
}
|
||||
}
|
||||
}
|
||||
// echo '$maxCol=',$maxCol,'; $maxRow=',$maxRow,'<br />';
|
||||
//
|
||||
foreach($sheet->Styles->StyleRegion as $styleRegion) {
|
||||
$styleAttributes = $styleRegion->attributes();
|
||||
if (($styleAttributes['startRow'] <= $maxRow) &&
|
||||
($styleAttributes['startCol'] <= $maxCol)) {
|
||||
|
||||
$startColumn = PHPExcel_Cell::stringFromColumnIndex((int) $styleAttributes['startCol']);
|
||||
$startRow = $styleAttributes['startRow'] + 1;
|
||||
|
||||
$endColumn = ($styleAttributes['endCol'] > $maxCol) ? $maxCol : (int) $styleAttributes['endCol'];
|
||||
$endColumn = PHPExcel_Cell::stringFromColumnIndex($endColumn);
|
||||
$endRow = ($styleAttributes['endRow'] > $maxRow) ? $maxRow : $styleAttributes['endRow'];
|
||||
$endRow += 1;
|
||||
$cellRange = $startColumn.$startRow.':'.$endColumn.$endRow;
|
||||
// echo $cellRange,'<br />';
|
||||
|
||||
$styleAttributes = $styleRegion->Style->attributes();
|
||||
// var_dump($styleAttributes);
|
||||
// echo '<br />';
|
||||
|
||||
// We still set the number format mask for date/time values, even if _readDataOnly is true
|
||||
if ((!$this->_readDataOnly) ||
|
||||
(PHPExcel_Shared_Date::isDateTimeFormatCode((string) $styleAttributes['Format']))) {
|
||||
$styleArray = array();
|
||||
$styleArray['numberformat']['code'] = (string) $styleAttributes['Format'];
|
||||
// If _readDataOnly is false, we set all formatting information
|
||||
if (!$this->_readDataOnly) {
|
||||
switch($styleAttributes['HAlign']) {
|
||||
case '1' :
|
||||
$styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
|
||||
break;
|
||||
case '2' :
|
||||
$styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_LEFT;
|
||||
break;
|
||||
case '4' :
|
||||
$styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_RIGHT;
|
||||
break;
|
||||
case '8' :
|
||||
$styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_CENTER;
|
||||
break;
|
||||
case '16' :
|
||||
case '64' :
|
||||
$styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS;
|
||||
break;
|
||||
case '32' :
|
||||
$styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY;
|
||||
break;
|
||||
}
|
||||
|
||||
switch($styleAttributes['VAlign']) {
|
||||
case '1' :
|
||||
$styleArray['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_TOP;
|
||||
break;
|
||||
case '2' :
|
||||
$styleArray['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
|
||||
break;
|
||||
case '4' :
|
||||
$styleArray['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_CENTER;
|
||||
break;
|
||||
case '8' :
|
||||
$styleArray['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_JUSTIFY;
|
||||
break;
|
||||
}
|
||||
|
||||
$styleArray['alignment']['wrap'] = ($styleAttributes['WrapText'] == '1') ? True : False;
|
||||
$styleArray['alignment']['shrinkToFit'] = ($styleAttributes['ShrinkToFit'] == '1') ? True : False;
|
||||
$styleArray['alignment']['indent'] = (intval($styleAttributes["Indent"]) > 0) ? $styleAttributes["indent"] : 0;
|
||||
|
||||
$RGB = self::_parseGnumericColour($styleAttributes["Fore"]);
|
||||
$styleArray['font']['color']['rgb'] = $RGB;
|
||||
$RGB = self::_parseGnumericColour($styleAttributes["Back"]);
|
||||
$shade = $styleAttributes["Shade"];
|
||||
if (($RGB != '000000') || ($shade != '0')) {
|
||||
$styleArray['fill']['color']['rgb'] = $styleArray['fill']['startcolor']['rgb'] = $RGB;
|
||||
$RGB2 = self::_parseGnumericColour($styleAttributes["PatternColor"]);
|
||||
$styleArray['fill']['endcolor']['rgb'] = $RGB2;
|
||||
switch($shade) {
|
||||
case '1' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_SOLID;
|
||||
break;
|
||||
case '2' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR;
|
||||
break;
|
||||
case '3' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_GRADIENT_PATH;
|
||||
break;
|
||||
case '4' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN;
|
||||
break;
|
||||
case '5' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY;
|
||||
break;
|
||||
case '6' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID;
|
||||
break;
|
||||
case '7' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL;
|
||||
break;
|
||||
case '8' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS;
|
||||
break;
|
||||
case '9' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKUP;
|
||||
break;
|
||||
case '10' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL;
|
||||
break;
|
||||
case '11' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625;
|
||||
break;
|
||||
case '12' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_GRAY125;
|
||||
break;
|
||||
case '13' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN;
|
||||
break;
|
||||
case '14' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY;
|
||||
break;
|
||||
case '15' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID;
|
||||
break;
|
||||
case '16' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL;
|
||||
break;
|
||||
case '17' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS;
|
||||
break;
|
||||
case '18' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP;
|
||||
break;
|
||||
case '19' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL;
|
||||
break;
|
||||
case '20' :
|
||||
$styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$fontAttributes = $styleRegion->Style->Font->attributes();
|
||||
// var_dump($fontAttributes);
|
||||
// echo '<br />';
|
||||
$styleArray['font']['name'] = (string) $styleRegion->Style->Font;
|
||||
$styleArray['font']['size'] = intval($fontAttributes['Unit']);
|
||||
$styleArray['font']['bold'] = ($fontAttributes['Bold'] == '1') ? True : False;
|
||||
$styleArray['font']['italic'] = ($fontAttributes['Italic'] == '1') ? True : False;
|
||||
$styleArray['font']['strike'] = ($fontAttributes['StrikeThrough'] == '1') ? True : False;
|
||||
switch($fontAttributes['Underline']) {
|
||||
case '1' :
|
||||
$styleArray['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_SINGLE;
|
||||
break;
|
||||
case '2' :
|
||||
$styleArray['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_DOUBLE;
|
||||
break;
|
||||
case '3' :
|
||||
$styleArray['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING;
|
||||
break;
|
||||
case '4' :
|
||||
$styleArray['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING;
|
||||
break;
|
||||
default :
|
||||
$styleArray['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_NONE;
|
||||
break;
|
||||
}
|
||||
switch($fontAttributes['Script']) {
|
||||
case '1' :
|
||||
$styleArray['font']['superScript'] = True;
|
||||
break;
|
||||
case '-1' :
|
||||
$styleArray['font']['subScript'] = True;
|
||||
break;
|
||||
}
|
||||
|
||||
if (isset($styleRegion->Style->StyleBorder)) {
|
||||
if (isset($styleRegion->Style->StyleBorder->Top)) {
|
||||
$styleArray['borders']['top'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Top->attributes());
|
||||
}
|
||||
if (isset($styleRegion->Style->StyleBorder->Bottom)) {
|
||||
$styleArray['borders']['bottom'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Bottom->attributes());
|
||||
}
|
||||
if (isset($styleRegion->Style->StyleBorder->Left)) {
|
||||
$styleArray['borders']['left'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Left->attributes());
|
||||
}
|
||||
if (isset($styleRegion->Style->StyleBorder->Right)) {
|
||||
$styleArray['borders']['right'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Right->attributes());
|
||||
}
|
||||
if ((isset($styleRegion->Style->StyleBorder->Diagonal)) && (isset($styleRegion->Style->StyleBorder->{'Rev-Diagonal'}))) {
|
||||
$styleArray['borders']['diagonal'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Diagonal->attributes());
|
||||
$styleArray['borders']['diagonaldirection'] = PHPExcel_Style_Borders::DIAGONAL_BOTH;
|
||||
} elseif (isset($styleRegion->Style->StyleBorder->Diagonal)) {
|
||||
$styleArray['borders']['diagonal'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Diagonal->attributes());
|
||||
$styleArray['borders']['diagonaldirection'] = PHPExcel_Style_Borders::DIAGONAL_UP;
|
||||
} elseif (isset($styleRegion->Style->StyleBorder->{'Rev-Diagonal'})) {
|
||||
$styleArray['borders']['diagonal'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->{'Rev-Diagonal'}->attributes());
|
||||
$styleArray['borders']['diagonaldirection'] = PHPExcel_Style_Borders::DIAGONAL_DOWN;
|
||||
}
|
||||
}
|
||||
if (isset($styleRegion->Style->HyperLink)) {
|
||||
// TO DO
|
||||
$hyperlink = $styleRegion->Style->HyperLink->attributes();
|
||||
}
|
||||
}
|
||||
// var_dump($styleArray);
|
||||
// echo '<br />';
|
||||
$objPHPExcel->getActiveSheet()->getStyle($cellRange)->applyFromArray($styleArray);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((!$this->_readDataOnly) && (isset($sheet->Cols))) {
|
||||
// Column Widths
|
||||
$columnAttributes = $sheet->Cols->attributes();
|
||||
$defaultWidth = $columnAttributes['DefaultSizePts'] / 5.4;
|
||||
$c = 0;
|
||||
foreach($sheet->Cols->ColInfo as $columnOverride) {
|
||||
$columnAttributes = $columnOverride->attributes();
|
||||
$column = $columnAttributes['No'];
|
||||
$columnWidth = $columnAttributes['Unit'] / 5.4;
|
||||
$hidden = ((isset($columnAttributes['Hidden'])) && ($columnAttributes['Hidden'] == '1')) ? true : false;
|
||||
$columnCount = (isset($columnAttributes['Count'])) ? $columnAttributes['Count'] : 1;
|
||||
while ($c < $column) {
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($c))->setWidth($defaultWidth);
|
||||
++$c;
|
||||
}
|
||||
while (($c < ($column+$columnCount)) && ($c <= $maxCol)) {
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($c))->setWidth($columnWidth);
|
||||
if ($hidden) {
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($c))->setVisible(false);
|
||||
}
|
||||
++$c;
|
||||
}
|
||||
}
|
||||
while ($c <= $maxCol) {
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($c))->setWidth($defaultWidth);
|
||||
++$c;
|
||||
}
|
||||
}
|
||||
|
||||
if ((!$this->_readDataOnly) && (isset($sheet->Rows))) {
|
||||
// Row Heights
|
||||
$rowAttributes = $sheet->Rows->attributes();
|
||||
$defaultHeight = $rowAttributes['DefaultSizePts'];
|
||||
$r = 0;
|
||||
|
||||
foreach($sheet->Rows->RowInfo as $rowOverride) {
|
||||
$rowAttributes = $rowOverride->attributes();
|
||||
$row = $rowAttributes['No'];
|
||||
$rowHeight = $rowAttributes['Unit'];
|
||||
$hidden = ((isset($rowAttributes['Hidden'])) && ($rowAttributes['Hidden'] == '1')) ? true : false;
|
||||
$rowCount = (isset($rowAttributes['Count'])) ? $rowAttributes['Count'] : 1;
|
||||
while ($r < $row) {
|
||||
++$r;
|
||||
$objPHPExcel->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight);
|
||||
}
|
||||
while (($r < ($row+$rowCount)) && ($r < $maxRow)) {
|
||||
++$r;
|
||||
$objPHPExcel->getActiveSheet()->getRowDimension($r)->setRowHeight($rowHeight);
|
||||
if ($hidden) {
|
||||
$objPHPExcel->getActiveSheet()->getRowDimension($r)->setVisible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
while ($r < $maxRow) {
|
||||
++$r;
|
||||
$objPHPExcel->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Merged Cells in this worksheet
|
||||
if (isset($sheet->MergedRegions)) {
|
||||
foreach($sheet->MergedRegions->Merge as $mergeCells) {
|
||||
if (strpos($mergeCells,':') !== FALSE) {
|
||||
$objPHPExcel->getActiveSheet()->mergeCells($mergeCells);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$worksheetID++;
|
||||
}
|
||||
|
||||
// Loop through definedNames (global named ranges)
|
||||
if (isset($gnmXML->Names)) {
|
||||
foreach($gnmXML->Names->Name as $namedRange) {
|
||||
$name = (string) $namedRange->name;
|
||||
$range = (string) $namedRange->value;
|
||||
if (stripos($range, '#REF!') !== false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$range = explode('!',$range);
|
||||
$range[0] = trim($range[0],"'");;
|
||||
if ($worksheet = $objPHPExcel->getSheetByName($range[0])) {
|
||||
$extractedRange = str_replace('$', '', $range[1]);
|
||||
$objPHPExcel->addNamedRange( new PHPExcel_NamedRange($name, $worksheet, $extractedRange) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Return
|
||||
return $objPHPExcel;
|
||||
}
|
||||
|
||||
|
||||
private static function _parseBorderAttributes($borderAttributes)
|
||||
{
|
||||
$styleArray = array();
|
||||
|
||||
if (isset($borderAttributes["Color"])) {
|
||||
$RGB = self::_parseGnumericColour($borderAttributes["Color"]);
|
||||
$styleArray['color']['rgb'] = $RGB;
|
||||
}
|
||||
|
||||
switch ($borderAttributes["Style"]) {
|
||||
case '0' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_NONE;
|
||||
break;
|
||||
case '1' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_THIN;
|
||||
break;
|
||||
case '2' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_MEDIUM;
|
||||
break;
|
||||
case '4' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_DASHED;
|
||||
break;
|
||||
case '5' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_THICK;
|
||||
break;
|
||||
case '6' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_DOUBLE;
|
||||
break;
|
||||
case '7' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_DOTTED;
|
||||
break;
|
||||
case '9' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_DASHDOT;
|
||||
break;
|
||||
case '10' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT;
|
||||
break;
|
||||
case '11' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_DASHDOTDOT;
|
||||
break;
|
||||
case '12' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;
|
||||
break;
|
||||
case '13' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;
|
||||
break;
|
||||
case '3' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_SLANTDASHDOT;
|
||||
break;
|
||||
case '8' :
|
||||
$styleArray['style'] = PHPExcel_Style_Border::BORDER_MEDIUMDASHED;
|
||||
break;
|
||||
}
|
||||
return $styleArray;
|
||||
}
|
||||
|
||||
|
||||
private function _parseRichText($is = '') {
|
||||
$value = new PHPExcel_RichText();
|
||||
|
||||
$value->createText($is);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
private static function _parseGnumericColour($gnmColour) {
|
||||
list($gnmR,$gnmG,$gnmB) = explode(':',$gnmColour);
|
||||
$gnmR = substr(str_pad($gnmR,4,'0',STR_PAD_RIGHT),0,2);
|
||||
$gnmG = substr(str_pad($gnmG,4,'0',STR_PAD_RIGHT),0,2);
|
||||
$gnmB = substr(str_pad($gnmB,4,'0',STR_PAD_RIGHT),0,2);
|
||||
$RGB = $gnmR.$gnmG.$gnmB;
|
||||
// echo 'Excel Colour: ',$RGB,'<br />';
|
||||
return $RGB;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,534 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
/** PHPExcel root directory */
|
||||
if (!defined('PHPEXCEL_ROOT')) {
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
|
||||
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_HTML
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
|
||||
{
|
||||
|
||||
/**
|
||||
* Input encoding
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_inputEncoding = 'ANSI';
|
||||
|
||||
/**
|
||||
* Sheet index to read
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $_sheetIndex = 0;
|
||||
|
||||
/**
|
||||
* Formats
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_formats = array(
|
||||
'h1' => array('font' => array('bold' => true,
|
||||
'size' => 24,
|
||||
),
|
||||
), // Bold, 24pt
|
||||
'h2' => array('font' => array('bold' => true,
|
||||
'size' => 18,
|
||||
),
|
||||
), // Bold, 18pt
|
||||
'h3' => array('font' => array('bold' => true,
|
||||
'size' => 13.5,
|
||||
),
|
||||
), // Bold, 13.5pt
|
||||
'h4' => array('font' => array('bold' => true,
|
||||
'size' => 12,
|
||||
),
|
||||
), // Bold, 12pt
|
||||
'h5' => array('font' => array('bold' => true,
|
||||
'size' => 10,
|
||||
),
|
||||
), // Bold, 10pt
|
||||
'h6' => array('font' => array('bold' => true,
|
||||
'size' => 7.5,
|
||||
),
|
||||
), // Bold, 7.5pt
|
||||
'a' => array('font' => array('underline' => true,
|
||||
'color' => array('argb' => PHPExcel_Style_Color::COLOR_BLUE,
|
||||
),
|
||||
),
|
||||
), // Blue underlined
|
||||
'hr' => array('borders' => array('bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN,
|
||||
'color' => array(\PHPExcel_Style_Color::COLOR_BLACK,
|
||||
),
|
||||
),
|
||||
),
|
||||
), // Bottom border
|
||||
);
|
||||
|
||||
protected $rowspan = array();
|
||||
|
||||
/**
|
||||
* Create a new PHPExcel_Reader_HTML
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the current file is an HTML file
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _isValidFormat()
|
||||
{
|
||||
// Reading 2048 bytes should be enough to validate that the format is HTML
|
||||
$data = fread($this->_fileHandle, 2048);
|
||||
if ((strpos($data, '<') !== FALSE) &&
|
||||
(strlen($data) !== strlen(strip_tags($data)))) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function load($pFilename)
|
||||
{
|
||||
// Create new PHPExcel
|
||||
$objPHPExcel = new PHPExcel();
|
||||
|
||||
// Load into this instance
|
||||
return $this->loadIntoExisting($pFilename, $objPHPExcel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set input encoding
|
||||
*
|
||||
* @param string $pValue Input encoding
|
||||
*/
|
||||
public function setInputEncoding($pValue = 'ANSI')
|
||||
{
|
||||
$this->_inputEncoding = $pValue;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get input encoding
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getInputEncoding()
|
||||
{
|
||||
return $this->_inputEncoding;
|
||||
}
|
||||
|
||||
// Data Array used for testing only, should write to PHPExcel object on completion of tests
|
||||
protected $_dataArray = array();
|
||||
protected $_tableLevel = 0;
|
||||
protected $_nestedColumn = array('A');
|
||||
|
||||
protected function _setTableStartColumn($column)
|
||||
{
|
||||
if ($this->_tableLevel == 0)
|
||||
$column = 'A';
|
||||
++$this->_tableLevel;
|
||||
$this->_nestedColumn[$this->_tableLevel] = $column;
|
||||
|
||||
return $this->_nestedColumn[$this->_tableLevel];
|
||||
}
|
||||
|
||||
protected function _getTableStartColumn()
|
||||
{
|
||||
return $this->_nestedColumn[$this->_tableLevel];
|
||||
}
|
||||
|
||||
protected function _releaseTableStartColumn()
|
||||
{
|
||||
--$this->_tableLevel;
|
||||
|
||||
return array_pop($this->_nestedColumn);
|
||||
}
|
||||
|
||||
protected function _flushCell($sheet, $column, $row, &$cellContent)
|
||||
{
|
||||
if (is_string($cellContent)) {
|
||||
// Simple String content
|
||||
if (trim($cellContent) > '') {
|
||||
// Only actually write it if there's content in the string
|
||||
// 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
|
||||
$sheet->setCellValue($column . $row, $cellContent, true);
|
||||
$this->_dataArray[$row][$column] = $cellContent;
|
||||
}
|
||||
} else {
|
||||
// We have a Rich Text run
|
||||
// TODO
|
||||
$this->_dataArray[$row][$column] = 'RICH TEXT: ' . $cellContent;
|
||||
}
|
||||
$cellContent = (string) '';
|
||||
}
|
||||
|
||||
protected function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent, $format = null)
|
||||
{
|
||||
foreach ($element->childNodes as $child) {
|
||||
if ($child instanceof DOMText) {
|
||||
$domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue));
|
||||
if (is_string($cellContent)) {
|
||||
// simply append the text if the cell content is a plain text string
|
||||
$cellContent .= $domText;
|
||||
} else {
|
||||
// but if we have a rich text run instead, we need to append it correctly
|
||||
// TODO
|
||||
}
|
||||
} elseif ($child instanceof DOMElement) {
|
||||
// echo '<b>DOM ELEMENT: </b>' , strtoupper($child->nodeName) , '<br />';
|
||||
|
||||
$attributeArray = array();
|
||||
foreach ($child->attributes as $attribute) {
|
||||
// echo '<b>ATTRIBUTE: </b>' , $attribute->name , ' => ' , $attribute->value , '<br />';
|
||||
$attributeArray[$attribute->name] = $attribute->value;
|
||||
}
|
||||
|
||||
switch ($child->nodeName) {
|
||||
case 'meta' :
|
||||
foreach ($attributeArray as $attributeName => $attributeValue) {
|
||||
switch ($attributeName) {
|
||||
case 'content':
|
||||
// TODO
|
||||
// Extract character set, so we can convert to UTF-8 if required
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
break;
|
||||
case 'title' :
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
$sheet->setTitle($cellContent);
|
||||
$cellContent = '';
|
||||
break;
|
||||
case 'span' :
|
||||
case 'div' :
|
||||
case 'font' :
|
||||
case 'i' :
|
||||
case 'em' :
|
||||
case 'strong':
|
||||
case 'b' :
|
||||
// echo 'STYLING, SPAN OR DIV<br />';
|
||||
if ($cellContent > '')
|
||||
$cellContent .= ' ';
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
if ($cellContent > '')
|
||||
$cellContent .= ' ';
|
||||
// echo 'END OF STYLING, SPAN OR DIV<br />';
|
||||
break;
|
||||
case 'hr' :
|
||||
$this->_flushCell($sheet, $column, $row, $cellContent);
|
||||
++$row;
|
||||
if (isset($this->_formats[$child->nodeName])) {
|
||||
$sheet->getStyle($column . $row)->applyFromArray($this->_formats[$child->nodeName]);
|
||||
} else {
|
||||
$cellContent = '----------';
|
||||
$this->_flushCell($sheet, $column, $row, $cellContent);
|
||||
}
|
||||
++$row;
|
||||
case 'br' :
|
||||
if ($this->_tableLevel > 0) {
|
||||
// If we're inside a table, replace with a \n
|
||||
$cellContent .= "\n";
|
||||
} else {
|
||||
// Otherwise flush our existing content and move the row cursor on
|
||||
$this->_flushCell($sheet, $column, $row, $cellContent);
|
||||
++$row;
|
||||
}
|
||||
// echo 'HARD LINE BREAK: ' , '<br />';
|
||||
break;
|
||||
case 'a' :
|
||||
// echo 'START OF HYPERLINK: ' , '<br />';
|
||||
foreach ($attributeArray as $attributeName => $attributeValue) {
|
||||
switch ($attributeName) {
|
||||
case 'href':
|
||||
// echo 'Link to ' , $attributeValue , '<br />';
|
||||
$sheet->getCell($column . $row)->getHyperlink()->setUrl($attributeValue);
|
||||
if (isset($this->_formats[$child->nodeName])) {
|
||||
$sheet->getStyle($column . $row)->applyFromArray($this->_formats[$child->nodeName]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
$cellContent .= ' ';
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
// echo 'END OF HYPERLINK:' , '<br />';
|
||||
break;
|
||||
case 'h1' :
|
||||
case 'h2' :
|
||||
case 'h3' :
|
||||
case 'h4' :
|
||||
case 'h5' :
|
||||
case 'h6' :
|
||||
case 'ol' :
|
||||
case 'ul' :
|
||||
case 'p' :
|
||||
if ($this->_tableLevel > 0) {
|
||||
// If we're inside a table, replace with a \n
|
||||
$cellContent .= "\n";
|
||||
// echo 'LIST ENTRY: ' , '<br />';
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
// echo 'END OF LIST ENTRY:' , '<br />';
|
||||
} else {
|
||||
if ($cellContent > '') {
|
||||
$this->_flushCell($sheet, $column, $row, $cellContent);
|
||||
$row++;
|
||||
}
|
||||
// echo 'START OF PARAGRAPH: ' , '<br />';
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
// echo 'END OF PARAGRAPH:' , '<br />';
|
||||
$this->_flushCell($sheet, $column, $row, $cellContent);
|
||||
|
||||
if (isset($this->_formats[$child->nodeName])) {
|
||||
$sheet->getStyle($column . $row)->applyFromArray($this->_formats[$child->nodeName]);
|
||||
}
|
||||
|
||||
$row++;
|
||||
$column = 'A';
|
||||
}
|
||||
break;
|
||||
case 'li' :
|
||||
if ($this->_tableLevel > 0) {
|
||||
// If we're inside a table, replace with a \n
|
||||
$cellContent .= "\n";
|
||||
// echo 'LIST ENTRY: ' , '<br />';
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
// echo 'END OF LIST ENTRY:' , '<br />';
|
||||
} else {
|
||||
if ($cellContent > '') {
|
||||
$this->_flushCell($sheet, $column, $row, $cellContent);
|
||||
}
|
||||
++$row;
|
||||
// echo 'LIST ENTRY: ' , '<br />';
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
// echo 'END OF LIST ENTRY:' , '<br />';
|
||||
$this->_flushCell($sheet, $column, $row, $cellContent);
|
||||
$column = 'A';
|
||||
}
|
||||
break;
|
||||
case 'table' :
|
||||
$this->_flushCell($sheet, $column, $row, $cellContent);
|
||||
$column = $this->_setTableStartColumn($column);
|
||||
// echo 'START OF TABLE LEVEL ' , $this->_tableLevel , '<br />';
|
||||
if ($this->_tableLevel > 1)
|
||||
--$row;
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
// echo 'END OF TABLE LEVEL ' , $this->_tableLevel , '<br />';
|
||||
$column = $this->_releaseTableStartColumn();
|
||||
if ($this->_tableLevel > 1) {
|
||||
++$column;
|
||||
} else {
|
||||
++$row;
|
||||
}
|
||||
break;
|
||||
case 'thead' :
|
||||
case 'tbody' :
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
break;
|
||||
case 'tr' :
|
||||
$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' :
|
||||
case 'td' :
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
if (isset($attributeArray['rowspan']) && isset($attributeArray['colspan'])) {
|
||||
//create merging rowspan and 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);
|
||||
$column = $columnTo;
|
||||
} elseif (isset($attributeArray['rowspan'])) {
|
||||
//create merging 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'])) {
|
||||
//create merging colspan
|
||||
$columnTo = $column;
|
||||
for ($i = 0; $i < $attributeArray['colspan'] - 1; $i++) {
|
||||
++$columnTo;
|
||||
}
|
||||
$sheet->mergeCells($column . $row . ':' . $columnTo . $row);
|
||||
$column = $columnTo;
|
||||
}
|
||||
++$column;
|
||||
break;
|
||||
case 'body' :
|
||||
$row = 1;
|
||||
$column = 'A';
|
||||
$content = '';
|
||||
$this->_tableLevel = 0;
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
break;
|
||||
default:
|
||||
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file into PHPExcel instance
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @param PHPExcel $objPHPExcel
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
|
||||
{
|
||||
// Open file to validate
|
||||
$this->_openFile($pFilename);
|
||||
if (!$this->_isValidFormat()) {
|
||||
fclose($this->_fileHandle);
|
||||
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid HTML file.");
|
||||
}
|
||||
// Close after validating
|
||||
fclose($this->_fileHandle);
|
||||
|
||||
// Create new PHPExcel
|
||||
while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
|
||||
$objPHPExcel->createSheet();
|
||||
}
|
||||
$objPHPExcel->setActiveSheetIndex($this->_sheetIndex);
|
||||
|
||||
// Create a new DOM object
|
||||
$dom = new domDocument;
|
||||
// Reload the HTML file into the DOM object
|
||||
$loaded = $dom->loadHTML($this->securityScanFile($pFilename));
|
||||
if ($loaded === FALSE) {
|
||||
throw new PHPExcel_Reader_Exception('Failed to load ', $pFilename, ' as a DOM Document');
|
||||
}
|
||||
|
||||
// Discard white space
|
||||
$dom->preserveWhiteSpace = false;
|
||||
|
||||
$row = 0;
|
||||
$column = 'A';
|
||||
$content = '';
|
||||
$this->_processDomElement($dom, $objPHPExcel->getActiveSheet(), $row, $column, $content);
|
||||
|
||||
// Return
|
||||
return $objPHPExcel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sheet index
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSheetIndex()
|
||||
{
|
||||
return $this->_sheetIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set sheet index
|
||||
*
|
||||
* @param int $pValue Sheet index
|
||||
* @return PHPExcel_Reader_HTML
|
||||
*/
|
||||
public function setSheetIndex($pValue = 0)
|
||||
{
|
||||
$this->_sheetIndex = $pValue;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
|
||||
*
|
||||
* @param string $xml
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function securityScan($xml)
|
||||
{
|
||||
$pattern = '/\\0?' . implode('\\0?', str_split('<!ENTITY')) . '\\0?/';
|
||||
if (preg_match($pattern, $xml)) {
|
||||
throw new PHPExcel_Reader_Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
|
||||
}
|
||||
return $xml;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_IReadFilter
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
interface PHPExcel_Reader_IReadFilter
|
||||
{
|
||||
/**
|
||||
* Should this cell be read?
|
||||
*
|
||||
* @param $column String column index
|
||||
* @param $row Row index
|
||||
* @param $worksheetName Optional worksheet name
|
||||
* @return boolean
|
||||
*/
|
||||
public function readCell($column, $row, $worksheetName = '');
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_IReader
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
interface PHPExcel_Reader_IReader
|
||||
{
|
||||
/**
|
||||
* Can the current PHPExcel_Reader_IReader read the file?
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return boolean
|
||||
*/
|
||||
public function canRead($pFilename);
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function load($pFilename);
|
||||
}
|
|
@ -0,0 +1,711 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/** PHPExcel root directory */
|
||||
if (!defined('PHPEXCEL_ROOT')) {
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
|
||||
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_OOCalc
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
class PHPExcel_Reader_OOCalc extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
|
||||
{
|
||||
/**
|
||||
* Formats
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_styles = array();
|
||||
|
||||
|
||||
/**
|
||||
* Create a new PHPExcel_Reader_OOCalc
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Can the current PHPExcel_Reader_IReader read the file?
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return boolean
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function canRead($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
$zipClass = PHPExcel_Settings::getZipClass();
|
||||
|
||||
// Check if zip class exists
|
||||
// if (!class_exists($zipClass, FALSE)) {
|
||||
// throw new PHPExcel_Reader_Exception($zipClass . " library is not enabled");
|
||||
// }
|
||||
|
||||
$mimeType = 'UNKNOWN';
|
||||
// Load file
|
||||
$zip = new $zipClass;
|
||||
if ($zip->open($pFilename) === true) {
|
||||
// check if it is an OOXML archive
|
||||
$stat = $zip->statName('mimetype');
|
||||
if ($stat && ($stat['size'] <= 255)) {
|
||||
$mimeType = $zip->getFromName($stat['name']);
|
||||
} elseif($stat = $zip->statName('META-INF/manifest.xml')) {
|
||||
$xml = simplexml_load_string($this->securityScan($zip->getFromName('META-INF/manifest.xml')), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());
|
||||
$namespacesContent = $xml->getNamespaces(true);
|
||||
if (isset($namespacesContent['manifest'])) {
|
||||
$manifest = $xml->children($namespacesContent['manifest']);
|
||||
foreach($manifest as $manifestDataSet) {
|
||||
$manifestAttributes = $manifestDataSet->attributes($namespacesContent['manifest']);
|
||||
if ($manifestAttributes->{'full-path'} == '/') {
|
||||
$mimeType = (string) $manifestAttributes->{'media-type'};
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
|
||||
return ($mimeType === 'application/vnd.oasis.opendocument.spreadsheet');
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function listWorksheetNames($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
$zipClass = PHPExcel_Settings::getZipClass();
|
||||
|
||||
$zip = new $zipClass;
|
||||
if (!$zip->open($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! Error opening file.");
|
||||
}
|
||||
|
||||
$worksheetNames = array();
|
||||
|
||||
$xml = new XMLReader();
|
||||
$res = $xml->xml($this->securityScanFile('zip://'.realpath($pFilename).'#content.xml'), null, PHPExcel_Settings::getLibXmlLoaderOptions());
|
||||
$xml->setParserProperty(2,true);
|
||||
|
||||
// Step into the first level of content of the XML
|
||||
$xml->read();
|
||||
while ($xml->read()) {
|
||||
// Quickly jump through to the office:body node
|
||||
while ($xml->name !== 'office:body') {
|
||||
if ($xml->isEmptyElement)
|
||||
$xml->read();
|
||||
else
|
||||
$xml->next();
|
||||
}
|
||||
// Now read each node until we find our first table:table node
|
||||
while ($xml->read()) {
|
||||
if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
|
||||
// Loop through each table:table node reading the table:name attribute for each worksheet name
|
||||
do {
|
||||
$worksheetNames[] = $xml->getAttribute('table:name');
|
||||
$xml->next();
|
||||
} while ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $worksheetNames;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function listWorksheetInfo($pFilename)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
$worksheetInfo = array();
|
||||
|
||||
$zipClass = PHPExcel_Settings::getZipClass();
|
||||
|
||||
$zip = new $zipClass;
|
||||
if (!$zip->open($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! Error opening file.");
|
||||
}
|
||||
|
||||
$xml = new XMLReader();
|
||||
$res = $xml->xml($this->securityScanFile('zip://'.realpath($pFilename).'#content.xml'), null, PHPExcel_Settings::getLibXmlLoaderOptions());
|
||||
$xml->setParserProperty(2,true);
|
||||
|
||||
// Step into the first level of content of the XML
|
||||
$xml->read();
|
||||
while ($xml->read()) {
|
||||
// Quickly jump through to the office:body node
|
||||
while ($xml->name !== 'office:body') {
|
||||
if ($xml->isEmptyElement)
|
||||
$xml->read();
|
||||
else
|
||||
$xml->next();
|
||||
}
|
||||
// Now read each node until we find our first table:table node
|
||||
while ($xml->read()) {
|
||||
if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
|
||||
$worksheetNames[] = $xml->getAttribute('table:name');
|
||||
|
||||
$tmpInfo = array(
|
||||
'worksheetName' => $xml->getAttribute('table:name'),
|
||||
'lastColumnLetter' => 'A',
|
||||
'lastColumnIndex' => 0,
|
||||
'totalRows' => 0,
|
||||
'totalColumns' => 0,
|
||||
);
|
||||
|
||||
// Loop through each child node of the table:table element reading
|
||||
$currCells = 0;
|
||||
do {
|
||||
$xml->read();
|
||||
if ($xml->name == 'table:table-row' && $xml->nodeType == XMLReader::ELEMENT) {
|
||||
$rowspan = $xml->getAttribute('table:number-rows-repeated');
|
||||
$rowspan = empty($rowspan) ? 1 : $rowspan;
|
||||
$tmpInfo['totalRows'] += $rowspan;
|
||||
$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'],$currCells);
|
||||
$currCells = 0;
|
||||
// Step into the row
|
||||
$xml->read();
|
||||
do {
|
||||
if ($xml->name == 'table:table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
|
||||
if (!$xml->isEmptyElement) {
|
||||
$currCells++;
|
||||
$xml->next();
|
||||
} else {
|
||||
$xml->read();
|
||||
}
|
||||
} elseif ($xml->name == 'table:covered-table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
|
||||
$mergeSize = $xml->getAttribute('table:number-columns-repeated');
|
||||
$currCells += $mergeSize;
|
||||
$xml->read();
|
||||
}
|
||||
} while ($xml->name != 'table:table-row');
|
||||
}
|
||||
} while ($xml->name != 'table:table');
|
||||
|
||||
$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'],$currCells);
|
||||
$tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1;
|
||||
$tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
|
||||
$worksheetInfo[] = $tmpInfo;
|
||||
}
|
||||
}
|
||||
|
||||
// foreach($workbookData->table as $worksheetDataSet) {
|
||||
// $worksheetData = $worksheetDataSet->children($namespacesContent['table']);
|
||||
// $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
|
||||
//
|
||||
// $rowIndex = 0;
|
||||
// foreach ($worksheetData as $key => $rowData) {
|
||||
// switch ($key) {
|
||||
// case 'table-row' :
|
||||
// $rowDataTableAttributes = $rowData->attributes($namespacesContent['table']);
|
||||
// $rowRepeats = (isset($rowDataTableAttributes['number-rows-repeated'])) ?
|
||||
// $rowDataTableAttributes['number-rows-repeated'] : 1;
|
||||
// $columnIndex = 0;
|
||||
//
|
||||
// foreach ($rowData as $key => $cellData) {
|
||||
// $cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
|
||||
// $colRepeats = (isset($cellDataTableAttributes['number-columns-repeated'])) ?
|
||||
// $cellDataTableAttributes['number-columns-repeated'] : 1;
|
||||
// $cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
|
||||
// if (isset($cellDataOfficeAttributes['value-type'])) {
|
||||
// $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex + $colRepeats - 1);
|
||||
// $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex + $rowRepeats);
|
||||
// }
|
||||
// $columnIndex += $colRepeats;
|
||||
// }
|
||||
// $rowIndex += $rowRepeats;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
|
||||
// $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
|
||||
//
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
return $worksheetInfo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function load($pFilename)
|
||||
{
|
||||
// Create new PHPExcel
|
||||
$objPHPExcel = new PHPExcel();
|
||||
|
||||
// Load into this instance
|
||||
return $this->loadIntoExisting($pFilename, $objPHPExcel);
|
||||
}
|
||||
|
||||
|
||||
private static function identifyFixedStyleValue($styleList,&$styleAttributeValue) {
|
||||
$styleAttributeValue = strtolower($styleAttributeValue);
|
||||
foreach($styleList as $style) {
|
||||
if ($styleAttributeValue == strtolower($style)) {
|
||||
$styleAttributeValue = $style;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file into PHPExcel instance
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @param PHPExcel $objPHPExcel
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
|
||||
{
|
||||
// Check if file exists
|
||||
if (!file_exists($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
||||
}
|
||||
|
||||
$timezoneObj = new DateTimeZone('Europe/London');
|
||||
$GMT = new DateTimeZone('UTC');
|
||||
|
||||
$zipClass = PHPExcel_Settings::getZipClass();
|
||||
|
||||
$zip = new $zipClass;
|
||||
if (!$zip->open($pFilename)) {
|
||||
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! Error opening file.");
|
||||
}
|
||||
|
||||
// echo '<h1>Meta Information</h1>';
|
||||
$xml = simplexml_load_string($this->securityScan($zip->getFromName("meta.xml")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());
|
||||
$namespacesMeta = $xml->getNamespaces(true);
|
||||
// echo '<pre>';
|
||||
// print_r($namespacesMeta);
|
||||
// echo '</pre><hr />';
|
||||
|
||||
$docProps = $objPHPExcel->getProperties();
|
||||
$officeProperty = $xml->children($namespacesMeta['office']);
|
||||
foreach($officeProperty as $officePropertyData) {
|
||||
$officePropertyDC = array();
|
||||
if (isset($namespacesMeta['dc'])) {
|
||||
$officePropertyDC = $officePropertyData->children($namespacesMeta['dc']);
|
||||
}
|
||||
foreach($officePropertyDC as $propertyName => $propertyValue) {
|
||||
$propertyValue = (string) $propertyValue;
|
||||
switch ($propertyName) {
|
||||
case 'title' :
|
||||
$docProps->setTitle($propertyValue);
|
||||
break;
|
||||
case 'subject' :
|
||||
$docProps->setSubject($propertyValue);
|
||||
break;
|
||||
case 'creator' :
|
||||
$docProps->setCreator($propertyValue);
|
||||
$docProps->setLastModifiedBy($propertyValue);
|
||||
break;
|
||||
case 'date' :
|
||||
$creationDate = strtotime($propertyValue);
|
||||
$docProps->setCreated($creationDate);
|
||||
$docProps->setModified($creationDate);
|
||||
break;
|
||||
case 'description' :
|
||||
$docProps->setDescription($propertyValue);
|
||||
break;
|
||||
}
|
||||
}
|
||||
$officePropertyMeta = array();
|
||||
if (isset($namespacesMeta['dc'])) {
|
||||
$officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
|
||||
}
|
||||
foreach($officePropertyMeta as $propertyName => $propertyValue) {
|
||||
$propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']);
|
||||
$propertyValue = (string) $propertyValue;
|
||||
switch ($propertyName) {
|
||||
case 'initial-creator' :
|
||||
$docProps->setCreator($propertyValue);
|
||||
break;
|
||||
case 'keyword' :
|
||||
$docProps->setKeywords($propertyValue);
|
||||
break;
|
||||
case 'creation-date' :
|
||||
$creationDate = strtotime($propertyValue);
|
||||
$docProps->setCreated($creationDate);
|
||||
break;
|
||||
case 'user-defined' :
|
||||
$propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_STRING;
|
||||
foreach ($propertyValueAttributes as $key => $value) {
|
||||
if ($key == 'name') {
|
||||
$propertyValueName = (string) $value;
|
||||
} elseif($key == 'value-type') {
|
||||
switch ($value) {
|
||||
case 'date' :
|
||||
$propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue,'date');
|
||||
$propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_DATE;
|
||||
break;
|
||||
case 'boolean' :
|
||||
$propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue,'bool');
|
||||
$propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_BOOLEAN;
|
||||
break;
|
||||
case 'float' :
|
||||
$propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue,'r4');
|
||||
$propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_FLOAT;
|
||||
break;
|
||||
default :
|
||||
$propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_STRING;
|
||||
}
|
||||
}
|
||||
}
|
||||
$docProps->setCustomProperty($propertyValueName,$propertyValue,$propertyValueType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// echo '<h1>Workbook Content</h1>';
|
||||
$xml = simplexml_load_string($this->securityScan($zip->getFromName("content.xml")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());
|
||||
$namespacesContent = $xml->getNamespaces(true);
|
||||
// echo '<pre>';
|
||||
// print_r($namespacesContent);
|
||||
// echo '</pre><hr />';
|
||||
|
||||
$workbook = $xml->children($namespacesContent['office']);
|
||||
foreach($workbook->body->spreadsheet as $workbookData) {
|
||||
$workbookData = $workbookData->children($namespacesContent['table']);
|
||||
$worksheetID = 0;
|
||||
foreach($workbookData->table as $worksheetDataSet) {
|
||||
$worksheetData = $worksheetDataSet->children($namespacesContent['table']);
|
||||
// print_r($worksheetData);
|
||||
// echo '<br />';
|
||||
$worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
|
||||
// print_r($worksheetDataAttributes);
|
||||
// echo '<br />';
|
||||
if ((isset($this->_loadSheetsOnly)) && (isset($worksheetDataAttributes['name'])) &&
|
||||
(!in_array($worksheetDataAttributes['name'], $this->_loadSheetsOnly))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// echo '<h2>Worksheet '.$worksheetDataAttributes['name'].'</h2>';
|
||||
// Create new Worksheet
|
||||
$objPHPExcel->createSheet();
|
||||
$objPHPExcel->setActiveSheetIndex($worksheetID);
|
||||
if (isset($worksheetDataAttributes['name'])) {
|
||||
$worksheetName = (string) $worksheetDataAttributes['name'];
|
||||
// Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
|
||||
// formula cells... during the load, all formulae should be correct, and we're simply
|
||||
// bringing the worksheet name in line with the formula, not the reverse
|
||||
$objPHPExcel->getActiveSheet()->setTitle($worksheetName,false);
|
||||
}
|
||||
|
||||
$rowID = 1;
|
||||
foreach($worksheetData as $key => $rowData) {
|
||||
// echo '<b>'.$key.'</b><br />';
|
||||
switch ($key) {
|
||||
case 'table-header-rows':
|
||||
foreach ($rowData as $key=>$cellData) {
|
||||
$rowData = $cellData;
|
||||
break;
|
||||
}
|
||||
case 'table-row' :
|
||||
$rowDataTableAttributes = $rowData->attributes($namespacesContent['table']);
|
||||
$rowRepeats = (isset($rowDataTableAttributes['number-rows-repeated'])) ?
|
||||
$rowDataTableAttributes['number-rows-repeated'] : 1;
|
||||
$columnID = 'A';
|
||||
foreach($rowData as $key => $cellData) {
|
||||
if ($this->getReadFilter() !== NULL) {
|
||||
if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// echo '<b>'.$columnID.$rowID.'</b><br />';
|
||||
$cellDataText = (isset($namespacesContent['text'])) ?
|
||||
$cellData->children($namespacesContent['text']) :
|
||||
'';
|
||||
$cellDataOffice = $cellData->children($namespacesContent['office']);
|
||||
$cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
|
||||
$cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
|
||||
|
||||
// echo 'Office Attributes: ';
|
||||
// print_r($cellDataOfficeAttributes);
|
||||
// echo '<br />Table Attributes: ';
|
||||
// print_r($cellDataTableAttributes);
|
||||
// echo '<br />Cell Data Text';
|
||||
// print_r($cellDataText);
|
||||
// echo '<br />';
|
||||
//
|
||||
$type = $formatting = $hyperlink = null;
|
||||
$hasCalculatedValue = false;
|
||||
$cellDataFormula = '';
|
||||
if (isset($cellDataTableAttributes['formula'])) {
|
||||
$cellDataFormula = $cellDataTableAttributes['formula'];
|
||||
$hasCalculatedValue = true;
|
||||
}
|
||||
|
||||
if (isset($cellDataOffice->annotation)) {
|
||||
// echo 'Cell has comment<br />';
|
||||
$annotationText = $cellDataOffice->annotation->children($namespacesContent['text']);
|
||||
$textArray = array();
|
||||
foreach($annotationText as $t) {
|
||||
if (isset($t->span)) {
|
||||
foreach($t->span as $text) {
|
||||
$textArray[] = (string)$text;
|
||||
}
|
||||
} else {
|
||||
$textArray[] = (string) $t;
|
||||
}
|
||||
}
|
||||
$text = implode("\n",$textArray);
|
||||
// echo $text,'<br />';
|
||||
$objPHPExcel->getActiveSheet()->getComment( $columnID.$rowID )
|
||||
// ->setAuthor( $author )
|
||||
->setText($this->_parseRichText($text) );
|
||||
}
|
||||
|
||||
if (isset($cellDataText->p)) {
|
||||
// Consolidate if there are multiple p records (maybe with spans as well)
|
||||
$dataArray = array();
|
||||
// Text can have multiple text:p and within those, multiple text:span.
|
||||
// text:p newlines, but text:span does not.
|
||||
// Also, here we assume there is no text data is span fields are specified, since
|
||||
// we have no way of knowing proper positioning anyway.
|
||||
foreach ($cellDataText->p as $pData) {
|
||||
if (isset($pData->span)) {
|
||||
// span sections do not newline, so we just create one large string here
|
||||
$spanSection = "";
|
||||
foreach ($pData->span as $spanData) {
|
||||
$spanSection .= $spanData;
|
||||
}
|
||||
array_push($dataArray, $spanSection);
|
||||
} else {
|
||||
array_push($dataArray, $pData);
|
||||
}
|
||||
}
|
||||
$allCellDataText = implode($dataArray, "\n");
|
||||
|
||||
// echo 'Value Type is '.$cellDataOfficeAttributes['value-type'].'<br />';
|
||||
switch ($cellDataOfficeAttributes['value-type']) {
|
||||
case 'string' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_STRING;
|
||||
$dataValue = $allCellDataText;
|
||||
if (isset($dataValue->a)) {
|
||||
$dataValue = $dataValue->a;
|
||||
$cellXLinkAttributes = $dataValue->attributes($namespacesContent['xlink']);
|
||||
$hyperlink = $cellXLinkAttributes['href'];
|
||||
}
|
||||
break;
|
||||
case 'boolean' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_BOOL;
|
||||
$dataValue = ($allCellDataText == 'TRUE') ? True : False;
|
||||
break;
|
||||
case 'percentage' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
|
||||
$dataValue = (float) $cellDataOfficeAttributes['value'];
|
||||
if (floor($dataValue) == $dataValue) {
|
||||
$dataValue = (integer) $dataValue;
|
||||
}
|
||||
$formatting = PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00;
|
||||
break;
|
||||
case 'currency' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
|
||||
$dataValue = (float) $cellDataOfficeAttributes['value'];
|
||||
if (floor($dataValue) == $dataValue) {
|
||||
$dataValue = (integer) $dataValue;
|
||||
}
|
||||
$formatting = PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE;
|
||||
break;
|
||||
case 'float' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
|
||||
$dataValue = (float) $cellDataOfficeAttributes['value'];
|
||||
if (floor($dataValue) == $dataValue) {
|
||||
if ($dataValue == (integer) $dataValue)
|
||||
$dataValue = (integer) $dataValue;
|
||||
else
|
||||
$dataValue = (float) $dataValue;
|
||||
}
|
||||
break;
|
||||
case 'date' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
|
||||
$dateObj = new DateTime($cellDataOfficeAttributes['date-value'], $GMT);
|
||||
$dateObj->setTimeZone($timezoneObj);
|
||||
list($year,$month,$day,$hour,$minute,$second) = explode(' ',$dateObj->format('Y m d H i s'));
|
||||
$dataValue = PHPExcel_Shared_Date::FormattedPHPToExcel($year,$month,$day,$hour,$minute,$second);
|
||||
if ($dataValue != floor($dataValue)) {
|
||||
$formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15.' '.PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4;
|
||||
} else {
|
||||
$formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15;
|
||||
}
|
||||
break;
|
||||
case 'time' :
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
|
||||
$dataValue = PHPExcel_Shared_Date::PHPToExcel(strtotime('01-01-1970 '.implode(':',sscanf($cellDataOfficeAttributes['time-value'],'PT%dH%dM%dS'))));
|
||||
$formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4;
|
||||
break;
|
||||
}
|
||||
// echo 'Data value is '.$dataValue.'<br />';
|
||||
// if ($hyperlink !== NULL) {
|
||||
// echo 'Hyperlink is '.$hyperlink.'<br />';
|
||||
// }
|
||||
} else {
|
||||
$type = PHPExcel_Cell_DataType::TYPE_NULL;
|
||||
$dataValue = NULL;
|
||||
}
|
||||
|
||||
if ($hasCalculatedValue) {
|
||||
$type = PHPExcel_Cell_DataType::TYPE_FORMULA;
|
||||
// echo 'Formula: ', $cellDataFormula, PHP_EOL;
|
||||
$cellDataFormula = substr($cellDataFormula,strpos($cellDataFormula,':=')+1);
|
||||
$temp = explode('"',$cellDataFormula);
|
||||
$tKey = false;
|
||||
foreach($temp as &$value) {
|
||||
// Only replace in alternate array entries (i.e. non-quoted blocks)
|
||||
if ($tKey = !$tKey) {
|
||||
$value = preg_replace('/\[([^\.]+)\.([^\.]+):\.([^\.]+)\]/Ui','$1!$2:$3',$value); // Cell range reference in another sheet
|
||||
$value = preg_replace('/\[([^\.]+)\.([^\.]+)\]/Ui','$1!$2',$value); // Cell reference in another sheet
|
||||
$value = preg_replace('/\[\.([^\.]+):\.([^\.]+)\]/Ui','$1:$2',$value); // Cell range reference
|
||||
$value = preg_replace('/\[\.([^\.]+)\]/Ui','$1',$value); // Simple cell reference
|
||||
$value = PHPExcel_Calculation::_translateSeparator(';',',',$value,$inBraces);
|
||||
}
|
||||
}
|
||||
unset($value);
|
||||
// Then rebuild the formula string
|
||||
$cellDataFormula = implode('"',$temp);
|
||||
// echo 'Adjusted Formula: ', $cellDataFormula, PHP_EOL;
|
||||
}
|
||||
|
||||
$colRepeats = (isset($cellDataTableAttributes['number-columns-repeated'])) ?
|
||||
$cellDataTableAttributes['number-columns-repeated'] : 1;
|
||||
if ($type !== NULL) {
|
||||
for ($i = 0; $i < $colRepeats; ++$i) {
|
||||
if ($i > 0) {
|
||||
++$columnID;
|
||||
}
|
||||
if ($type !== PHPExcel_Cell_DataType::TYPE_NULL) {
|
||||
for ($rowAdjust = 0; $rowAdjust < $rowRepeats; ++$rowAdjust) {
|
||||
$rID = $rowID + $rowAdjust;
|
||||
$objPHPExcel->getActiveSheet()->getCell($columnID.$rID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $dataValue),$type);
|
||||
if ($hasCalculatedValue) {
|
||||
// echo 'Forumla result is '.$dataValue.'<br />';
|
||||
$objPHPExcel->getActiveSheet()->getCell($columnID.$rID)->setCalculatedValue($dataValue);
|
||||
}
|
||||
if ($formatting !== NULL) {
|
||||
$objPHPExcel->getActiveSheet()->getStyle($columnID.$rID)->getNumberFormat()->setFormatCode($formatting);
|
||||
} else {
|
||||
$objPHPExcel->getActiveSheet()->getStyle($columnID.$rID)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
|
||||
}
|
||||
if ($hyperlink !== NULL) {
|
||||
$objPHPExcel->getActiveSheet()->getCell($columnID.$rID)->getHyperlink()->setUrl($hyperlink);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Merged cells
|
||||
if ((isset($cellDataTableAttributes['number-columns-spanned'])) || (isset($cellDataTableAttributes['number-rows-spanned']))) {
|
||||
if (($type !== PHPExcel_Cell_DataType::TYPE_NULL) || (!$this->_readDataOnly)) {
|
||||
$columnTo = $columnID;
|
||||
if (isset($cellDataTableAttributes['number-columns-spanned'])) {
|
||||
$columnTo = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($columnID) + $cellDataTableAttributes['number-columns-spanned'] -2);
|
||||
}
|
||||
$rowTo = $rowID;
|
||||
if (isset($cellDataTableAttributes['number-rows-spanned'])) {
|
||||
$rowTo = $rowTo + $cellDataTableAttributes['number-rows-spanned'] - 1;
|
||||
}
|
||||
$cellRange = $columnID.$rowID.':'.$columnTo.$rowTo;
|
||||
$objPHPExcel->getActiveSheet()->mergeCells($cellRange);
|
||||
}
|
||||
}
|
||||
|
||||
++$columnID;
|
||||
}
|
||||
$rowID += $rowRepeats;
|
||||
break;
|
||||
}
|
||||
}
|
||||
++$worksheetID;
|
||||
}
|
||||
}
|
||||
|
||||
// Return
|
||||
return $objPHPExcel;
|
||||
}
|
||||
|
||||
|
||||
private function _parseRichText($is = '') {
|
||||
$value = new PHPExcel_RichText();
|
||||
|
||||
$value->createText($is);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,450 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPExcel
|
||||
*
|
||||
* Copyright (c) 2006 - 2014 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_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
* @version ##VERSION##, ##DATE##
|
||||
*/
|
||||
|
||||
|
||||
/** PHPExcel root directory */
|
||||
if (!defined('PHPEXCEL_ROOT')) {
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
|
||||
require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* PHPExcel_Reader_SYLK
|
||||
*
|
||||
* @category PHPExcel
|
||||
* @package PHPExcel_Reader
|
||||
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
|
||||
*/
|
||||
class PHPExcel_Reader_SYLK extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
|
||||
{
|
||||
/**
|
||||
* Input encoding
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_inputEncoding = 'ANSI';
|
||||
|
||||
/**
|
||||
* Sheet index to read
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $_sheetIndex = 0;
|
||||
|
||||
/**
|
||||
* Formats
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $_formats = array();
|
||||
|
||||
/**
|
||||
* Format Count
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $_format = 0;
|
||||
|
||||
/**
|
||||
* Create a new PHPExcel_Reader_SYLK
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the current file is a SYLK file
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _isValidFormat()
|
||||
{
|
||||
// Read sample data (first 2 KB will do)
|
||||
$data = fread($this->_fileHandle, 2048);
|
||||
|
||||
// Count delimiters in file
|
||||
$delimiterCount = substr_count($data, ';');
|
||||
if ($delimiterCount < 1) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Analyze first line looking for ID; signature
|
||||
$lines = explode("\n", $data);
|
||||
if (substr($lines[0],0,4) != 'ID;P') {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set input encoding
|
||||
*
|
||||
* @param string $pValue Input encoding
|
||||
*/
|
||||
public function setInputEncoding($pValue = 'ANSI')
|
||||
{
|
||||
$this->_inputEncoding = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get input encoding
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getInputEncoding()
|
||||
{
|
||||
return $this->_inputEncoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function listWorksheetInfo($pFilename)
|
||||
{
|
||||
// Open file
|
||||
$this->_openFile($pFilename);
|
||||
if (!$this->_isValidFormat()) {
|
||||
fclose ($this->_fileHandle);
|
||||
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
|
||||
}
|
||||
$fileHandle = $this->_fileHandle;
|
||||
rewind($fileHandle);
|
||||
|
||||
$worksheetInfo = array();
|
||||
$worksheetInfo[0]['worksheetName'] = 'Worksheet';
|
||||
$worksheetInfo[0]['lastColumnLetter'] = 'A';
|
||||
$worksheetInfo[0]['lastColumnIndex'] = 0;
|
||||
$worksheetInfo[0]['totalRows'] = 0;
|
||||
$worksheetInfo[0]['totalColumns'] = 0;
|
||||
|
||||
// Loop through file
|
||||
$rowData = array();
|
||||
|
||||
// loop through one row (line) at a time in the file
|
||||
$rowIndex = 0;
|
||||
while (($rowData = fgets($fileHandle)) !== FALSE) {
|
||||
$columnIndex = 0;
|
||||
|
||||
// convert SYLK encoded $rowData to UTF-8
|
||||
$rowData = PHPExcel_Shared_String::SYLKtoUTF8($rowData);
|
||||
|
||||
// explode each row at semicolons while taking into account that literal semicolon (;)
|
||||
// is escaped like this (;;)
|
||||
$rowData = explode("\t",str_replace('¤',';',str_replace(';',"\t",str_replace(';;','¤',rtrim($rowData)))));
|
||||
|
||||
$dataType = array_shift($rowData);
|
||||
if ($dataType == 'C') {
|
||||
// Read cell value data
|
||||
foreach($rowData as $rowDatum) {
|
||||
switch($rowDatum{0}) {
|
||||
case 'C' :
|
||||
case 'X' :
|
||||
$columnIndex = substr($rowDatum,1) - 1;
|
||||
break;
|
||||
case 'R' :
|
||||
case 'Y' :
|
||||
$rowIndex = substr($rowDatum,1);
|
||||
break;
|
||||
}
|
||||
|
||||
$worksheetInfo[0]['totalRows'] = max($worksheetInfo[0]['totalRows'], $rowIndex);
|
||||
$worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], $columnIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
|
||||
$worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
|
||||
|
||||
// Close file
|
||||
fclose($fileHandle);
|
||||
|
||||
return $worksheetInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function load($pFilename)
|
||||
{
|
||||
// Create new PHPExcel
|
||||
$objPHPExcel = new PHPExcel();
|
||||
|
||||
// Load into this instance
|
||||
return $this->loadIntoExisting($pFilename, $objPHPExcel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads PHPExcel from file into PHPExcel instance
|
||||
*
|
||||
* @param string $pFilename
|
||||
* @param PHPExcel $objPHPExcel
|
||||
* @return PHPExcel
|
||||
* @throws PHPExcel_Reader_Exception
|
||||
*/
|
||||
public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
|
||||
{
|
||||
// Open file
|
||||
$this->_openFile($pFilename);
|
||||
if (!$this->_isValidFormat()) {
|
||||
fclose ($this->_fileHandle);
|
||||
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
|
||||
}
|
||||
$fileHandle = $this->_fileHandle;
|
||||
rewind($fileHandle);
|
||||
|
||||
// Create new PHPExcel
|
||||
while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
|
||||
$objPHPExcel->createSheet();
|
||||
}
|
||||
$objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
|
||||
|
||||
$fromFormats = array('\-', '\ ');
|
||||
$toFormats = array('-', ' ');
|
||||
|
||||
// Loop through file
|
||||
$rowData = array();
|
||||
$column = $row = '';
|
||||
|
||||
// loop through one row (line) at a time in the file
|
||||
while (($rowData = fgets($fileHandle)) !== FALSE) {
|
||||
|
||||
// convert SYLK encoded $rowData to UTF-8
|
||||
$rowData = PHPExcel_Shared_String::SYLKtoUTF8($rowData);
|
||||
|
||||
// explode each row at semicolons while taking into account that literal semicolon (;)
|
||||
// is escaped like this (;;)
|
||||
$rowData = explode("\t",str_replace('¤',';',str_replace(';',"\t",str_replace(';;','¤',rtrim($rowData)))));
|
||||
|
||||
$dataType = array_shift($rowData);
|
||||
// Read shared styles
|
||||
if ($dataType == 'P') {
|
||||
$formatArray = array();
|
||||
foreach($rowData as $rowDatum) {
|
||||
switch($rowDatum{0}) {
|
||||
case 'P' : $formatArray['numberformat']['code'] = str_replace($fromFormats,$toFormats,substr($rowDatum,1));
|
||||
break;
|
||||
case 'E' :
|
||||
case 'F' : $formatArray['font']['name'] = substr($rowDatum,1);
|
||||
break;
|
||||
case 'L' : $formatArray['font']['size'] = substr($rowDatum,1);
|
||||
break;
|
||||
case 'S' : $styleSettings = substr($rowDatum,1);
|
||||
for ($i=0;$i<strlen($styleSettings);++$i) {
|
||||
switch ($styleSettings{$i}) {
|
||||
case 'I' : $formatArray['font']['italic'] = true;
|
||||
break;
|
||||
case 'D' : $formatArray['font']['bold'] = true;
|
||||
break;
|
||||
case 'T' : $formatArray['borders']['top']['style'] = PHPExcel_Style_Border::BORDER_THIN;
|
||||
break;
|
||||
case 'B' : $formatArray['borders']['bottom']['style'] = PHPExcel_Style_Border::BORDER_THIN;
|
||||
break;
|
||||
case 'L' : $formatArray['borders']['left']['style'] = PHPExcel_Style_Border::BORDER_THIN;
|
||||
break;
|
||||
case 'R' : $formatArray['borders']['right']['style'] = PHPExcel_Style_Border::BORDER_THIN;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->_formats['P'.$this->_format++] = $formatArray;
|
||||
// Read cell value data
|
||||
} elseif ($dataType == 'C') {
|
||||
$hasCalculatedValue = false;
|
||||
$cellData = $cellDataFormula = '';
|
||||
foreach($rowData as $rowDatum) {
|
||||
switch($rowDatum{0}) {
|
||||
case 'C' :
|
||||
case 'X' : $column = substr($rowDatum,1);
|
||||
break;
|
||||
case 'R' :
|
||||
case 'Y' : $row = substr($rowDatum,1);
|
||||
break;
|
||||
case 'K' : $cellData = substr($rowDatum,1);
|
||||
break;
|
||||
case 'E' : $cellDataFormula = '='.substr($rowDatum,1);
|
||||
// Convert R1C1 style references to A1 style references (but only when not quoted)
|
||||
$temp = explode('"',$cellDataFormula);
|
||||
$key = false;
|
||||
foreach($temp as &$value) {
|
||||
// Only count/replace in alternate array entries
|
||||
if ($key = !$key) {
|
||||
preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
|
||||
// Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
|
||||
// through the formula from left to right. Reversing means that we work right to left.through
|
||||
// the formula
|
||||
$cellReferences = array_reverse($cellReferences);
|
||||
// Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
|
||||
// then modify the formula to use that new reference
|
||||
foreach($cellReferences as $cellReference) {
|
||||
$rowReference = $cellReference[2][0];
|
||||
// Empty R reference is the current row
|
||||
if ($rowReference == '') $rowReference = $row;
|
||||
// Bracketed R references are relative to the current row
|
||||
if ($rowReference{0} == '[') $rowReference = $row + trim($rowReference,'[]');
|
||||
$columnReference = $cellReference[4][0];
|
||||
// Empty C reference is the current column
|
||||
if ($columnReference == '') $columnReference = $column;
|
||||
// Bracketed C references are relative to the current column
|
||||
if ($columnReference{0} == '[') $columnReference = $column + trim($columnReference,'[]');
|
||||
$A1CellReference = PHPExcel_Cell::stringFromColumnIndex($columnReference-1).$rowReference;
|
||||
|
||||
$value = substr_replace($value,$A1CellReference,$cellReference[0][1],strlen($cellReference[0][0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($value);
|
||||
// Then rebuild the formula string
|
||||
$cellDataFormula = implode('"',$temp);
|
||||
$hasCalculatedValue = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
|
||||
$cellData = PHPExcel_Calculation::_unwrapResult($cellData);
|
||||
|
||||
// Set cell value
|
||||
$objPHPExcel->getActiveSheet()->getCell($columnLetter.$row)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData);
|
||||
if ($hasCalculatedValue) {
|
||||
$cellData = PHPExcel_Calculation::_unwrapResult($cellData);
|
||||
$objPHPExcel->getActiveSheet()->getCell($columnLetter.$row)->setCalculatedValue($cellData);
|
||||
}
|
||||
// Read cell formatting
|
||||
} elseif ($dataType == 'F') {
|
||||
$formatStyle = $columnWidth = $styleSettings = '';
|
||||
$styleData = array();
|
||||
foreach($rowData as $rowDatum) {
|
||||
switch($rowDatum{0}) {
|
||||
case 'C' :
|
||||
case 'X' : $column = substr($rowDatum,1);
|
||||
break;
|
||||
case 'R' :
|
||||
case 'Y' : $row = substr($rowDatum,1);
|
||||
break;
|
||||
case 'P' : $formatStyle = $rowDatum;
|
||||
break;
|
||||
case 'W' : list($startCol,$endCol,$columnWidth) = explode(' ',substr($rowDatum,1));
|
||||
break;
|
||||
case 'S' : $styleSettings = substr($rowDatum,1);
|
||||
for ($i=0;$i<strlen($styleSettings);++$i) {
|
||||
switch ($styleSettings{$i}) {
|
||||
case 'I' : $styleData['font']['italic'] = true;
|
||||
break;
|
||||
case 'D' : $styleData['font']['bold'] = true;
|
||||
break;
|
||||
case 'T' : $styleData['borders']['top']['style'] = PHPExcel_Style_Border::BORDER_THIN;
|
||||
break;
|
||||
case 'B' : $styleData['borders']['bottom']['style'] = PHPExcel_Style_Border::BORDER_THIN;
|
||||
break;
|
||||
case 'L' : $styleData['borders']['left']['style'] = PHPExcel_Style_Border::BORDER_THIN;
|
||||
break;
|
||||
case 'R' : $styleData['borders']['right']['style'] = PHPExcel_Style_Border::BORDER_THIN;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (($formatStyle > '') && ($column > '') && ($row > '')) {
|
||||
$columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
|
||||
if (isset($this->_formats[$formatStyle])) {
|
||||
$objPHPExcel->getActiveSheet()->getStyle($columnLetter.$row)->applyFromArray($this->_formats[$formatStyle]);
|
||||
}
|
||||
}
|
||||
if ((!empty($styleData)) && ($column > '') && ($row > '')) {
|
||||
$columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
|
||||
$objPHPExcel->getActiveSheet()->getStyle($columnLetter.$row)->applyFromArray($styleData);
|
||||
}
|
||||
if ($columnWidth > '') {
|
||||
if ($startCol == $endCol) {
|
||||
$startCol = PHPExcel_Cell::stringFromColumnIndex($startCol-1);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
|
||||
} else {
|
||||
$startCol = PHPExcel_Cell::stringFromColumnIndex($startCol-1);
|
||||
$endCol = PHPExcel_Cell::stringFromColumnIndex($endCol-1);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
|
||||
do {
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension(++$startCol)->setWidth($columnWidth);
|
||||
} while ($startCol != $endCol);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach($rowData as $rowDatum) {
|
||||
switch($rowDatum{0}) {
|
||||
case 'C' :
|
||||
case 'X' : $column = substr($rowDatum,1);
|
||||
break;
|
||||
case 'R' :
|
||||
case 'Y' : $row = substr($rowDatum,1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close file
|
||||
fclose($fileHandle);
|
||||
|
||||
// Return
|
||||
return $objPHPExcel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sheet index
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSheetIndex() {
|
||||
return $this->_sheetIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set sheet index
|
||||
*
|
||||
* @param int $pValue Sheet index
|
||||
* @return PHPExcel_Reader_SYLK
|
||||
*/
|
||||
public function setSheetIndex($pValue = 0) {
|
||||
$this->_sheetIndex = $pValue;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue