代码之家  ›  专栏  ›  技术社区  ›  nik

如何在php中从单个excel文件中读取多个工作表?

  •  4
  • nik  · 技术社区  · 16 年前

    我有一个excel工作表有三个工作表,我在从第二个工作表获取记录时遇到问题。

    三个工作表都有不同类型的记录和不同的字段,我试图用谷歌搜索,但找不到解决方案。

    2 回复  |  直到 11 年前
        1
  •  3
  •   Nik    16 年前

    你可以参考一下,

    http://phpexcel.codeplex.com/

    这是一个很好的项目开发的excel阅读器和作家。你可以用它来做你的项目。它具有excel所需的所有必要方法。

        2
  •  3
  •   Ravi Prakash    10 年前

    /** 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);
    
    
        }
    
    }
    
    推荐文章