thinkPHP 导入栗子 (2)

//导入方法

public function importdata(){
        $file = './a.excel';
        if (!file_exists($file)) {
            $re['msg'] = '模板文件出错,请重新上次模板文件';
        }
        /** Include PHPExcel */
        require_once LIB_ROOT_PATH.'3rdParty/PHPExcel/PHPExcel.php';
        if (!class_exists('PHPExcel_IOFactory')) {
            //PHPExcel类未找到
            $re['msg'] = 'PHPExcel类未找到';
        }
        $inputFileType=\PHPExcel_IOFactory::identify($file);
        if (in_array($inputFileType, array('Excel2007', 'Excel5'))) {
        $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
        $objReader->setReadDataOnly(true);
        $objPHPExcel = $objReader->load($file);
        $sheetData = $objPHPExcel->getActiveSheet();
        $rc = $sheetData->getCellCollection();
        foreach($rc as $val){
            $b = '';
            if($sheetData->getCell("$val")->getDataType()=='s'){
                $b =  $sheetData->getCell("$val")->getValue();
            }else{
                $b =  $sheetData->getCell("$val")->getOldCalculatedValue();
                //$b =  $sheetData->getCell("$val")->getCalculatedValue();
            }
            $b =  $sheetData->getCell("$val")->getValue();
            $calculate = false;
            try {
                $tokens = $objPHPExcel->getCalculationEngine()->parseFormula($b,$sheetData->getCell($val));
                $calculate = true;
            } catch (Exception $e) {
                echo 'PARSER ERROR: ' , $e->getMessage(). PHP_EOL;
                echo 'Parser Stack :-';
                echo PHP_EOL;
            }
            if ($calculate) {
                try {
                    $cellValue = $sheetData->getCell($val)->getCalculatedValue();
                    echo 'Calculated Value is '.$cellValue;
                    echo PHP_EOL;
                } catch (Exception $e) {
                    echo 23;die;
                }
            }
            $fp = fopen(WEB_ROOT_DIR.'../a.txt', 'a+b');
            fwrite($fp, $sheetData->getCell("$val")->getDataType().'-'.$val."\n");
            fclose($fp);
        }
    }else{
        $re['msg'] = '模板文件为空或格式有误';
    }
}

转载于:https://www.cnblogs.com/richardcastle/p/8297388.html

你可能感兴趣的:(php)