Reputation: 12433
I am using PHPExcel plugin in codeigniter and my requirement is i have a excel file with multiple sheets
by using below code i am trying to get some data from excel sheets in looping with getAllSheets,getCellCollection is always pointing to the last only
example my out
Array ( [0] => Array ( [timing] => Array ( [0] => 10:30 PM [1] => 7:30 AM ) [header] => Array ( [4] => Array ( [D] => 2017-03-27 [E] => 2017-03-28 [F] => 2017-03-29 [G] => 2017-03-30 [H] => 2017-03-31 ) ) [arr_data] => Array ( [5] => Array ( [A] => Damojipurapu Venkata Raghavendra Praveen [B] => Array ( [0] => Railway new colony [1] => 9052932938 ) [C] => 08:50 PM [G] => OFF ) [6] => Array ( [A] => Seerapu Venkata Rao [B] => Array ( [0] => Maharanipeta [1] => 9701964583 ) [C] => 09:00 PM ) [7] => Array ( [A] => Challa Vinod Kumar [B] => Array ( [0] => Chinna Walter [1] => 9963463234 ) [C] => 09:10 PM [D] => 6:00AM [E] => OFF ) [8] => Array ( [A] => Satish Kumar [B] => Array ( [0] => Maruthi Paradise, Opp Vilas Rest,PM palem [1] => 9731347726 ) [C] => 09:20 PM ) [9] => Array ( [A] => Bandi Saketh Reddy [B] => Array ( [0] => Madhurwada [1] => 8501999751 ) [C] => 09:40 PM [H] => OFF ) [10] => Array ( [A] => D Selvaa Murugan [B] => Array ( [0] => Car Shed Madhurwada [1] => 9043287087 ) [C] => 09:50 PM ) ) ) [1] => Array ( [timing] => Array ( [0] => 10:30 PM [1] => 7:30 AM ) [header] => Array ( [4] => Array ( [D] => 2017-03-27 [E] => 2017-03-28 [F] => 2017-03-29 [G] => 2017-03-30 [H] => 2017-03-31 ) ) [arr_data] => Array ( [5] => Array ( [A] => Damojipurapu Venkata Raghavendra Praveen [B] => Array ( [0] => Railway new colony [1] => 9052932938 ) [C] => 08:50 PM [G] => OFF ) [6] => Array ( [A] => Seerapu Venkata Rao [B] => Array ( [0] => Maharanipeta [1] => 9701964583 ) [C] => 09:00 PM ) [7] => Array ( [A] => Challa Vinod Kumar [B] => Array ( [0] => Chinna Walter [1] => 9963463234 ) [C] => 09:10 PM [D] => 6:00AM [E] => OFF ) [8] => Array ( [A] => Satish Kumar [B] => Array ( [0] => Maruthi Paradise, Opp Vilas Rest,PM palem [1] => 9731347726 ) [C] => 09:20 PM ) [9] => Array ( [A] => Bandi Saketh Reddy [B] => Array ( [0] => Madhurwada [1] => 8501999751 ) [C] => 09:40 PM [H] => OFF ) [10] => Array ( [A] => D Selvaa Murugan [B] => Array ( [0] => Car Shed Madhurwada [1] => 9043287087 ) [C] => 09:50 PM ) ) ) )
and my code is
try {
$fileType = PHPExcel_IOFactory::identify($file["tmp_name"]);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($file["tmp_name"]);
$all_data=array();
foreach ($objPHPExcel->getAllSheets() as $sheet) {
//echo $sheet->getTitle()."<br>";
$cell_collection = $sheet->getCellCollection();
//print_r($cell_collection);
foreach ($cell_collection as $cell) {
$c=$objPHPExcel->getActiveSheet()->getCell($cell);
$column = $c->getColumn();
$row = $c->getRow();
$data_value = $c->getValue();
if(strstr($data_value,'=')==true)
{
$data_value = $c->getOldCalculatedValue();
}
if ($row == 3) {
if(count($data_value)>0){
$a = explode("Shift: ",$data_value);
$data["timing"] =array_map('trim',explode("-",$a[1]));
}
}else if ($row == 4){
if($column!='A'&&$column!='B'&&$column!='C'){
if (PHPExcel_Shared_Date::isDateTime($c)) {
$data_value = PHPExcel_Shared_Date::ExcelToPHPObject($c->getValue())->format('Y-m-d');
}
if(count($data_value)>0)
$data["header"][$row][$column] = $data_value;
}
}else if ($row > 4){
if($column=='B'){
if(count($data_value)>0)
$data_value=array_map('trim',explode("/",$data_value));
}
if($column=='C'){
if (PHPExcel_Shared_Date::isDateTime($c)) {
$data_value = PHPExcel_Shared_Date::ExcelToPHPObject($c->getValue())->format('h:i A');
}
}
if(count($data_value)>0)
$data["arr_data"][$row][$column] = $data_value;
}
}
array_push($all_data, $data);
$data=array();
}
//echo "<pre>";
print_r($all_data);
exit;
}catch (Exception $ex) {}
Upvotes: 1
Views: 851
Reputation: 12433
Thank you, I found Solution
Actually i need to get data $sheet not with $objPHPExcel->getActiveSheet(), by doing this it causes to always read data from the sheet where we last saved that excel file, I just change this line
$c=$objPHPExcel->getActiveSheet()->getCell($cell);
to
$c=$sheet->getCell($cell);
then its fixed for me
Once agian thanks to all
Upvotes: 1