Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read multiple worksheet from a single excel file in php?

Tags:

php

phpexcel

I have an excel sheet having three worksheets, I am having trouble in fetching records from second worksheet.

All three worksheet is having different kind of records and with different fields, I try to google it but couldn't find a solution.

like image 790
nik Avatar asked Oct 06 '10 14:10

nik


2 Answers

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';
$file = '/media/sf_E_DRIVE/om/newData.xlsx';
$inputFileType = PHPExcel_IOFactory::identify($file);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($file);
$objWorksheet = $objPHPExcel->getActiveSheet();
$CurrentWorkSheetIndex = 0;

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    // echo 'WorkSheet' . $CurrentWorkSheetIndex++ . "\n";
    echo 'Worksheet number - ', $objPHPExcel->getIndex($worksheet), PHP_EOL;
    $highestRow = $worksheet->getHighestDataRow();
    $highestColumn = $worksheet->getHighestDataColumn();
    $headings = $worksheet->rangeToArray('A1:' . $highestColumn . 1,
        NULL,
        TRUE,
        FALSE);

    for ($row = 2; $row <= $highestRow; $row++) {

        $rowData = $worksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
        $rowData[0] = array_combine($headings[0], $rowData[0]);
        print_r($rowData);


    }

}
like image 187
Ravi Prakash Avatar answered Nov 13 '22 05:11

Ravi Prakash


You can refer,

http://phpexcel.codeplex.com/

This is good project developed for excel reader and writer. You can use it for your project. It has all necessary methods that are required for excel.

like image 3
Nik Avatar answered Nov 13 '22 06:11

Nik