PHPExcel: the right way to open files

Just let PHPExcel figure out the file type by using PHPExcel_IOFactory::identify()

$filename = 'your_file.xls';
 
require_once 'PHPExcel/Classes/PHPExcel.php';
 
// Create new PHPExcel object
$filetype = PHPExcel_IOFactory::identify($filename);
$objReader = PHPExcel_IOFactory::createReader($filetype);
$objReader->setReadDataOnly(true);  // set this if you don't need to write
$objPHPExcel = $objReader->load($filename);

// go through each sheet
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
  ...
}

2 thoughts on “PHPExcel: the right way to open files

  1. Anonymous says:

    This comment is wrong..

    $objReader->setReadDataOnly(true); // set this if you don’t need to write

    it’s not “read-only” .. it’s “only-data” .. means cell values, but no format, comment, etc.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s