首页 > 代码库 > PHP 读取EXCEL

PHP 读取EXCEL

PHPExcel 自己下载。

PHP读取EXCEL

public function import_Excel($file_name){
        include_once LIB_ROOT_PATH."3rdParty/phpexcel/PHPExcel.php";
        $objReader = PHPExcel_IOFactory::createReader(‘Excel2007‘);
        $objPHPExcel = $objReader->load($file_name,$encode=‘utf-8‘);
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();//取得总行数
        $highestColumn = $sheet->getHighestColumn();//取得总列数
        $data = array();
        for($i=2;$i<=$highestRow;$i++){
            for($j=‘A‘;$j<=$highestColumn;$j++){
                $data[$i][] = $objPHPExcel->getActiveSheet()->getCell("$j$i")->getValue();
            }
        }
        return $data;
    }

 

  

PHP导出EXCEL

public function data_excel(){ include_once LIB_ROOT_PATH."3rdParty/phpexcel/PHPExcel.php";
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue(‘A1‘, ‘相近字‘)  
            ->setCellValue(‘B1‘, ‘可能的字‘);
        $i = 0;
        foreach ($temp as $key => $value) {
            $i++;
            $objPHPExcel->getActiveSheet(0)->setCellValue(‘A‘.($i+1), $key);
            $objPHPExcel->getActiveSheet(0)->setCellValue(‘B‘.($i+1), $value);
        }
        //  sheet命名  
        $objPHPExcel->getActiveSheet()->setTitle(‘易错字统计‘);
        $objPHPExcel->setActiveSheetIndex(0);  
        // excel头参数  
        header(‘Content-Type: application/vnd.ms-excel‘);  
        header(‘Content-Disposition: attachment;filename="(‘.date(‘Ymd-His‘).‘).xlsx"‘);  //日期为文件名后缀  
        header(‘Cache-Control: max-age=0‘);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007‘);  //excel5为xls格式,excel2007为xlsx格式  
        $objWriter->save(‘php://output‘); 
        exit; 
    } 

 

PHP 读取EXCEL