PHPExcel ---- get / import excel file data [simple and practical demo2]

For excel export / save, please refer to another blog post: PHPExcel - save / export excel file data [simple and practical demo]

First, you need to have PHPExcel class: Portal
Just save the Classes in it.
Reference PHPExcel.php in the project

First look at the excel data source table you want to obtain:

Implementation code:

    public function getExcelContent(){
		//Import PHPExcel
    	//As it is a local development test, the practical one is a local file (try not to name it in Chinese)
        $filePath = "F:\project\build\yonghe_pc\application\Admin\Controller\personnel.xlsx";
		//     echo 'file exists';
		// }
        $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));//Determine import table suffix format
        //Use different reading methods according to different formats
        if ($extension == 'xlsx') {
            $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
        }else if ($extension == 'xls'){
            $objReader =\PHPExcel_IOFactory::createReader('Excel5');
            print_r('The format text import is not supported temporarily');
        $objPHPExcel =$objReader->load($filePath, $encode = 'utf-8');
        $sheet =$objPHPExcel->getSheet(0);//Activate first sheet table
        $highestRow = $sheet->getHighestRow();//Get total number
        $highestColumn =$sheet->getHighestColumn(); //Get total columns
        //Verify that the header data is aligned
        $A1 = $objPHPExcel->getActiveSheet()->getCell("A1")->getValue();
        $B1 = $objPHPExcel->getActiveSheet()->getCell("B1")->getValue();
        $C1 = $objPHPExcel->getActiveSheet()->getCell("C1")->getValue();
        if($A1!='Full name'||$B1!='Gender'||$C1!='Nation'){
            print_r('EXCEL Text data does not correspond');
            $row['name'] =$objPHPExcel->getActiveSheet()->getCell("A" .$i)->getValue();
            $row['sex'] =$objPHPExcel->getActiveSheet()->getCell("B" .$i)->getValue();
            $row['nation'] = $objPHPExcel->getActiveSheet()->getCell("C". $i)->getValue();
            $data[] = $row;
        /**Negligible, to show the effect*/
        header("Content-type: text/html; charset=utf-8");
        print_r("Row number:{$highestRow}<br>");
        print_r("Column number:{$highestColumn}<br>");
        print_r("Table header:{$A1}---{$B1}---{$C1}<br>");
        //Get the content of Excel. Next, write the data table and store it

Result printing

ERRO : Could not open XXX.xls for reading! File does not exist,

Solution suggestions:

Several possible error reasons are suggested:
1. Check whether the path of xls file is correct: file [exists ("path/to/file.xls");
2. If you are testing locally, see if the xls file is opened or occupied by other software.
3. It is recommended to open the XLS file of the local server instead of the XLS file on the remote server. /user/public/docs/filex.xls instead of
I hope it helps you.

Keywords: Excel PHP

Added by transformationstarts on Wed, 25 Dec 2019 17:10:11 +0200