3

I've downloaded PHPExcel 1.7.5 Production.

I would like to use setReadDataOnly() and enableMemoryOptimization() as discussed in their forum here and in stackoverflow questions.

However when I use them, I get a Call to undefined method error.

Is there another version or some plugin or library that I have not installed? What do I have to do to access these methods?

$objPHPExcel = PHPExcel_IOFactory::load("data/".$file_name);
$objPHPExcel->setReadDataOnly(true); //Call to undefined method
$objPHPExcel->enableMemoryOptimization(); //Call to undefined method
Community
  • 1
  • 1
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047

2 Answers2

3

You've already identified why setReadDataOnly() isn't working.

enableMemoryOptimization() is not a valid method for any class within PHPExcel. If you wish to optimise memory, you enable one of the cell cacheing methods before loading any file or instantiating a new PHPExcel object.

$inputFileType = 'Excel2007';
$inputFileName = 'testBook.xlsx';

$r = PHPExcel_CachedObjectStorageFactory::initialize(PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp);
if (!$r) {
    die('Unable to set cell cacheing');
}

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);

EDIT

The thread on enableMemoryOptimization() that you link to in your question is from a user who wanted this built into PHPExcel in a way that broke most of the class's functionality, while improving performance for his specific use case (at the expense of most other users); and which would have resulted in two incompatible methods for manipulating data within the library. As such, I rejected it.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

ok, it's just a syntax issue, you have to make a reader, e.g.:

$reader = PHPExcel_IOFactory::createReaderForFile("data/".$file_name);
$reader->setReadDataOnly(true);
//$reader->enableMemoryOptimization(); //Call to undefined method
$objPHPExcel = $reader->load("data/".$file_name);
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • Correct. setReadDataOnly() is a method of the Reader object, not of the PHPExcel object. the static PHPExcel_IOFactory::load() method creates a Reader object and loads the file using that Reader with default settings, but returns the fully loaded PHPExcel object. If you want to read data only, you need to create the reader manually, call the appropriate methods for your settings, then call the load() method manually. – Mark Baker Dec 28 '10 at 12:15