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

牧云@^-^@ 提交于 2021-01-18 19:13:50

问题


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.


回答1:


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.




回答2:


/** 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);


    }

}


来源:https://stackoverflow.com/questions/3873792/how-to-read-multiple-worksheet-from-a-single-excel-file-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!