PHPExcel_IOFactory

Topics: Developer Forum, User Forum
Jul 8, 2009 at 5:54 PM

I'm using PHPExcel for the first time to read/load files. In this application, I may get Excel2007 files, or I may get older files. So I'm using PHPExcel_IOFactory::load() so that it will figure out the file type and handle it appropriately. However, it doesn't look like I can set any options on the reader it uses. For example, I cannot add a ReadFilter or use setReadDataOnly(). Am I correct in this?

 

If I add the ability to set these in the PHPExcel_IOFactory::load() method, would anyone else be interested? I could also create a PHPExcel_IOFactory::getReader($pFilename) method to find and return the reader without loading the file. These, especially the latter, would be pretty simple derivations of the load() method.

Jul 8, 2009 at 6:05 PM

I went ahead and tried it out, and it's working for me. Perhaps it wouldn't work for CSV or Serialized readers since they don't have the same interface?

 

/**
 * Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution
 *
 * @param 	string 		$pFileName
 * @return	PHPExcel_Reader_IReader
 * @throws 	Exception
 */	
public static function createReaderForFile($pFilename) {
	// Try loading using self::$_autoResolveClasses
	foreach (self::$_autoResolveClasses as $autoResolveClass) {
		$reader = self::createReader($autoResolveClass);
		if ($reader->canRead($pFilename)) {
			return $reader;
		}
	}
		
	throw new Exception("Could not automatically determine PHPExcel_Reader_IReader for file.");
}

 

Usage:

 

try 
{
	$reader = PHPExcel_IOFactory::createReaderForFile($filename);
	$reader->setReadDataOnly(true);
	$spreadsheet = $reader->load($filename);
}
catch (Exception $e)
{
	die('no load');
}

 

 

 

Developer
Jul 9, 2009 at 6:31 PM

This looks good! I have created a work item for this:
http://phpexcel.codeplex.com/WorkItem/View.aspx?WorkItemId=10292