php - how to speed up PHPExcel reader -
hi expert newbie in php. can tell me speed phpexcel , code ,it read 20000 rows 4 columns . take more 15s. thank much
function upload_fl($files){ $file = $files['excel']; //echo getcwd(); //print_r($file); require 'phpexcel/phpexcel.php'; if(move_uploaded_file($file['tmp_name'],'c:/wamp/www/datatable_017/php/upload/'.$file['name'])){ $data = '../php/upload/'.$file['name']; $objphpexcel = phpexcel_iofactory::load($data); $sheetdata = $objphpexcel->getactivesheet()->toarray(null,true,true,true); //var_dump($sheetdata); echo sizeof($sheetdata); //$writefile = fopen($files['excel']['name'].'.txt','w'); /* foreach($sheetdata $row){ foreach($row $col->$value){ //$value_inser = (is_numeric($value) == true ? ''number',''.$value.'',''':''text','',''.$value.'''); //fwrite($writefile,$cate_set_id[$col].','.$com_id.','.$year.','.$value_insert.'\n'); } } --> fclose($writefile); */ return 'upload/'.$file['name'].'.txt'; }else{ return 'upload failed'; } }//function
if have multiple worksheets, don't need load of them, can limit worksheets reader load using setloadsheetsonly() method. load single named worksheet:
$inputfiletype = 'excel5'; $inputfilename = './sampledata/example1.xls'; $sheetname = 'data sheet #2'; /** create new reader of type defined in $inputfiletype **/ $objreader = phpexcel_iofactory::createreader($inputfiletype); /** advise reader of worksheets want load **/ $objreader->setloadsheetsonly($sheetname); /** load $inputfilename phpexcel object **/ $objphpexcel = $objreader->load($inputfilename);
or can specify several worksheets 1 call setloadsheetsonly() passing array of names:
$inputfiletype = 'excel5'; $inputfilename = './sampledata/example1.xls'; $sheetnames = array('data sheet #1','data sheet #3'); /** create new reader of type defined in $inputfiletype **/ $objreader = phpexcel_iofactory::createreader($inputfiletype); /** advise reader of worksheets want load **/ $objreader->setloadsheetsonly($sheetnames); /** load $inputfilename phpexcel object **/ $objphpexcel = $objreader->load($inputfilename);
if need access part of worksheet, can define read filter identify cells want load:
$inputfiletype = 'excel5'; $inputfilename = './sampledata/example1.xls'; $sheetname = 'data sheet #3'; /** define read filter class implementing phpexcel_reader_ireadfilter */ class myreadfilter implements phpexcel_reader_ireadfilter { public function readcell($column, $row, $worksheetname = '') { // read rows 1 7 , columns e if ($row >= 1 && $row <= 7) { if (in_array($column,range('a','e'))) { return true; } } return false; } } /** create instance of our read filter **/ $filtersubset = new myreadfilter(); /** create new reader of type defined in $inputfiletype **/ $objreader = phpexcel_iofactory::createreader($inputfiletype); /** advise reader of worksheets want load it's more efficient limit sheet loading in manner rather coding read filter **/ $objreader->setloadsheetsonly($sheetname); echo 'loading sheet using filter'; /** tell reader want use read filter we've instantiated **/ $objreader->setreadfilter($filtersubset); /** load rows , columns match our filter $inputfilename phpexcel object **/ $objphpexcel = $objreader->load($inputfilename);
if don't need load formatting information, worksheet data, setreaddataonly() method tell reader load cell values, ignoring cell formatting:
$inputfiletype = 'excel5'; $inputfilename = './sampledata/example1.xls'; /** create new reader of type defined in $inputfiletype **/ $objreader = phpexcel_iofactory::createreader($inputfiletype); /** advise reader want load cell data, not formatting **/ $objreader->setreaddataonly(true); /** load $inputfilename phpexcel object **/ $objphpexcel = $objreader->load($inputfilename);
although note reading raw data won't allow differentiate between date values , floats
if want work large excel files, don't build large php array in memory making toarray()
call, because that's big overhead in memory usage, , there's big cost in performance constant need allocate more , more memory array built..... if you're going process row @ time, use iterators built phpexcel, or use loop access each individual row in turn.
Comments
Post a Comment