3

I am using PHPExcel for the first time. I just wrote a basic code snippet to read one of my Excel files. I want to load the file, iterate through each row and process its contents. However, the function to load the file seems to dump its contents onto the screen.

My code snippet is this:

      include 'lib/Classes/PHPExcel/IOFactory.php';
      $dest = "uploads/";
      $excel = "2012-12-STANDARD.xls";

      $objPHPExcel = PHPExcel_IOFactory::load($dest.$excel);

On running this code, the data in the sheet has been echoed twice onto the screen. First, like a regular echo, and the second is a var_dump.

Here is a sample snippet of the screen output:

DOM ELEMENT: HTML DOM ELEMENT: BODY DOM ELEMENT: P START OF PARAGRAPH: END OF PARAGRAPH: FLUSH CELL: A1 => Type ZipCode City State County AreaCode CityType CityAliasAbbreviation CityAliasName Latitude Longitude TimeZone Elevation CountyFIPS DayLightSaving PreferredLastLineKey ClassificationCode MultiCounty StateFIPS CityStateKey CityAliasCode PrimaryRecord CityMixedCase CityAliasMixedCase StateANSI CountyANSI FacilityCode CityDeliveryIndicator CarrierRouteRateSortation FinanceNumber UniqueZIPName D 18540 ......

array 1 => array 'A' => string 'Type ZipCode City State County AreaCode CityType CityAliasAbbreviation CityAliasName Latitude Longitude TimeZone Elevation CountyFIPS DayLightSaving PreferredLastLineKey ClassificationCode MultiCounty StateFIPS CityStateKey CityAliasCode ........'... (length=4573)

Am I doing something wrong here? Why would the load function echo the contents before accessing it?

4

3 回答 3

3

The cause of the problem is that the IOFactory static load method fails to determine your file's format correctly. You might not want to use it after all, because according to the documentation:

While easy to implement in your code, and you don't need to worry about the file type; this isn't the most efficient method to load a file; and it lacks the flexibility to configure the loader in any way before actually reading the file into a PHPExcel object.

To successfully load the file you can instantiate a Reader explicitly specifying the format. For a file in Excel 2007 format it would be:

$xl_reader = PHPExcel_IOFactory::createReader('Excel2007');
$xl = $xl_reader->load("/tmp/yourfile.xls");

You can also use a Reader's canRead() method to determine wether the reader that you created can load the specified file.

$xl_reader = PHPExcel_IOFactory::createReader('Excel2007');
if ($xl_reader->canRead('/tmp/yourfile.xls')) {
    echo "It's a success! Loading the file..."; 
    $xl = $xl_reader->load('/tmp/yourfile.xls');
    ...
} else {
    echo "Cannot read the file.";
    ...
}
于 2013-01-17T07:08:28.183 回答
1

That code is picking up on the HTML Reader, which still has some of my diagnostics in the code (mea culpa)... if you edit the file Classes/PHPExcel/Reader/HTML.php and comment out every line that contains an echo or a var_dump statement, then it should eliminate the problem.

Coincidence that it's something I was actually working on last night.

Then you can also ask the person who provided you with the file to give you a proper .xls file in future, rather than one which has an extension of .xls but contains html markup rather than a properly formatted BIFF file.

于 2013-01-17T07:25:12.330 回答
0

Try adding the PHPExcel to the PHP include path.

  set_include_path(get_include_path().PATH_SEPARATOR.'lib/Classes/PHPExcel');
  include 'lib/Classes/PHPExcel/IOFactory.php';
  $dest = "uploads/";
  $excel = "2012-12-STANDARD.xls";

  $objPHPExcel = PHPExcel_IOFactory::load($dest.$excel);
于 2013-01-17T01:10:41.150 回答