Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read only specific sheet

Tags:

php

xls

phpexcel

I am trying to read just one sheet from an xls document and I have this:

 $objPHPExcel = $objReader->load('daily/' . $fisierInbound);
 $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
 foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {

            $worksheetTitle     = $worksheet->getTitle();
            $highestRow         = $worksheet->getHighestRow(); // e.g. 10
            $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
            $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
            $dataCalls = $worksheet->getCellByColumnAndRow(2, 2)->getValue();
            $dataSubstr = substr($dataCalls, 53);


        } 

The problem is that it reads all the sheets of the file.

Any ideas?

like image 851
ardb Avatar asked Apr 15 '14 10:04

ardb


People also ask

How do I restrict access to certain tabs in Excel?

Select File > Info. Select Protect Workbook, point to Restrict Permission by People, and then select Restricted Access. In the Permissions dialog box, select Restrict permission to this workbook, and then assign the access levels that you want for each user.


1 Answers

As described in the PHPExcel User Documentation - Reading Spreadsheet Files document in the /Documentation folder (section 5.2. entitled"Reading Only Named WorkSheets from a File"):

If you know the name of the sheet that you want to read.

$inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example1.xls'; 
$sheetname = 'Data Sheet #2'; 

/**  Create a new Reader of the type defined in $inputFileType  **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 
/**  Advise the Reader of which WorkSheets we want to load  **/ 
$objReader->setLoadSheetsOnly($sheetname); 
/**  Load $inputFileName to a PHPExcel Object  **/ 
$objPHPExcel = $objReader->load($inputFileName); 

If you don't know the name of the worksheet in advance, you can get a list of all worksheets before loading the file

$inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example1.xls'; 

/**  Create a new Reader of the type defined in $inputFileType  **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 
/**  Read the list of worksheet names and select the one that we want to load  **/
$worksheetList = $objReader->listWorksheetNames($inputFileName)
$sheetname = $worksheetList[0]; 

/**  Advise the Reader of which WorkSheets we want to load  **/ 
$objReader->setLoadSheetsOnly($sheetname); 
/**  Load $inputFileName to a PHPExcel Object  **/ 
$objPHPExcel = $objReader->load($inputFileName); 
like image 98
Mark Baker Avatar answered Sep 21 '22 01:09

Mark Baker