Commit 0909e2a50cd96bf4820edea64eefe8fd7d207e9b
1 parent
c68bbb32
Add PHPExcelReader (http://sourceforge.net/projects/phpexcelreader/),
LGPL licensed. git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@4474 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
6 changed files
with
1175 additions
and
0 deletions
thirdparty/excelreader/Excel/oleread.inc
0 → 100644
| 1 | +<?php | |
| 2 | +define('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c); | |
| 3 | +define('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c); | |
| 4 | +define('ROOT_START_BLOCK_POS', 0x30); | |
| 5 | +define('BIG_BLOCK_SIZE', 0x200); | |
| 6 | +define('SMALL_BLOCK_SIZE', 0x40); | |
| 7 | +define('EXTENSION_BLOCK_POS', 0x44); | |
| 8 | +define('NUM_EXTENSION_BLOCK_POS', 0x48); | |
| 9 | +define('PROPERTY_STORAGE_BLOCK_SIZE', 0x80); | |
| 10 | +define('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c); | |
| 11 | +define('SMALL_BLOCK_THRESHOLD', 0x1000); | |
| 12 | +// property storage offsets | |
| 13 | +define('SIZE_OF_NAME_POS', 0x40); | |
| 14 | +define('TYPE_POS', 0x42); | |
| 15 | +define('START_BLOCK_POS', 0x74); | |
| 16 | +define('SIZE_POS', 0x78); | |
| 17 | +define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1)); | |
| 18 | + | |
| 19 | +//echo 'ROOT_START_BLOCK_POS = '.ROOT_START_BLOCK_POS."\n"; | |
| 20 | + | |
| 21 | +//echo bin2hex($data[ROOT_START_BLOCK_POS])."\n"; | |
| 22 | +//echo "a="; | |
| 23 | +//echo $data[ROOT_START_BLOCK_POS]; | |
| 24 | +//function log | |
| 25 | + | |
| 26 | +function GetInt4d($data, $pos) { | |
| 27 | + return ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24); | |
| 28 | +} | |
| 29 | + | |
| 30 | + | |
| 31 | +class OLERead { | |
| 32 | + var $data = ''; | |
| 33 | + | |
| 34 | + | |
| 35 | + function OLERead(){ | |
| 36 | + | |
| 37 | + | |
| 38 | + } | |
| 39 | + | |
| 40 | + function read($sFileName){ | |
| 41 | + | |
| 42 | + // check if file exist and is readable (Darko Miljanovic) | |
| 43 | + if(!is_readable($sFileName)) { | |
| 44 | + $this->error = 1; | |
| 45 | + return false; | |
| 46 | + } | |
| 47 | + | |
| 48 | + $this->data = @file_get_contents($sFileName); | |
| 49 | + if (!$this->data) { | |
| 50 | + $this->error = 1; | |
| 51 | + return false; | |
| 52 | + } | |
| 53 | + //echo IDENTIFIER_OLE; | |
| 54 | + //echo 'start'; | |
| 55 | + if (substr($this->data, 0, 8) != IDENTIFIER_OLE) { | |
| 56 | + $this->error = 1; | |
| 57 | + return false; | |
| 58 | + } | |
| 59 | + $this->numBigBlockDepotBlocks = GetInt4d($this->data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS); | |
| 60 | + $this->sbdStartBlock = GetInt4d($this->data, SMALL_BLOCK_DEPOT_BLOCK_POS); | |
| 61 | + $this->rootStartBlock = GetInt4d($this->data, ROOT_START_BLOCK_POS); | |
| 62 | + $this->extensionBlock = GetInt4d($this->data, EXTENSION_BLOCK_POS); | |
| 63 | + $this->numExtensionBlocks = GetInt4d($this->data, NUM_EXTENSION_BLOCK_POS); | |
| 64 | + | |
| 65 | + /* | |
| 66 | + echo $this->numBigBlockDepotBlocks." "; | |
| 67 | + echo $this->sbdStartBlock." "; | |
| 68 | + echo $this->rootStartBlock." "; | |
| 69 | + echo $this->extensionBlock." "; | |
| 70 | + echo $this->numExtensionBlocks." "; | |
| 71 | + */ | |
| 72 | + //echo "sbdStartBlock = $this->sbdStartBlock\n"; | |
| 73 | + $bigBlockDepotBlocks = array(); | |
| 74 | + $pos = BIG_BLOCK_DEPOT_BLOCKS_POS; | |
| 75 | + // echo "pos = $pos"; | |
| 76 | + $bbdBlocks = $this->numBigBlockDepotBlocks; | |
| 77 | + | |
| 78 | + if ($this->numExtensionBlocks != 0) { | |
| 79 | + $bbdBlocks = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4; | |
| 80 | + } | |
| 81 | + | |
| 82 | + for ($i = 0; $i < $bbdBlocks; $i++) { | |
| 83 | + $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos); | |
| 84 | + $pos += 4; | |
| 85 | + } | |
| 86 | + | |
| 87 | + | |
| 88 | + for ($j = 0; $j < $this->numExtensionBlocks; $j++) { | |
| 89 | + $pos = ($this->extensionBlock + 1) * BIG_BLOCK_SIZE; | |
| 90 | + $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, BIG_BLOCK_SIZE / 4 - 1); | |
| 91 | + | |
| 92 | + for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i++) { | |
| 93 | + $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos); | |
| 94 | + $pos += 4; | |
| 95 | + } | |
| 96 | + | |
| 97 | + $bbdBlocks += $blocksToRead; | |
| 98 | + if ($bbdBlocks < $this->numBigBlockDepotBlocks) { | |
| 99 | + $this->extensionBlock = GetInt4d($this->data, $pos); | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + // var_dump($bigBlockDepotBlocks); | |
| 104 | + | |
| 105 | + // readBigBlockDepot | |
| 106 | + $pos = 0; | |
| 107 | + $index = 0; | |
| 108 | + $this->bigBlockChain = array(); | |
| 109 | + | |
| 110 | + for ($i = 0; $i < $this->numBigBlockDepotBlocks; $i++) { | |
| 111 | + $pos = ($bigBlockDepotBlocks[$i] + 1) * BIG_BLOCK_SIZE; | |
| 112 | + //echo "pos = $pos"; | |
| 113 | + for ($j = 0 ; $j < BIG_BLOCK_SIZE / 4; $j++) { | |
| 114 | + $this->bigBlockChain[$index] = GetInt4d($this->data, $pos); | |
| 115 | + $pos += 4 ; | |
| 116 | + $index++; | |
| 117 | + } | |
| 118 | + } | |
| 119 | + | |
| 120 | + //var_dump($this->bigBlockChain); | |
| 121 | + //echo '=====2'; | |
| 122 | + // readSmallBlockDepot(); | |
| 123 | + $pos = 0; | |
| 124 | + $index = 0; | |
| 125 | + $sbdBlock = $this->sbdStartBlock; | |
| 126 | + $this->smallBlockChain = array(); | |
| 127 | + | |
| 128 | + while ($sbdBlock != -2) { | |
| 129 | + | |
| 130 | + $pos = ($sbdBlock + 1) * BIG_BLOCK_SIZE; | |
| 131 | + | |
| 132 | + for ($j = 0; $j < BIG_BLOCK_SIZE / 4; $j++) { | |
| 133 | + $this->smallBlockChain[$index] = GetInt4d($this->data, $pos); | |
| 134 | + $pos += 4; | |
| 135 | + $index++; | |
| 136 | + } | |
| 137 | + | |
| 138 | + $sbdBlock = $this->bigBlockChain[$sbdBlock]; | |
| 139 | + } | |
| 140 | + | |
| 141 | + | |
| 142 | + // readData(rootStartBlock) | |
| 143 | + $block = $this->rootStartBlock; | |
| 144 | + $pos = 0; | |
| 145 | + $this->entry = $this->__readData($block); | |
| 146 | + | |
| 147 | + /* | |
| 148 | + while ($block != -2) { | |
| 149 | + $pos = ($block + 1) * BIG_BLOCK_SIZE; | |
| 150 | + $this->entry = $this->entry.substr($this->data, $pos, BIG_BLOCK_SIZE); | |
| 151 | + $block = $this->bigBlockChain[$block]; | |
| 152 | + } | |
| 153 | + */ | |
| 154 | + //echo '==='.$this->entry."==="; | |
| 155 | + $this->__readPropertySets(); | |
| 156 | + | |
| 157 | + } | |
| 158 | + | |
| 159 | + function __readData($bl) { | |
| 160 | + $block = $bl; | |
| 161 | + $pos = 0; | |
| 162 | + $data = ''; | |
| 163 | + | |
| 164 | + while ($block != -2) { | |
| 165 | + $pos = ($block + 1) * BIG_BLOCK_SIZE; | |
| 166 | + $data = $data.substr($this->data, $pos, BIG_BLOCK_SIZE); | |
| 167 | + //echo "pos = $pos data=$data\n"; | |
| 168 | + $block = $this->bigBlockChain[$block]; | |
| 169 | + } | |
| 170 | + return $data; | |
| 171 | + } | |
| 172 | + | |
| 173 | + function __readPropertySets(){ | |
| 174 | + $offset = 0; | |
| 175 | + //var_dump($this->entry); | |
| 176 | + while ($offset < strlen($this->entry)) { | |
| 177 | + $d = substr($this->entry, $offset, PROPERTY_STORAGE_BLOCK_SIZE); | |
| 178 | + | |
| 179 | + $nameSize = ord($d[SIZE_OF_NAME_POS]) | (ord($d[SIZE_OF_NAME_POS+1]) << 8); | |
| 180 | + | |
| 181 | + $type = ord($d[TYPE_POS]); | |
| 182 | + //$maxBlock = strlen($d) / BIG_BLOCK_SIZE - 1; | |
| 183 | + | |
| 184 | + $startBlock = GetInt4d($d, START_BLOCK_POS); | |
| 185 | + $size = GetInt4d($d, SIZE_POS); | |
| 186 | + | |
| 187 | + $name = ''; | |
| 188 | + for ($i = 0; $i < $nameSize ; $i++) { | |
| 189 | + $name .= $d[$i]; | |
| 190 | + } | |
| 191 | + | |
| 192 | + $name = str_replace("\x00", "", $name); | |
| 193 | + | |
| 194 | + $this->props[] = array ( | |
| 195 | + 'name' => $name, | |
| 196 | + 'type' => $type, | |
| 197 | + 'startBlock' => $startBlock, | |
| 198 | + 'size' => $size); | |
| 199 | + | |
| 200 | + if (($name == "Workbook") || ($name == "Book")) { | |
| 201 | + $this->wrkbook = count($this->props) - 1; | |
| 202 | + } | |
| 203 | + | |
| 204 | + if ($name == "Root Entry") { | |
| 205 | + $this->rootentry = count($this->props) - 1; | |
| 206 | + } | |
| 207 | + | |
| 208 | + //echo "name ==$name=\n"; | |
| 209 | + | |
| 210 | + | |
| 211 | + $offset += PROPERTY_STORAGE_BLOCK_SIZE; | |
| 212 | + } | |
| 213 | + | |
| 214 | + } | |
| 215 | + | |
| 216 | + | |
| 217 | + function getWorkBook(){ | |
| 218 | + if ($this->props[$this->wrkbook]['size'] < SMALL_BLOCK_THRESHOLD){ | |
| 219 | +// getSmallBlockStream(PropertyStorage ps) | |
| 220 | + | |
| 221 | + $rootdata = $this->__readData($this->props[$this->rootentry]['startBlock']); | |
| 222 | + | |
| 223 | + $streamData = ''; | |
| 224 | + $block = $this->props[$this->wrkbook]['startBlock']; | |
| 225 | + //$count = 0; | |
| 226 | + $pos = 0; | |
| 227 | + while ($block != -2) { | |
| 228 | + $pos = $block * SMALL_BLOCK_SIZE; | |
| 229 | + $streamData .= substr($rootdata, $pos, SMALL_BLOCK_SIZE); | |
| 230 | + | |
| 231 | + $block = $this->smallBlockChain[$block]; | |
| 232 | + } | |
| 233 | + | |
| 234 | + return $streamData; | |
| 235 | + | |
| 236 | + | |
| 237 | + }else{ | |
| 238 | + | |
| 239 | + $numBlocks = $this->props[$this->wrkbook]['size'] / BIG_BLOCK_SIZE; | |
| 240 | + if ($this->props[$this->wrkbook]['size'] % BIG_BLOCK_SIZE != 0) { | |
| 241 | + $numBlocks++; | |
| 242 | + } | |
| 243 | + | |
| 244 | + if ($numBlocks == 0) return ''; | |
| 245 | + | |
| 246 | + //echo "numBlocks = $numBlocks\n"; | |
| 247 | + //byte[] streamData = new byte[numBlocks * BIG_BLOCK_SIZE]; | |
| 248 | + //print_r($this->wrkbook); | |
| 249 | + $streamData = ''; | |
| 250 | + $block = $this->props[$this->wrkbook]['startBlock']; | |
| 251 | + //$count = 0; | |
| 252 | + $pos = 0; | |
| 253 | + //echo "block = $block"; | |
| 254 | + while ($block != -2) { | |
| 255 | + $pos = ($block + 1) * BIG_BLOCK_SIZE; | |
| 256 | + $streamData .= substr($this->data, $pos, BIG_BLOCK_SIZE); | |
| 257 | + $block = $this->bigBlockChain[$block]; | |
| 258 | + } | |
| 259 | + //echo 'stream'.$streamData; | |
| 260 | + return $streamData; | |
| 261 | + } | |
| 262 | + } | |
| 263 | + | |
| 264 | +} | |
| 265 | +?> | |
| 0 | 266 | \ No newline at end of file | ... | ... |
thirdparty/excelreader/Excel/reader.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | +//require_once 'PEAR.php'; | |
| 4 | +require_once 'oleread.inc'; | |
| 5 | + | |
| 6 | +//define('Spreadsheet_Excel_Reader_HAVE_ICONV', function_exists('iconv')); | |
| 7 | +//define('Spreadsheet_Excel_Reader_HAVE_MB', function_exists('mb_convert_encoding')); | |
| 8 | + | |
| 9 | +define('Spreadsheet_Excel_Reader_BIFF8', 0x600); | |
| 10 | +define('Spreadsheet_Excel_Reader_BIFF7', 0x500); | |
| 11 | +define('Spreadsheet_Excel_Reader_WorkbookGlobals', 0x5); | |
| 12 | +define('Spreadsheet_Excel_Reader_Worksheet', 0x10); | |
| 13 | + | |
| 14 | +define('Spreadsheet_Excel_Reader_Type_BOF', 0x809); | |
| 15 | +define('Spreadsheet_Excel_Reader_Type_EOF', 0x0a); | |
| 16 | +define('Spreadsheet_Excel_Reader_Type_BOUNDSHEET', 0x85); | |
| 17 | +define('Spreadsheet_Excel_Reader_Type_DIMENSION', 0x200); | |
| 18 | +define('Spreadsheet_Excel_Reader_Type_ROW', 0x208); | |
| 19 | +define('Spreadsheet_Excel_Reader_Type_DBCELL', 0xd7); | |
| 20 | +define('Spreadsheet_Excel_Reader_Type_FILEPASS', 0x2f); | |
| 21 | +define('Spreadsheet_Excel_Reader_Type_NOTE', 0x1c); | |
| 22 | +define('Spreadsheet_Excel_Reader_Type_TXO', 0x1b6); | |
| 23 | +define('Spreadsheet_Excel_Reader_Type_RK', 0x7e); | |
| 24 | +define('Spreadsheet_Excel_Reader_Type_RK2', 0x27e); | |
| 25 | +define('Spreadsheet_Excel_Reader_Type_MULRK', 0xbd); | |
| 26 | +define('Spreadsheet_Excel_Reader_Type_MULBLANK', 0xbe); | |
| 27 | +define('Spreadsheet_Excel_Reader_Type_INDEX', 0x20b); | |
| 28 | +define('Spreadsheet_Excel_Reader_Type_SST', 0xfc); | |
| 29 | +define('Spreadsheet_Excel_Reader_Type_EXTSST', 0xff); | |
| 30 | +define('Spreadsheet_Excel_Reader_Type_CONTINUE', 0x3c); | |
| 31 | +define('Spreadsheet_Excel_Reader_Type_LABEL', 0x204); | |
| 32 | +define('Spreadsheet_Excel_Reader_Type_LABELSST', 0xfd); | |
| 33 | +define('Spreadsheet_Excel_Reader_Type_NUMBER', 0x203); | |
| 34 | +define('Spreadsheet_Excel_Reader_Type_NAME', 0x18); | |
| 35 | +define('Spreadsheet_Excel_Reader_Type_ARRAY', 0x221); | |
| 36 | +define('Spreadsheet_Excel_Reader_Type_STRING', 0x207); | |
| 37 | +define('Spreadsheet_Excel_Reader_Type_FORMULA', 0x406); | |
| 38 | +define('Spreadsheet_Excel_Reader_Type_FORMULA2', 0x6); | |
| 39 | +define('Spreadsheet_Excel_Reader_Type_FORMAT', 0x41e); | |
| 40 | +define('Spreadsheet_Excel_Reader_Type_XF', 0xe0); | |
| 41 | +define('Spreadsheet_Excel_Reader_Type_BOOLERR', 0x205); | |
| 42 | +define('Spreadsheet_Excel_Reader_Type_UNKNOWN', 0xffff); | |
| 43 | +define('Spreadsheet_Excel_Reader_Type_NINETEENFOUR', 0x22); | |
| 44 | +define('Spreadsheet_Excel_Reader_Type_MERGEDCELLS', 0xE5); | |
| 45 | + | |
| 46 | +define('Spreadsheet_Excel_Reader_utcOffsetDays' , 25569); | |
| 47 | +define('Spreadsheet_Excel_Reader_utcOffsetDays1904', 24107); | |
| 48 | +define('Spreadsheet_Excel_Reader_msInADay', 24 * 60 * 60); | |
| 49 | + | |
| 50 | +//define('Spreadsheet_Excel_Reader_DEF_NUM_FORMAT', "%.2f"); | |
| 51 | +define('Spreadsheet_Excel_Reader_DEF_NUM_FORMAT', "%s"); | |
| 52 | + | |
| 53 | +// function file_get_contents for PHP < 4.3.0 | |
| 54 | +// Thanks Marian Steinbach for this function | |
| 55 | +if (!function_exists('file_get_contents')) { | |
| 56 | + function file_get_contents($filename, $use_include_path = 0) { | |
| 57 | + $data = ''; | |
| 58 | + $file = @fopen($filename, "rb", $use_include_path); | |
| 59 | + if ($file) { | |
| 60 | + while (!feof($file)) $data .= fread($file, 1024); | |
| 61 | + fclose($file); | |
| 62 | + } else { | |
| 63 | + // There was a problem opening the file | |
| 64 | + $data = FALSE; | |
| 65 | + } | |
| 66 | + return $data; | |
| 67 | + } | |
| 68 | +} | |
| 69 | + | |
| 70 | + | |
| 71 | +//class Spreadsheet_Excel_Reader extends PEAR { | |
| 72 | +class Spreadsheet_Excel_Reader { | |
| 73 | + | |
| 74 | + var $boundsheets = array(); | |
| 75 | + var $formatRecords = array(); | |
| 76 | + var $sst = array(); | |
| 77 | + var $sheets = array(); | |
| 78 | + var $data; | |
| 79 | + var $pos; | |
| 80 | + var $_ole; | |
| 81 | + var $_defaultEncoding; | |
| 82 | + var $_defaultFormat = Spreadsheet_Excel_Reader_DEF_NUM_FORMAT; | |
| 83 | + var $_columnsFormat = array(); | |
| 84 | + var $_rowoffset = 1; | |
| 85 | + var $_coloffset = 1; | |
| 86 | + | |
| 87 | + var $dateFormats = array ( | |
| 88 | + 0xe => "d/m/Y", | |
| 89 | + 0xf => "d-M-Y", | |
| 90 | + 0x10 => "d-M", | |
| 91 | + 0x11 => "M-Y", | |
| 92 | + 0x12 => "h:i a", | |
| 93 | + 0x13 => "h:i:s a", | |
| 94 | + 0x14 => "H:i", | |
| 95 | + 0x15 => "H:i:s", | |
| 96 | + 0x16 => "d/m/Y H:i", | |
| 97 | + 0x2d => "i:s", | |
| 98 | + 0x2e => "H:i:s", | |
| 99 | + 0x2f => "i:s.S"); | |
| 100 | + | |
| 101 | + var $numberFormats = array( | |
| 102 | + 0x1 => "%1.0f", // "0" | |
| 103 | + 0x2 => "%1.2f", // "0.00", | |
| 104 | + 0x3 => "%1.0f", //"#,##0", | |
| 105 | + 0x4 => "%1.2f", //"#,##0.00", | |
| 106 | + 0x5 => "%1.0f", /*"$#,##0;($#,##0)",*/ | |
| 107 | + 0x6 => '$%1.0f', /*"$#,##0;($#,##0)",*/ | |
| 108 | + 0x7 => '$%1.2f', //"$#,##0.00;($#,##0.00)", | |
| 109 | + 0x8 => '$%1.2f', //"$#,##0.00;($#,##0.00)", | |
| 110 | + 0x9 => '%1.0f%%', // "0%" | |
| 111 | + 0xa => '%1.2f%%', // "0.00%" | |
| 112 | + 0xb => '%1.2f', // 0.00E00", | |
| 113 | + 0x25 => '%1.0f', // "#,##0;(#,##0)", | |
| 114 | + 0x26 => '%1.0f', //"#,##0;(#,##0)", | |
| 115 | + 0x27 => '%1.2f', //"#,##0.00;(#,##0.00)", | |
| 116 | + 0x28 => '%1.2f', //"#,##0.00;(#,##0.00)", | |
| 117 | + 0x29 => '%1.0f', //"#,##0;(#,##0)", | |
| 118 | + 0x2a => '$%1.0f', //"$#,##0;($#,##0)", | |
| 119 | + 0x2b => '%1.2f', //"#,##0.00;(#,##0.00)", | |
| 120 | + 0x2c => '$%1.2f', //"$#,##0.00;($#,##0.00)", | |
| 121 | + 0x30 => '%1.0f'); //"##0.0E0"; | |
| 122 | + | |
| 123 | + function Spreadsheet_Excel_Reader(){ | |
| 124 | + $this->_ole =& new OLERead(); | |
| 125 | + $this->setUTFEncoder('iconv'); | |
| 126 | + | |
| 127 | + } | |
| 128 | + | |
| 129 | + function setOutputEncoding($Encoding){ | |
| 130 | + $this->_defaultEncoding = $Encoding; | |
| 131 | + } | |
| 132 | + | |
| 133 | + /** | |
| 134 | + * $encoder = 'iconv' or 'mb' | |
| 135 | + * set iconv if you would like use 'iconv' for encode UTF-16LE to your encoding | |
| 136 | + * set mb if you would like use 'mb_convert_encoding' for encode UTF-16LE to your encoding | |
| 137 | + */ | |
| 138 | + function setUTFEncoder($encoder = 'iconv'){ | |
| 139 | + $this->_encoderFunction = ''; | |
| 140 | + if ($encoder == 'iconv'){ | |
| 141 | + $this->_encoderFunction = function_exists('iconv') ? 'iconv' : ''; | |
| 142 | + }elseif ($encoder == 'mb') { | |
| 143 | + $this->_encoderFunction = function_exists('mb_convert_encoding') ? 'mb_convert_encoding' : ''; | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | + function setRowColOffset($iOffset){ | |
| 148 | + $this->_rowoffset = $iOffset; | |
| 149 | + $this->_coloffset = $iOffset; | |
| 150 | + } | |
| 151 | + | |
| 152 | + function setDefaultFormat($sFormat){ | |
| 153 | + $this->_defaultFormat = $sFormat; | |
| 154 | + } | |
| 155 | + | |
| 156 | + function setColumnFormat($column, $sFormat){ | |
| 157 | + $this->_columnsFormat[$column] = $sFormat; | |
| 158 | + } | |
| 159 | + | |
| 160 | + | |
| 161 | + function read($sFileName) { | |
| 162 | + $errlevel = error_reporting(); | |
| 163 | + error_reporting($errlevel ^ E_NOTICE); | |
| 164 | + $res = $this->_ole->read($sFileName); | |
| 165 | + | |
| 166 | + // oops, something goes wrong (Darko Miljanovic) | |
| 167 | + if($res === false) { | |
| 168 | + // check error code | |
| 169 | + if($this->_ole->error == 1) { | |
| 170 | + // bad file | |
| 171 | + die('The filename ' . $sFileName . ' is not readable'); | |
| 172 | + } | |
| 173 | + // check other error codes here (eg bad fileformat, etc...) | |
| 174 | + } | |
| 175 | + | |
| 176 | + $this->data = $this->_ole->getWorkBook(); | |
| 177 | + | |
| 178 | + | |
| 179 | + /* | |
| 180 | + $res = $this->_ole->read($sFileName); | |
| 181 | + | |
| 182 | + if ($this->isError($res)) { | |
| 183 | +// var_dump($res); | |
| 184 | + return $this->raiseError($res); | |
| 185 | + } | |
| 186 | + | |
| 187 | + $total = $this->_ole->ppsTotal(); | |
| 188 | + for ($i = 0; $i < $total; $i++) { | |
| 189 | + if ($this->_ole->isFile($i)) { | |
| 190 | + $type = unpack("v", $this->_ole->getData($i, 0, 2)); | |
| 191 | + if ($type[''] == 0x0809) { // check if it's a BIFF stream | |
| 192 | + $this->_index = $i; | |
| 193 | + $this->data = $this->_ole->getData($i, 0, $this->_ole->getDataLength($i)); | |
| 194 | + break; | |
| 195 | + } | |
| 196 | + } | |
| 197 | + } | |
| 198 | + | |
| 199 | + if ($this->_index === null) { | |
| 200 | + return $this->raiseError("$file doesn't seem to be an Excel file"); | |
| 201 | + } | |
| 202 | + | |
| 203 | + */ | |
| 204 | + | |
| 205 | + //var_dump($this->data); | |
| 206 | + //echo "data =".$this->data; | |
| 207 | + $this->pos = 0; | |
| 208 | + //$this->readRecords(); | |
| 209 | + $this->_parse(); | |
| 210 | + error_reporting($errlevel); | |
| 211 | + | |
| 212 | + } | |
| 213 | + | |
| 214 | + function _parse(){ | |
| 215 | + $pos = 0; | |
| 216 | + | |
| 217 | + $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8; | |
| 218 | + $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8; | |
| 219 | + | |
| 220 | + $version = ord($this->data[$pos + 4]) | ord($this->data[$pos + 5])<<8; | |
| 221 | + $substreamType = ord($this->data[$pos + 6]) | ord($this->data[$pos + 7])<<8; | |
| 222 | + //echo "Start parse code=".base_convert($code,10,16)." version=".base_convert($version,10,16)." substreamType=".base_convert($substreamType,10,16).""."\n"; | |
| 223 | + | |
| 224 | + if (($version != Spreadsheet_Excel_Reader_BIFF8) && ($version != Spreadsheet_Excel_Reader_BIFF7)) { | |
| 225 | + return false; | |
| 226 | + } | |
| 227 | + | |
| 228 | + if ($substreamType != Spreadsheet_Excel_Reader_WorkbookGlobals){ | |
| 229 | + return false; | |
| 230 | + } | |
| 231 | + | |
| 232 | + //print_r($rec); | |
| 233 | + $pos += $length + 4; | |
| 234 | + | |
| 235 | + $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8; | |
| 236 | + $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8; | |
| 237 | + | |
| 238 | + while ($code != Spreadsheet_Excel_Reader_Type_EOF){ | |
| 239 | + switch ($code) { | |
| 240 | + case Spreadsheet_Excel_Reader_Type_SST: | |
| 241 | + //echo "Type_SST\n"; | |
| 242 | + $spos = $pos + 4; | |
| 243 | + $limitpos = $spos + $length; | |
| 244 | + $uniqueStrings = $this->_GetInt4d($this->data, $spos+4); | |
| 245 | + $spos += 8; | |
| 246 | + for ($i = 0; $i < $uniqueStrings; $i++) { | |
| 247 | + // Read in the number of characters | |
| 248 | + if ($spos == $limitpos) { | |
| 249 | + $opcode = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 250 | + $conlength = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 251 | + if ($opcode != 0x3c) { | |
| 252 | + return -1; | |
| 253 | + } | |
| 254 | + $spos += 4; | |
| 255 | + $limitpos = $spos + $conlength; | |
| 256 | + } | |
| 257 | + $numChars = ord($this->data[$spos]) | (ord($this->data[$spos+1]) << 8); | |
| 258 | + //echo "i = $i pos = $pos numChars = $numChars "; | |
| 259 | + $spos += 2; | |
| 260 | + $optionFlags = ord($this->data[$spos]); | |
| 261 | + $spos++; | |
| 262 | + $asciiEncoding = (($optionFlags & 0x01) == 0) ; | |
| 263 | + $extendedString = ( ($optionFlags & 0x04) != 0); | |
| 264 | + | |
| 265 | + // See if string contains formatting information | |
| 266 | + $richString = ( ($optionFlags & 0x08) != 0); | |
| 267 | + | |
| 268 | + if ($richString) { | |
| 269 | + // Read in the crun | |
| 270 | + $formattingRuns = ord($this->data[$spos]) | (ord($this->data[$spos+1]) << 8); | |
| 271 | + $spos += 2; | |
| 272 | + } | |
| 273 | + | |
| 274 | + if ($extendedString) { | |
| 275 | + // Read in cchExtRst | |
| 276 | + $extendedRunLength = $this->_GetInt4d($this->data, $spos); | |
| 277 | + $spos += 4; | |
| 278 | + } | |
| 279 | + | |
| 280 | + $len = ($asciiEncoding)? $numChars : $numChars*2; | |
| 281 | + if ($spos + $len < $limitpos) { | |
| 282 | + $retstr = substr($this->data, $spos, $len); | |
| 283 | + $spos += $len; | |
| 284 | + }else{ | |
| 285 | + // found countinue | |
| 286 | + $retstr = substr($this->data, $spos, $limitpos - $spos); | |
| 287 | + $bytesRead = $limitpos - $spos; | |
| 288 | + $charsLeft = $numChars - (($asciiEncoding) ? $bytesRead : ($bytesRead / 2)); | |
| 289 | + $spos = $limitpos; | |
| 290 | + | |
| 291 | + while ($charsLeft > 0){ | |
| 292 | + $opcode = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 293 | + $conlength = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 294 | + if ($opcode != 0x3c) { | |
| 295 | + return -1; | |
| 296 | + } | |
| 297 | + $spos += 4; | |
| 298 | + $limitpos = $spos + $conlength; | |
| 299 | + $option = ord($this->data[$spos]); | |
| 300 | + $spos += 1; | |
| 301 | + if ($asciiEncoding && ($option == 0)) { | |
| 302 | + $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength); | |
| 303 | + $retstr .= substr($this->data, $spos, $len); | |
| 304 | + $charsLeft -= $len; | |
| 305 | + $asciiEncoding = true; | |
| 306 | + }elseif (!$asciiEncoding && ($option != 0)){ | |
| 307 | + $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength); | |
| 308 | + $retstr .= substr($this->data, $spos, $len); | |
| 309 | + $charsLeft -= $len/2; | |
| 310 | + $asciiEncoding = false; | |
| 311 | + }elseif (!$asciiEncoding && ($option == 0)) { | |
| 312 | + // Bummer - the string starts off as Unicode, but after the | |
| 313 | + // continuation it is in straightforward ASCII encoding | |
| 314 | + $len = min($charsLeft, $limitpos - $spos); // min($charsLeft, $conlength); | |
| 315 | + for ($j = 0; $j < $len; $j++) { | |
| 316 | + $retstr .= $this->data[$spos + $j].chr(0); | |
| 317 | + } | |
| 318 | + $charsLeft -= $len; | |
| 319 | + $asciiEncoding = false; | |
| 320 | + }else{ | |
| 321 | + $newstr = ''; | |
| 322 | + for ($j = 0; $j < strlen($retstr); $j++) { | |
| 323 | + $newstr = $retstr[$j].chr(0); | |
| 324 | + } | |
| 325 | + $retstr = $newstr; | |
| 326 | + $len = min($charsLeft * 2, $limitpos - $spos); // min($charsLeft, $conlength); | |
| 327 | + $retstr .= substr($this->data, $spos, $len); | |
| 328 | + $charsLeft -= $len/2; | |
| 329 | + $asciiEncoding = false; | |
| 330 | + //echo "Izavrat\n"; | |
| 331 | + } | |
| 332 | + $spos += $len; | |
| 333 | + | |
| 334 | + } | |
| 335 | + } | |
| 336 | + $retstr = ($asciiEncoding) ? $retstr : $this->_encodeUTF16($retstr); | |
| 337 | +// echo "Str $i = $retstr\n"; | |
| 338 | + if ($richString){ | |
| 339 | + $spos += 4 * $formattingRuns; | |
| 340 | + } | |
| 341 | + | |
| 342 | + // For extended strings, skip over the extended string data | |
| 343 | + if ($extendedString) { | |
| 344 | + $spos += $extendedRunLength; | |
| 345 | + } | |
| 346 | + //if ($retstr == 'Derby'){ | |
| 347 | + // echo "bb\n"; | |
| 348 | + //} | |
| 349 | + $this->sst[]=$retstr; | |
| 350 | + } | |
| 351 | + /*$continueRecords = array(); | |
| 352 | + while ($this->getNextCode() == Type_CONTINUE) { | |
| 353 | + $continueRecords[] = &$this->nextRecord(); | |
| 354 | + } | |
| 355 | + //echo " 1 Type_SST\n"; | |
| 356 | + $this->shareStrings = new SSTRecord($r, $continueRecords); | |
| 357 | + //print_r($this->shareStrings->strings); | |
| 358 | + */ | |
| 359 | + // echo 'SST read: '.($time_end-$time_start)."\n"; | |
| 360 | + break; | |
| 361 | + | |
| 362 | + case Spreadsheet_Excel_Reader_Type_FILEPASS: | |
| 363 | + return false; | |
| 364 | + break; | |
| 365 | + case Spreadsheet_Excel_Reader_Type_NAME: | |
| 366 | + //echo "Type_NAME\n"; | |
| 367 | + break; | |
| 368 | + case Spreadsheet_Excel_Reader_Type_FORMAT: | |
| 369 | + $indexCode = ord($this->data[$pos+4]) | ord($this->data[$pos+5]) << 8; | |
| 370 | + | |
| 371 | + if ($version == Spreadsheet_Excel_Reader_BIFF8) { | |
| 372 | + $numchars = ord($this->data[$pos+6]) | ord($this->data[$pos+7]) << 8; | |
| 373 | + if (ord($this->data[$pos+8]) == 0){ | |
| 374 | + $formatString = substr($this->data, $pos+9, $numchars); | |
| 375 | + } else { | |
| 376 | + $formatString = substr($this->data, $pos+9, $numchars*2); | |
| 377 | + } | |
| 378 | + } else { | |
| 379 | + $numchars = ord($this->data[$pos+6]); | |
| 380 | + $formatString = substr($this->data, $pos+7, $numchars*2); | |
| 381 | + } | |
| 382 | + | |
| 383 | + $this->formatRecords[$indexCode] = $formatString; | |
| 384 | + // echo "Type.FORMAT\n"; | |
| 385 | + break; | |
| 386 | + case Spreadsheet_Excel_Reader_Type_XF: | |
| 387 | + //global $dateFormats, $numberFormats; | |
| 388 | + $indexCode = ord($this->data[$pos+6]) | ord($this->data[$pos+7]) << 8; | |
| 389 | + //echo "\nType.XF ".count($this->formatRecords['xfrecords'])." $indexCode "; | |
| 390 | + if (array_key_exists($indexCode, $this->dateFormats)) { | |
| 391 | + //echo "isdate ".$dateFormats[$indexCode]; | |
| 392 | + $this->formatRecords['xfrecords'][] = array( | |
| 393 | + 'type' => 'date', | |
| 394 | + 'format' => $this->dateFormats[$indexCode] | |
| 395 | + ); | |
| 396 | + }elseif (array_key_exists($indexCode, $this->numberFormats)) { | |
| 397 | + //echo "isnumber ".$this->numberFormats[$indexCode]; | |
| 398 | + $this->formatRecords['xfrecords'][] = array( | |
| 399 | + 'type' => 'number', | |
| 400 | + 'format' => $this->numberFormats[$indexCode] | |
| 401 | + ); | |
| 402 | + }else{ | |
| 403 | + $isdate = FALSE; | |
| 404 | + if ($indexCode > 0){ | |
| 405 | + if (isset($this->formatRecords[$indexCode])) | |
| 406 | + $formatstr = $this->formatRecords[$indexCode]; | |
| 407 | + //echo '.other.'; | |
| 408 | + //echo "\ndate-time=$formatstr=\n"; | |
| 409 | + if ($formatstr) | |
| 410 | + if (preg_match("/[^hmsday\/\-:\s]/i", $formatstr) == 0) { // found day and time format | |
| 411 | + $isdate = TRUE; | |
| 412 | + $formatstr = str_replace('mm', 'i', $formatstr); | |
| 413 | + $formatstr = str_replace('h', 'H', $formatstr); | |
| 414 | + //echo "\ndate-time $formatstr \n"; | |
| 415 | + } | |
| 416 | + } | |
| 417 | + | |
| 418 | + if ($isdate){ | |
| 419 | + $this->formatRecords['xfrecords'][] = array( | |
| 420 | + 'type' => 'date', | |
| 421 | + 'format' => $formatstr, | |
| 422 | + ); | |
| 423 | + }else{ | |
| 424 | + $this->formatRecords['xfrecords'][] = array( | |
| 425 | + 'type' => 'other', | |
| 426 | + 'format' => '', | |
| 427 | + 'code' => $indexCode | |
| 428 | + ); | |
| 429 | + } | |
| 430 | + } | |
| 431 | + //echo "\n"; | |
| 432 | + break; | |
| 433 | + case Spreadsheet_Excel_Reader_Type_NINETEENFOUR: | |
| 434 | + //echo "Type.NINETEENFOUR\n"; | |
| 435 | + $this->nineteenFour = (ord($this->data[$pos+4]) == 1); | |
| 436 | + break; | |
| 437 | + case Spreadsheet_Excel_Reader_Type_BOUNDSHEET: | |
| 438 | + //echo "Type.BOUNDSHEET\n"; | |
| 439 | + $rec_offset = $this->_GetInt4d($this->data, $pos+4); | |
| 440 | + $rec_typeFlag = ord($this->data[$pos+8]); | |
| 441 | + $rec_visibilityFlag = ord($this->data[$pos+9]); | |
| 442 | + $rec_length = ord($this->data[$pos+10]); | |
| 443 | + | |
| 444 | + if ($version == Spreadsheet_Excel_Reader_BIFF8){ | |
| 445 | + $chartype = ord($this->data[$pos+11]); | |
| 446 | + if ($chartype == 0){ | |
| 447 | + $rec_name = substr($this->data, $pos+12, $rec_length); | |
| 448 | + } else { | |
| 449 | + $rec_name = $this->_encodeUTF16(substr($this->data, $pos+12, $rec_length*2)); | |
| 450 | + } | |
| 451 | + }elseif ($version == Spreadsheet_Excel_Reader_BIFF7){ | |
| 452 | + $rec_name = substr($this->data, $pos+11, $rec_length); | |
| 453 | + } | |
| 454 | + $this->boundsheets[] = array('name'=>$rec_name, | |
| 455 | + 'offset'=>$rec_offset); | |
| 456 | + | |
| 457 | + break; | |
| 458 | + | |
| 459 | + } | |
| 460 | + | |
| 461 | + //echo "Code = ".base_convert($r['code'],10,16)."\n"; | |
| 462 | + $pos += $length + 4; | |
| 463 | + $code = ord($this->data[$pos]) | ord($this->data[$pos+1])<<8; | |
| 464 | + $length = ord($this->data[$pos+2]) | ord($this->data[$pos+3])<<8; | |
| 465 | + | |
| 466 | + //$r = &$this->nextRecord(); | |
| 467 | + //echo "1 Code = ".base_convert($r['code'],10,16)."\n"; | |
| 468 | + } | |
| 469 | + | |
| 470 | + foreach ($this->boundsheets as $key=>$val){ | |
| 471 | + $this->sn = $key; | |
| 472 | + $this->_parsesheet($val['offset']); | |
| 473 | + } | |
| 474 | + return true; | |
| 475 | + | |
| 476 | + } | |
| 477 | + | |
| 478 | + function _parsesheet($spos){ | |
| 479 | + $cont = true; | |
| 480 | + // read BOF | |
| 481 | + $code = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 482 | + $length = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 483 | + | |
| 484 | + $version = ord($this->data[$spos + 4]) | ord($this->data[$spos + 5])<<8; | |
| 485 | + $substreamType = ord($this->data[$spos + 6]) | ord($this->data[$spos + 7])<<8; | |
| 486 | + | |
| 487 | + if (($version != Spreadsheet_Excel_Reader_BIFF8) && ($version != Spreadsheet_Excel_Reader_BIFF7)) { | |
| 488 | + return -1; | |
| 489 | + } | |
| 490 | + | |
| 491 | + if ($substreamType != Spreadsheet_Excel_Reader_Worksheet){ | |
| 492 | + return -2; | |
| 493 | + } | |
| 494 | + //echo "Start parse code=".base_convert($code,10,16)." version=".base_convert($version,10,16)." substreamType=".base_convert($substreamType,10,16).""."\n"; | |
| 495 | + $spos += $length + 4; | |
| 496 | + //var_dump($this->formatRecords); | |
| 497 | + //echo "code $code $length"; | |
| 498 | + while($cont) { | |
| 499 | + //echo "mem= ".memory_get_usage()."\n"; | |
| 500 | +// $r = &$this->file->nextRecord(); | |
| 501 | + $lowcode = ord($this->data[$spos]); | |
| 502 | + if ($lowcode == Spreadsheet_Excel_Reader_Type_EOF) break; | |
| 503 | + $code = $lowcode | ord($this->data[$spos+1])<<8; | |
| 504 | + $length = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 505 | + $spos += 4; | |
| 506 | + $this->sheets[$this->sn]['maxrow'] = $this->_rowoffset - 1; | |
| 507 | + $this->sheets[$this->sn]['maxcol'] = $this->_coloffset - 1; | |
| 508 | + //echo "Code=".base_convert($code,10,16)." $code\n"; | |
| 509 | + unset($this->rectype); | |
| 510 | + $this->multiplier = 1; // need for format with % | |
| 511 | + switch ($code) { | |
| 512 | + case Spreadsheet_Excel_Reader_Type_DIMENSION: | |
| 513 | + //echo 'Type_DIMENSION '; | |
| 514 | + if (!isset($this->numRows)) { | |
| 515 | + if (($length == 10) || ($version == Spreadsheet_Excel_Reader_BIFF7)){ | |
| 516 | + $this->sheets[$this->sn]['numRows'] = ord($this->data[$spos+2]) | ord($this->data[$spos+3]) << 8; | |
| 517 | + $this->sheets[$this->sn]['numCols'] = ord($this->data[$spos+6]) | ord($this->data[$spos+7]) << 8; | |
| 518 | + } else { | |
| 519 | + $this->sheets[$this->sn]['numRows'] = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8; | |
| 520 | + $this->sheets[$this->sn]['numCols'] = ord($this->data[$spos+10]) | ord($this->data[$spos+11]) << 8; | |
| 521 | + } | |
| 522 | + } | |
| 523 | + //echo 'numRows '.$this->numRows.' '.$this->numCols."\n"; | |
| 524 | + break; | |
| 525 | + case Spreadsheet_Excel_Reader_Type_MERGEDCELLS: | |
| 526 | + $cellRanges = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 527 | + for ($i = 0; $i < $cellRanges; $i++) { | |
| 528 | + $fr = ord($this->data[$spos + 8*$i + 2]) | ord($this->data[$spos + 8*$i + 3])<<8; | |
| 529 | + $lr = ord($this->data[$spos + 8*$i + 4]) | ord($this->data[$spos + 8*$i + 5])<<8; | |
| 530 | + $fc = ord($this->data[$spos + 8*$i + 6]) | ord($this->data[$spos + 8*$i + 7])<<8; | |
| 531 | + $lc = ord($this->data[$spos + 8*$i + 8]) | ord($this->data[$spos + 8*$i + 9])<<8; | |
| 532 | + //$this->sheets[$this->sn]['mergedCells'][] = array($fr + 1, $fc + 1, $lr + 1, $lc + 1); | |
| 533 | + if ($lr - $fr > 0) { | |
| 534 | + $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['rowspan'] = $lr - $fr + 1; | |
| 535 | + } | |
| 536 | + if ($lc - $fc > 0) { | |
| 537 | + $this->sheets[$this->sn]['cellsInfo'][$fr+1][$fc+1]['colspan'] = $lc - $fc + 1; | |
| 538 | + } | |
| 539 | + } | |
| 540 | + //echo "Merged Cells $cellRanges $lr $fr $lc $fc\n"; | |
| 541 | + break; | |
| 542 | + case Spreadsheet_Excel_Reader_Type_RK: | |
| 543 | + case Spreadsheet_Excel_Reader_Type_RK2: | |
| 544 | + //echo 'Spreadsheet_Excel_Reader_Type_RK'."\n"; | |
| 545 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 546 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 547 | + $rknum = $this->_GetInt4d($this->data, $spos + 6); | |
| 548 | + $numValue = $this->_GetIEEE754($rknum); | |
| 549 | + //echo $numValue." "; | |
| 550 | + if ($this->isDate($spos)) { | |
| 551 | + list($string, $raw) = $this->createDate($numValue); | |
| 552 | + }else{ | |
| 553 | + $raw = $numValue; | |
| 554 | + if (isset($this->_columnsFormat[$column + 1])){ | |
| 555 | + $this->curformat = $this->_columnsFormat[$column + 1]; | |
| 556 | + } | |
| 557 | + $string = sprintf($this->curformat, $numValue * $this->multiplier); | |
| 558 | + //$this->addcell(RKRecord($r)); | |
| 559 | + } | |
| 560 | + $this->addcell($row, $column, $string, $raw); | |
| 561 | + //echo "Type_RK $row $column $string $raw {$this->curformat}\n"; | |
| 562 | + break; | |
| 563 | + case Spreadsheet_Excel_Reader_Type_LABELSST: | |
| 564 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 565 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 566 | + $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5])<<8; | |
| 567 | + $index = $this->_GetInt4d($this->data, $spos + 6); | |
| 568 | + //var_dump($this->sst); | |
| 569 | + $this->addcell($row, $column, $this->sst[$index]); | |
| 570 | + //echo "LabelSST $row $column $string\n"; | |
| 571 | + break; | |
| 572 | + case Spreadsheet_Excel_Reader_Type_MULRK: | |
| 573 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 574 | + $colFirst = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 575 | + $colLast = ord($this->data[$spos + $length - 2]) | ord($this->data[$spos + $length - 1])<<8; | |
| 576 | + $columns = $colLast - $colFirst + 1; | |
| 577 | + $tmppos = $spos+4; | |
| 578 | + for ($i = 0; $i < $columns; $i++) { | |
| 579 | + $numValue = $this->_GetIEEE754($this->_GetInt4d($this->data, $tmppos + 2)); | |
| 580 | + if ($this->isDate($tmppos-4)) { | |
| 581 | + list($string, $raw) = $this->createDate($numValue); | |
| 582 | + }else{ | |
| 583 | + $raw = $numValue; | |
| 584 | + if (isset($this->_columnsFormat[$colFirst + $i + 1])){ | |
| 585 | + $this->curformat = $this->_columnsFormat[$colFirst + $i + 1]; | |
| 586 | + } | |
| 587 | + $string = sprintf($this->curformat, $numValue * $this->multiplier); | |
| 588 | + } | |
| 589 | + //$rec['rknumbers'][$i]['xfindex'] = ord($rec['data'][$pos]) | ord($rec['data'][$pos+1]) << 8; | |
| 590 | + $tmppos += 6; | |
| 591 | + $this->addcell($row, $colFirst + $i, $string, $raw); | |
| 592 | + //echo "MULRK $row ".($colFirst + $i)." $string\n"; | |
| 593 | + } | |
| 594 | + //MulRKRecord($r); | |
| 595 | + // Get the individual cell records from the multiple record | |
| 596 | + //$num = ; | |
| 597 | + | |
| 598 | + break; | |
| 599 | + case Spreadsheet_Excel_Reader_Type_NUMBER: | |
| 600 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 601 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 602 | + $tmp = unpack("ddouble", substr($this->data, $spos + 6, 8)); // It machine machine dependent | |
| 603 | + if ($this->isDate($spos)) { | |
| 604 | + list($string, $raw) = $this->createDate($tmp['double']); | |
| 605 | + // $this->addcell(DateRecord($r, 1)); | |
| 606 | + }else{ | |
| 607 | + //$raw = $tmp['']; | |
| 608 | + if (isset($this->_columnsFormat[$column + 1])){ | |
| 609 | + $this->curformat = $this->_columnsFormat[$column + 1]; | |
| 610 | + } | |
| 611 | + $raw = $this->createNumber($spos); | |
| 612 | + $string = sprintf($this->curformat, $raw * $this->multiplier); | |
| 613 | + | |
| 614 | + // $this->addcell(NumberRecord($r)); | |
| 615 | + } | |
| 616 | + $this->addcell($row, $column, $string, $raw); | |
| 617 | + //echo "Number $row $column $string\n"; | |
| 618 | + break; | |
| 619 | + case Spreadsheet_Excel_Reader_Type_FORMULA: | |
| 620 | + case Spreadsheet_Excel_Reader_Type_FORMULA2: | |
| 621 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 622 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 623 | + if ((ord($this->data[$spos+6])==0) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) { | |
| 624 | + //String formula. Result follows in a STRING record | |
| 625 | + //echo "FORMULA $row $column Formula with a string<br>\n"; | |
| 626 | + } elseif ((ord($this->data[$spos+6])==1) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) { | |
| 627 | + //Boolean formula. Result is in +2; 0=false,1=true | |
| 628 | + } elseif ((ord($this->data[$spos+6])==2) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) { | |
| 629 | + //Error formula. Error code is in +2; | |
| 630 | + } elseif ((ord($this->data[$spos+6])==3) && (ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) { | |
| 631 | + //Formula result is a null string. | |
| 632 | + } else { | |
| 633 | + // result is a number, so first 14 bytes are just like a _NUMBER record | |
| 634 | + $tmp = unpack("ddouble", substr($this->data, $spos + 6, 8)); // It machine machine dependent | |
| 635 | + if ($this->isDate($spos)) { | |
| 636 | + list($string, $raw) = $this->createDate($tmp['double']); | |
| 637 | + // $this->addcell(DateRecord($r, 1)); | |
| 638 | + }else{ | |
| 639 | + //$raw = $tmp['']; | |
| 640 | + if (isset($this->_columnsFormat[$column + 1])){ | |
| 641 | + $this->curformat = $this->_columnsFormat[$column + 1]; | |
| 642 | + } | |
| 643 | + $raw = $this->createNumber($spos); | |
| 644 | + $string = sprintf($this->curformat, $raw * $this->multiplier); | |
| 645 | + | |
| 646 | + // $this->addcell(NumberRecord($r)); | |
| 647 | + } | |
| 648 | + $this->addcell($row, $column, $string, $raw); | |
| 649 | + //echo "Number $row $column $string\n"; | |
| 650 | + } | |
| 651 | + break; | |
| 652 | + case Spreadsheet_Excel_Reader_Type_BOOLERR: | |
| 653 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 654 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 655 | + $string = ord($this->data[$spos+6]); | |
| 656 | + $this->addcell($row, $column, $string); | |
| 657 | + //echo 'Type_BOOLERR '."\n"; | |
| 658 | + break; | |
| 659 | + case Spreadsheet_Excel_Reader_Type_ROW: | |
| 660 | + case Spreadsheet_Excel_Reader_Type_DBCELL: | |
| 661 | + case Spreadsheet_Excel_Reader_Type_MULBLANK: | |
| 662 | + break; | |
| 663 | + case Spreadsheet_Excel_Reader_Type_LABEL: | |
| 664 | + $row = ord($this->data[$spos]) | ord($this->data[$spos+1])<<8; | |
| 665 | + $column = ord($this->data[$spos+2]) | ord($this->data[$spos+3])<<8; | |
| 666 | + $this->addcell($row, $column, substr($this->data, $spos + 8, ord($this->data[$spos + 6]) | ord($this->data[$spos + 7])<<8)); | |
| 667 | + | |
| 668 | + // $this->addcell(LabelRecord($r)); | |
| 669 | + break; | |
| 670 | + | |
| 671 | + case Spreadsheet_Excel_Reader_Type_EOF: | |
| 672 | + $cont = false; | |
| 673 | + break; | |
| 674 | + default: | |
| 675 | + //echo ' unknown :'.base_convert($r['code'],10,16)."\n"; | |
| 676 | + break; | |
| 677 | + | |
| 678 | + } | |
| 679 | + $spos += $length; | |
| 680 | + } | |
| 681 | + | |
| 682 | + if (!isset($this->sheets[$this->sn]['numRows'])) | |
| 683 | + $this->sheets[$this->sn]['numRows'] = $this->sheets[$this->sn]['maxrow']; | |
| 684 | + if (!isset($this->sheets[$this->sn]['numCols'])) | |
| 685 | + $this->sheets[$this->sn]['numCols'] = $this->sheets[$this->sn]['maxcol']; | |
| 686 | + | |
| 687 | + } | |
| 688 | + | |
| 689 | + function isDate($spos){ | |
| 690 | + //$xfindex = GetInt2d(, 4); | |
| 691 | + $xfindex = ord($this->data[$spos+4]) | ord($this->data[$spos+5]) << 8; | |
| 692 | + //echo 'check is date '.$xfindex.' '.$this->formatRecords['xfrecords'][$xfindex]['type']."\n"; | |
| 693 | + //var_dump($this->formatRecords['xfrecords'][$xfindex]); | |
| 694 | + if ($this->formatRecords['xfrecords'][$xfindex]['type'] == 'date') { | |
| 695 | + $this->curformat = $this->formatRecords['xfrecords'][$xfindex]['format']; | |
| 696 | + $this->rectype = 'date'; | |
| 697 | + return true; | |
| 698 | + } else { | |
| 699 | + if ($this->formatRecords['xfrecords'][$xfindex]['type'] == 'number') { | |
| 700 | + $this->curformat = $this->formatRecords['xfrecords'][$xfindex]['format']; | |
| 701 | + $this->rectype = 'number'; | |
| 702 | + if (($xfindex == 0x9) || ($xfindex == 0xa)){ | |
| 703 | + $this->multiplier = 100; | |
| 704 | + } | |
| 705 | + }else{ | |
| 706 | + $this->curformat = $this->_defaultFormat; | |
| 707 | + $this->rectype = 'unknown'; | |
| 708 | + } | |
| 709 | + return false; | |
| 710 | + } | |
| 711 | + } | |
| 712 | + | |
| 713 | + function createDate($numValue){ | |
| 714 | + if ($numValue > 1){ | |
| 715 | + $utcDays = $numValue - ($this->nineteenFour ? Spreadsheet_Excel_Reader_utcOffsetDays1904 : Spreadsheet_Excel_Reader_utcOffsetDays); | |
| 716 | + $utcValue = round($utcDays * Spreadsheet_Excel_Reader_msInADay); | |
| 717 | + $string = date ($this->curformat, $utcValue); | |
| 718 | + $raw = $utcValue; | |
| 719 | + }else{ | |
| 720 | + $raw = $numValue; | |
| 721 | + $hours = floor($numValue * 24); | |
| 722 | + $mins = floor($numValue * 24 * 60) - $hours * 60; | |
| 723 | + $secs = floor($numValue * Spreadsheet_Excel_Reader_msInADay) - $hours * 60 * 60 - $mins * 60; | |
| 724 | + $string = date ($this->curformat, mktime($hours, $mins, $secs)); | |
| 725 | + } | |
| 726 | + return array($string, $raw); | |
| 727 | + } | |
| 728 | + | |
| 729 | + function createNumber($spos){ | |
| 730 | + $rknumhigh = $this->_GetInt4d($this->data, $spos + 10); | |
| 731 | + $rknumlow = $this->_GetInt4d($this->data, $spos + 6); | |
| 732 | + //for ($i=0; $i<8; $i++) { echo ord($this->data[$i+$spos+6]) . " "; } echo "<br>"; | |
| 733 | + $sign = ($rknumhigh & 0x80000000) >> 31; | |
| 734 | + $exp = ($rknumhigh & 0x7ff00000) >> 20; | |
| 735 | + $mantissa = (0x100000 | ($rknumhigh & 0x000fffff)); | |
| 736 | + $mantissalow1 = ($rknumlow & 0x80000000) >> 31; | |
| 737 | + $mantissalow2 = ($rknumlow & 0x7fffffff); | |
| 738 | + $value = $mantissa / pow( 2 , (20- ($exp - 1023))); | |
| 739 | + if ($mantissalow1 != 0) $value += 1 / pow (2 , (21 - ($exp - 1023))); | |
| 740 | + $value += $mantissalow2 / pow (2 , (52 - ($exp - 1023))); | |
| 741 | + //echo "Sign = $sign, Exp = $exp, mantissahighx = $mantissa, mantissalow1 = $mantissalow1, mantissalow2 = $mantissalow2<br>\n"; | |
| 742 | + if ($sign) {$value = -1 * $value;} | |
| 743 | + return $value; | |
| 744 | + } | |
| 745 | + | |
| 746 | + function addcell($row, $col, $string, $raw = ''){ | |
| 747 | + //echo "ADD cel $row-$col $string\n"; | |
| 748 | + $this->sheets[$this->sn]['maxrow'] = max($this->sheets[$this->sn]['maxrow'], $row + $this->_rowoffset); | |
| 749 | + $this->sheets[$this->sn]['maxcol'] = max($this->sheets[$this->sn]['maxcol'], $col + $this->_coloffset); | |
| 750 | + $this->sheets[$this->sn]['cells'][$row + $this->_rowoffset][$col + $this->_coloffset] = $string; | |
| 751 | + if ($raw) | |
| 752 | + $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['raw'] = $raw; | |
| 753 | + if (isset($this->rectype)) | |
| 754 | + $this->sheets[$this->sn]['cellsInfo'][$row + $this->_rowoffset][$col + $this->_coloffset]['type'] = $this->rectype; | |
| 755 | + | |
| 756 | + } | |
| 757 | + | |
| 758 | + | |
| 759 | + function _GetIEEE754($rknum){ | |
| 760 | + if (($rknum & 0x02) != 0) { | |
| 761 | + $value = $rknum >> 2; | |
| 762 | + } else { | |
| 763 | +//mmp | |
| 764 | +// first comment out the previously existing 7 lines of code here | |
| 765 | +// $tmp = unpack("d", pack("VV", 0, ($rknum & 0xfffffffc))); | |
| 766 | +// //$value = $tmp['']; | |
| 767 | +// if (array_key_exists(1, $tmp)) { | |
| 768 | +// $value = $tmp[1]; | |
| 769 | +// } else { | |
| 770 | +// $value = $tmp['']; | |
| 771 | +// } | |
| 772 | +// I got my info on IEEE754 encoding from | |
| 773 | +// http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html | |
| 774 | +// The RK format calls for using only the most significant 30 bits of the | |
| 775 | +// 64 bit floating point value. The other 34 bits are assumed to be 0 | |
| 776 | +// So, we use the upper 30 bits of $rknum as follows... | |
| 777 | + $sign = ($rknum & 0x80000000) >> 31; | |
| 778 | + $exp = ($rknum & 0x7ff00000) >> 20; | |
| 779 | + $mantissa = (0x100000 | ($rknum & 0x000ffffc)); | |
| 780 | + $value = $mantissa / pow( 2 , (20- ($exp - 1023))); | |
| 781 | + if ($sign) {$value = -1 * $value;} | |
| 782 | +//end of changes by mmp | |
| 783 | + | |
| 784 | + } | |
| 785 | + | |
| 786 | + if (($rknum & 0x01) != 0) { | |
| 787 | + $value /= 100; | |
| 788 | + } | |
| 789 | + return $value; | |
| 790 | + } | |
| 791 | + | |
| 792 | + function _encodeUTF16($string){ | |
| 793 | + $result = $string; | |
| 794 | + if ($this->_defaultEncoding){ | |
| 795 | + switch ($this->_encoderFunction){ | |
| 796 | + case 'iconv' : $result = iconv('UTF-16LE', $this->_defaultEncoding, $string); | |
| 797 | + break; | |
| 798 | + case 'mb_convert_encoding' : $result = mb_convert_encoding($string, $this->_defaultEncoding, 'UTF-16LE' ); | |
| 799 | + break; | |
| 800 | + } | |
| 801 | + } | |
| 802 | + return $result; | |
| 803 | + } | |
| 804 | + | |
| 805 | + function _GetInt4d($data, $pos) { | |
| 806 | + return ord($data[$pos]) | (ord($data[$pos+1]) << 8) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24); | |
| 807 | + } | |
| 808 | + | |
| 809 | + | |
| 810 | +} | |
| 811 | + | |
| 812 | + | |
| 813 | +?> | ... | ... |
thirdparty/excelreader/README
0 → 100644
| 1 | +INTRODUCTION | |
| 2 | +Read data from Excel spread sheets without Microsoft! | |
| 3 | +Provides an API to allow any application to read Excel | |
| 4 | +documents. Written in PHP. Based on the the Java version by Andy Khan. | |
| 5 | + | |
| 6 | +LINKS | |
| 7 | + | |
| 8 | +OpenOffice.org's Documentation | |
| 9 | +http://sc.openoffice.org/excelfileformat.pdf | |
| 10 | + | |
| 11 | +OLE2 Storage Documentation | |
| 12 | +http://jakarta.apache.org/poi/poifs/fileformat.html | |
| 13 | + | |
| 14 | +Java API for reading, writing and modifying the contents of Excel spreadsheets | |
| 15 | +http://www.andykhan.com/ | |
| 16 | + | |
| 17 | +CONTACT | |
| 18 | +Vadim Tkachenko | |
| 19 | +vt@apachephp.com | |
| 20 | + | |
| 21 | +INFO | |
| 22 | +For use encoding you must have installed iconv extension, otherwise data output in unicode | |
| 23 | + | |
| 24 | +HOW TO USE | |
| 25 | +see example.php | |
| 0 | 26 | \ No newline at end of file | ... | ... |
thirdparty/excelreader/README.KT
0 → 100644
thirdparty/excelreader/example.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | + | |
| 4 | +require_once 'Excel/reader.php'; | |
| 5 | + | |
| 6 | + | |
| 7 | +// ExcelFile($filename, $encoding); | |
| 8 | +$data = new Spreadsheet_Excel_Reader(); | |
| 9 | + | |
| 10 | + | |
| 11 | +// Set output Encoding. | |
| 12 | +$data->setOutputEncoding('CP1251'); | |
| 13 | + | |
| 14 | +/*** | |
| 15 | +* if you want you can change 'iconv' to mb_convert_encoding: | |
| 16 | +* $data->setUTFEncoder('mb'); | |
| 17 | +* | |
| 18 | +**/ | |
| 19 | + | |
| 20 | +/*** | |
| 21 | +* By default rows & cols indeces start with 1 | |
| 22 | +* For change initial index use: | |
| 23 | +* $data->setRowColOffset(0); | |
| 24 | +* | |
| 25 | +**/ | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | +/*** | |
| 30 | +* Some function for formatting output. | |
| 31 | +* $data->setDefaultFormat('%.2f'); | |
| 32 | +* setDefaultFormat - set format for columns with unknown formatting | |
| 33 | +* | |
| 34 | +* $data->setColumnFormat(4, '%.3f'); | |
| 35 | +* setColumnFormat - set format for column (apply only to number fields) | |
| 36 | +* | |
| 37 | +**/ | |
| 38 | + | |
| 39 | +$data->read('jxlrwtest.xls'); | |
| 40 | + | |
| 41 | +/* | |
| 42 | + | |
| 43 | + | |
| 44 | + $data->sheets[0]['numRows'] - count rows | |
| 45 | + $data->sheets[0]['numCols'] - count columns | |
| 46 | + $data->sheets[0]['cells'][$i][$j] - data from $i-row $j-column | |
| 47 | + | |
| 48 | + $data->sheets[0]['cellsInfo'][$i][$j] - extended info about cell | |
| 49 | + | |
| 50 | + $data->sheets[0]['cellsInfo'][$i][$j]['type'] = "date" | "number" | "unknown" | |
| 51 | + if 'type' == "unknown" - use 'raw' value, because cell contain value with format '0.00'; | |
| 52 | + $data->sheets[0]['cellsInfo'][$i][$j]['raw'] = value if cell without format | |
| 53 | + $data->sheets[0]['cellsInfo'][$i][$j]['colspan'] | |
| 54 | + $data->sheets[0]['cellsInfo'][$i][$j]['rowspan'] | |
| 55 | +*/ | |
| 56 | + | |
| 57 | +error_reporting(E_ALL ^ E_NOTICE); | |
| 58 | + | |
| 59 | +for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) { | |
| 60 | + for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { | |
| 61 | + echo "\"".$data->sheets[0]['cells'][$i][$j]."\","; | |
| 62 | + } | |
| 63 | + echo "\n"; | |
| 64 | + | |
| 65 | +} | |
| 66 | + | |
| 67 | + | |
| 68 | +//print_r($data); | |
| 69 | +//print_r($data->formatRecords); | |
| 70 | +?> | ... | ... |
thirdparty/excelreader/jxlrwtest.xls
0 → 100644
No preview for this file type