Public function Getexcel ($ file ){
$ File = $ _ FILES ['file'] ['tmp _ name'];
Yii: import ("special. extensions. PHPExcel ");
$ PHPExcel = new PHPExcel ();
$ PHPReader = new PHPExcel_Reader_Excel2007 ();
If (! $ PHPReader-> canRead ($ file )){
$ PHPReader = new PHPExcel_Reader_Excel5 ();
If (! $ PHPReader-> canRead ($ file )){
Return false;
}
}
$ PHPExcel = $ PHPReader-> load ($ file );
$ CurrentSheet = $ PHPExcel-> getSheet (0); // read the first worksheet
$ AllColumn = $ currentSheet-> getHighestColumn (); // obtain the largest column number.
$ AllRow = $ currentSheet-> getHighestRow (); // obtain the total number of rows.
/** Output from the second row because the first row column name in the excel table */
$ Arr = array ();
For ($ currentRow = 4; $ currentRow <= $ allRow; $ currentRow ++ ){
/** Output from column */
For ($ currentColumn = 'a'; $ currentColumn <= $ allColumn; $ currentColumn ++ ){
$ Val = $ currentSheet-> getCellByColumnAndRow (ord ($ currentColumn)-65, $ currentRow)-> getValue ();/* ord () convert characters into decimal numbers */
/** If garbled characters are output, use the iconv function to convert the Output Content. Convert gb2312 encoding to UTF-8 encoding as follows */
// $ Arr [$ currentRow] [] = iconv ('utf-8', 'gb2312 ', $ val). "\ t ";
$ Arr [$ currentRow] [] = trim ($ val );
}
}
// Delete all empty rows
Foreach ($ arr as $ key => $ vals ){
$ Tmp = '';
Foreach ($ vals as $ v ){
$ Tmp. = $ v;
}
If (! $ Tmp) unset ($ arr [$ key]);
}
Return $ arr;
}
The sample code is not imported to the database. In fact, it is simple to generate an SQL statement after reading and then execute it to import it to the database. This section will not introduce it.