PHP Excel import

public  function ImportFile () 
    { 
        $ File = Request () -> File ( 'File' );
         $ the params = $ the this -> request-> param (); // can obtain parameters 
        IF (! $ File ) {
             $ the this -> error ( 'Please select the import file " ); 
        } 
        // save the path file 
        $ info = $ file -> Move (root_path' public 'the DS' uploads'... ); 
     // get the file path
$ filePath = root_path .. 'public' the DS 'uploads' the DS... $ info -> getSaveName ();
     // find the file
IF (!is_file($filePath)) { $this->error('无相关记录'); }

     //===========开始
$PHPReader = new \PHPExcel_Reader_Excel2007(); if (!$PHPReader->canRead($filePath)) { $PHPReader = new \PHPExcel_Reader_Excel5(); if (!$PHPReader->canRead($filePath)) { $PHPReader = new \PHPExcel_Reader_CSV(); if (!$PHPReader-> canRead ( $ filePath )) { $ the this -> error ( 'unknown format' ); } } } $ PHPExcel = $ PHPReader -> Load ( $ filePath ); // load file $ currentSheet = $ PHPExcel -> getSheet ( 0); // read the file in the first worksheet $ allColumn = $ currentSheet -> getHighestDataColumn (); // obtain the maximum column number $ allRow = $ currentSheet -> getHighestRow (); // get a total number of line $ maxColumnNumber = \ PHPExcel_Cell :: columnIndexFromString (allColumn $ ); // Get the number of columns // get uploaded to excel in the first row as the column header for ( $ currentRow =. 1; $ currentRow <=. 1; ++ $ currentRow ) { for ( $ also currentColumn = 0 ; $ also currentColumn < $ maxColumnNumber ; ++ $ also currentColumn ) { $ Val = $ currentSheet -> getCellByColumnAndRow ( $ also currentColumn , $ currentRow ) -> the getValue (); $ Fields [] = $ Val ; } }
     // save data for total
$ INSERT = []; // number of cycles of rows: currentRow data is started from the second row for ( $ currentRow = 2; $ currentRow <= $ allRow ; ++ $ currentRow ) { $ values = []; // rows // cycle per row for ( $ also currentColumn = 0; $ also currentColumn < $ maxColumnNumber ; ++ $ also currentColumn ) { $ Val = $ currentSheet -> getCellByColumnAndRow ( $ also currentColumn , $ currentRow ) - >getValue(); $values[] = is_null($val) ? '' : $val; } $row = []; // 合并 $temp = array_combine($fields, $values);          foreach ($temp as $k => $v) { if ($k !== '') { $k = trim($k); //获得key COL $ = Self :: excel2dbColhandler ( $ K ); // pass the column header name Excel returns the corresponding database field IF ( $ COL ) { $ Row [ $ COL ] = TRIM ( $ V ); } } } IF ( $ Row ) { $ Row [ 'the create_time'] = DATE ( 'Ymd H: I: S' ); $ INSERT [] = $ Row ; } } // Get wish to add Model $ nowModel =the this $ -> getNowModel ( $ params [ 'tbName' ]); $ nowModel -> saveAll ( $ INSERT ); unset ( $ info ); // begin releasing variable unlink ( $ filePath ); // delete a file }

 

Guess you like

Origin www.cnblogs.com/xinchenhui/p/11460086.html