php 读取excel表数据

 /**
     * 读取excel
     * @$filename  绝对路径
     * @return array
     */
    function read_excel($filename)
    {
        try {
            vendor('PHPExcel.PHPExcel');
            //兼容2003和2007版本
            $objReader = PHPExcel_IOFactory::createReader('Excel5');
            if (!$objReader->canRead($filename)) {
                $objReader = PHPExcel_IOFactory::createReader('Excel2007');
                if (!$objReader->canRead($filename)) {
                    return false;
                }
            }

            $objReader->setReadDataOnly(true);

            $objPHPExcel = $objReader->load($filename);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
            $highestColumn = $objWorksheet->getHighestColumn();
            $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
            $excelData = array();
            if ($highestRow > 20001) {
                throw new Exception('至多一次导入20000条数据');
            }
            for ($row = 1; $row <= $highestRow; $row++) {
                for ($col = 0; $col < $highestColumnIndex; $col++) {
                    $excelData[$row][] = (string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
                }
            }
        } catch (Exception $e) {
            $msg = $e->getLine() . ' ' . $e->getMessage() . ' ' . $e->getFile();
            return $msg;
        }
        return $excelData;
    }

你可能感兴趣的:(PHP功能,php)