问题
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?
回答1:
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);
回答2:
You can do it easier than getting list of worksheet names:
$objPHPExcel->setActiveSheetIndex(2);
$worksheet = $objPHPExcel->getActiveSheet();
Loads #2 (third) worksheet.
回答3:
An easiest way for those who is still struggling with this -
//include library
include('path/to/PHPExcel/IOFactory.php');
//load the file
$objPHPExcel = PHPExcel_IOFactory::load('your/path/for/excel/file');
//get the worksheet of your choice by its name
$worksheet = $objPHPExcel->getSheetByName('Name of sheet');
#and your work goes here...
回答4:
//load library - EXCEL
$this->load->library('excel');
$objPHPExcel = PHPExcel_IOFactory::load('./folder/exceldata.xls');
Individual worksheets can be accessed by name, or by their index position in the workbook. The index position represents the order that each worksheet "tab" is shown when the workbook is opened in MS Excel (or other appropriate Spreadsheet program).
To access a sheet by name, use the getSheetByName() method, specifying the name of the worksheet that you want to access.
//Retrieve the worksheet called 'Worksheet 1'
$objPHPExcel->getSheetByName('Worksheet 1');
To access a sheet by its index, use the getSheet() method. Note that sheets are indexed from 0.
//Retrieve the **1st 'tab' worksheet** e.g. called 'Sheet 1'
$worksheet = $objPHPExcel->getSheet(0);
//Retrieve the **2nd 'tab' worksheet** e.g. called 'Sheet 2'
$worksheet = $objPHPExcel->getSheet(1);
This all can be achieved by help of @Mark Baker 's PHPExcel Library. Thanks.
来源:https://stackoverflow.com/questions/23081669/read-only-specific-sheet