php - phpexcel reads long numbers as boolean -
i'm using symfony2.3.4, php5.6.3 , phpexcel 1.8.0.
when tried read excel file works ok cells.
if cell contains large number, when read , show value in html view outputs false.
i tried use custom value binder mark baker instructed here couldn't make work, comes boolean right beginning.
important:
the excels i'm trying load in html downloaded(generated) site , noticed when try open them microsoft excel, first prompts warning window telling user file extension , file format not match, although if choose open anyway, opens fine.
i think that's what's causing problem, i'm sure(i can't contact guys implemented other site's download function) did this:
$objwriter = \phpexcel_iofactory::createwriter($objphpexcel, $ext == 'xlsx' ? 'excel5' : 'excel2007');
when should have done this:
$objwriter = \phpexcel_iofactory::createwriter($objphpexcel, $ext == 'xls' ? 'excel5' : 'excel2007');
making extension , format match, instructed in phpexcel's docs.
if need specific clarification please ask.
my code load file html:
public function uploadaction() { $request = $this->getrequest(); $form = $this->createformbuilder() ->add('file', 'file') ->getform(); if ($request->getmethod() == 'post'){ $form->submit($request); $file = $form['file']; $file->getdata()->move( 'uploads', $form['file']->getdata()->getclientoriginalname()); $ext = pathinfo($file->getdata()->getclientoriginalname(), pathinfo_extension); $name = pathinfo($file->getdata()->getclientoriginalname(), pathinfo_basename); //$objreader = \phpexcel_iofactory::createreader('xlsx' == $ext ? 'excel2007' : 'excel5'); $objreader = \phpexcel_iofactory::createreaderforfile('uploads/' . $name); $objreader->setreaddataonly(true); $objphpexcel = $objreader->load('uploads/' . $name); $activesheet = $objphpexcel->getactivesheet(); $rowiter = $activesheet->getrowiterator(); foreach ($rowiter $key => $row) { $columns = array(); $celliterator = $row->getcelliterator(); $celliterator->setiterateonlyexistingcells(false); foreach ($celliterator $cell) $columns[] = $cell->getcalculatedvalue(); } } }
note: don't know difference between:
$objreader = \phpexcel_iofactory::createreader('xlsx' == $ext ? 'excel2007' : 'excel5');
and
$objreader = \phpexcel_iofactory::createreaderforfile('uploads/' . $name);
i know can't use first because of problem described above files being ill-generated , so. if try use it, browser goes:
the filename uploads/<name>.xls not recognised ole file.
can point me workaround, because it's me on hook , i'm supposed make work somehow. maybe there's nothing wrong files , it's me doing wrong. please help, causing me problems dates 1 step @ time.
edit:
this read function in oleread.php.
i browsing , var_dump-ing hands on.
as can see there 2 var_dumps in code below, output:
string '<div> ' (length=8) string '��ࡱ�' (length=8)
which doesn't happen when try regular .xls file created manually:
string '��ࡱ�' (length=8) string '��ࡱ�' (length=8)
i guessed use better me if helps @ all. again.
public function read($sfilename) { // check if file exists , readable if (!is_readable($sfilename)) { throw new phpexcel_reader_exception("could not open " . $sfilename . " reading! file not exist, or not readable."); } // file identifier // don't bother reading whole file until know it's valid ole file $this->data = file_get_contents($sfilename, false, null, 0, 8); ////var_dumpssssssssssss var_dump($this->data); var_dump(self::identifier_ole); die(); // check ole identifier if ($this->data != self::identifier_ole) { throw new phpexcel_reader_exception('the filename ' . $sfilename . ' not recognised ole file'); } // file data $this->data = file_get_contents($sfilename); // total number of sectors used sat $this->numbigblockdepotblocks = self::_getint4d($this->data, self::num_big_block_depot_blocks_pos); // secid of first sector of directory stream $this->rootstartblock = self::_getint4d($this->data, self::root_start_block_pos); // secid of first sector of ssat (or -2 if not extant) $this->sbdstartblock = self::_getint4d($this->data, self::small_block_depot_block_pos); // secid of first sector of msat (or -2 if no additional sectors used) $this->extensionblock = self::_getint4d($this->data, self::extension_block_pos); // total number of sectors used msat $this->numextensionblocks = self::_getint4d($this->data, self::num_extension_block_pos); $bigblockdepotblocks = array(); $pos = self::big_block_depot_blocks_pos; $bbdblocks = $this->numbigblockdepotblocks; if ($this->numextensionblocks != 0) { $bbdblocks = (self::big_block_size - self::big_block_depot_blocks_pos) / 4; } ($i = 0; $i < $bbdblocks; ++$i) { $bigblockdepotblocks[$i] = self::_getint4d($this->data, $pos); $pos += 4; } ($j = 0; $j < $this->numextensionblocks; ++$j) { $pos = ($this->extensionblock + 1) * self::big_block_size; $blockstoread = min($this->numbigblockdepotblocks - $bbdblocks, self::big_block_size / 4 - 1); ($i = $bbdblocks; $i < $bbdblocks + $blockstoread; ++$i) { $bigblockdepotblocks[$i] = self::_getint4d($this->data, $pos); $pos += 4; } $bbdblocks += $blockstoread; if ($bbdblocks < $this->numbigblockdepotblocks) { $this->extensionblock = self::_getint4d($this->data, $pos); } } $pos = 0; $this->bigblockchain = ''; $bbs = self::big_block_size / 4; ($i = 0; $i < $this->numbigblockdepotblocks; ++$i) { $pos = ($bigblockdepotblocks[$i] + 1) * self::big_block_size; $this->bigblockchain .= substr($this->data, $pos, 4 * $bbs); $pos += 4 * $bbs; } $pos = 0; $sbdblock = $this->sbdstartblock; $this->smallblockchain = ''; while ($sbdblock != -2) { $pos = ($sbdblock + 1) * self::big_block_size; $this->smallblockchain .= substr($this->data, $pos, 4 * $bbs); $pos += 4 * $bbs; $sbdblock = self::_getint4d($this->bigblockchain, $sbdblock * 4); } // read directory stream $block = $this->rootstartblock; $this->entry = $this->_readdata($block); $this->_readpropertysets(); }
the difference between
$objreader = \phpexcel_iofactory::createreader('xlsx' == $ext ? 'excel2007' : 'excel5');
and
$objreader = \phpexcel_iofactory::createreaderforfile('uploads/' . $name);
the first trusting extension correct actual format of file, file extension of .xlsx
officeopenxml-format
file or extension of .xls
biff-format
file, , telling phpexcel use appropriate reader.
this isn't problem unless isn't (for example) html markup in file .xls
or .xlsx
extension.... you're selecting wrong reader actual format of file; , ms excel telling message "file extension , file format not match"
the second using phpexcel's identify()
method work out format file really is (irrespective of claims based on false extension), , selecting appropriate reader format.
edit
unsure how large large numbers are, i'll take @ html reader , see if can identify why should giving boolean false
instead of actual numeric value
Comments
Post a Comment