Reputation: 2647
I am using PHPExcel for import data from excel file.
In my current code I am reading sheet data with sheet name and getting results in array. Now I want to read sheet data with sheet id with bellow function.
$objPHPExcel->setActiveSheetIndex(2)
but this is not working with my code.
Here is my code work:
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** PHPExcel_IOFactory */
require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$inputFileType = 'Excel2007';
$inputFileName = 'hello.xlsx';
$sheetname = 'Sample'; // I DON'T WANT TO USE SHEET NAME HERE
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setLoadSheetsOnly($sheetname);
$objPHPExcel = $objReader->load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
echo ' Highest Column '. $getHighestColumn = $objPHPExcel->setActiveSheetIndex()->getHighestColumn(); // Get Highest Column
echo ' Get Highest Row '. $getHighestRow = $objPHPExcel->setActiveSheetIndex()->getHighestRow(); // Get Highest Row
echo "<pre>";
print_r($sheetData);
echo "</pre>";
For Mark Baker:
I am getting this result in my question code when sheet name is Sample (Sheet ID: 2)
Array
(
[1] => Array
(
[A] => OrderDate
[B] => Region
[C] => Rep
[D] => Item
[E] => Units
[F] => Unit Cost
[G] => Total
)
[2] => Array
(
[A] => 1/6/13
[B] => East
[C] => Jones
[D] => Pencil
[E] => 95
[F] => 1.99
[G] => 189.05
)
[3] => Array
(
[A] => 1/23/13
[B] => Central
[C] => Kivell
[D] => Binder
[E] => 50
[F] => 19.99
[G] => 999.5
)
After changing I am getting this result:
PHPExcel Object
(
[_uniqueID:PHPExcel:private] => 543b9dab3f6da
[_properties:PHPExcel:private] => PHPExcel_DocumentProperties Object
(
[_creator:PHPExcel_DocumentProperties:private] => comp-30
[_lastModifiedBy:PHPExcel_DocumentProperties:private] =>
[_created:PHPExcel_DocumentProperties:private] => 1158364800
[_modified:PHPExcel_DocumentProperties:private] => 1413191554
[_title:PHPExcel_DocumentProperties:private] =>
[_description:PHPExcel_DocumentProperties:private] =>
[_subject:PHPExcel_DocumentProperties:private] =>
[_keywords:PHPExcel_DocumentProperties:private] =>
[_category:PHPExcel_DocumentProperties:private] =>
[_manager:PHPExcel_DocumentProperties:private] =>
[_company:PHPExcel_DocumentProperties:private] => Microsoft Corporation
[_customProperties:PHPExcel_DocumentProperties:private] => Array
(
[KSOProductBuildVer] => Array
(
[value] => 1033-9.1.0.4674
[type] => s
)
)
)
[_security:PHPExcel:private] => PHPExcel_DocumentSecurity Object
(
[_lockRevision:PHPExcel_DocumentSecurity:private] =>
[_lockStructure:PHPExcel_DocumentSecurity:private] =>
[_lockWindows:PHPExcel_DocumentSecurity:private] =>
[_revisionsPassword:PHPExcel_DocumentSecurity:private] =>
[_workbookPassword:PHPExcel_DocumentSecurity:private] =>
)
[_workSheetCollection:PHPExcel:private] => Array
(
[0] => PHPExcel_Worksheet Object
Any Idea?
Thanks.
Upvotes: 1
Views: 16311
Reputation: 2647
After help of Mark Baker
Now I am able to get sheet data with sheet ID.
Here is full code work:
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** PHPExcel_IOFactory */
require_once dirname(__FILE__) . '../PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$inputFileType = 'Excel2007';
$inputFileName = 'hello.xlsx';
$sheetIndex = 2;
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$sheetnames = $objReader->listWorksheetNames($inputFileName);
$objReader->setLoadSheetsOnly($sheetnames[$sheetIndex]);
$objPHPExcel = $objReader->load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
echo "<pre>";
print_r($sheetData);
echo "</pre>";
?>
Thanks Mark Baker :)
Upvotes: 4
Reputation: 212402
If you want to load all the worksheets in the workbook, then change
$inputFileType = 'Excel2007';
$inputFileName = 'hello.xlsx';
$sheetname = 'Sample'; // I DON'T WANT TO USE SHEET NAME HERE
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setLoadSheetsOnly($sheetname);
$objPHPExcel = $objReader->load($inputFileName);
to
$inputFileType = 'Excel2007';
$inputFileName = 'hello.xlsx';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
EDIT
Based on your comments:
$inputFileType = 'Excel2007';
$inputFileName = 'hello.xlsx';
$sheetIndex = 2;
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$sheetnames = $objReader->listWorksheetNames($inputFileName);
$objReader->setLoadSheetsOnly($sheetname);
$objReader->setLoadSheetsOnly($sheetnames[$sheetIndex]);
$objPHPExcel = $objReader->load($inputFileName);
This will get the sheetname used for the sheet at sheetindex 2, and then use that sheetname to load only that sheet
Upvotes: 6