PHPExcel reads excel files and imports them to the database. Under normal circumstances, if we want to import data from the excel database to the mysql database, we have no good way to achieve it, but with PHPExcel out of control, everything becomes simple. Under normal circumstances, if we want to import data from the excel database to the mysql database, we have nothing to do, but with PHP Excel out of control, everything becomes simple.
This code is used in thinkphp.
1. introduce classes
| The code is as follows: |
|
|
Vendor ('phpexcel. PHPExcel '); // introduces the extension class. yes/
Vendor ('phpexcel. PHPExcel. IOFactory ');
Vendor ('phpexcel. PHPExcel. Reader. excel5 ');
$ Excel_file = ROOT_PATH. "/public/Uploads /". $ publicity_bankdata_mod-> where ("id = ". $ data ['id'])-> getField ('Excel _ file ');
// Dump ($ excel_file); exit;
$ ObjReader = PHPExcel_IOFactory: createReader ('excel5'); // use excel2007 for 2007 format
$ ObjPHPExcel = $ objReader-> load ($ excel_file); // $ uploadfile
$ Sheet = $ objPHPExcel-> getSheet (0 );
$ HighestRow = $ sheet-> getHighestRow (); // gets the total number of rows.
$ HighestColumn = $ sheet-> getHighestColumn (); // gets the total number of columns
$ Arr_result = array ();
$ Strs = array ();
$ Strs_datas = array ();
$ Succ_result = 0;
$ Error_result = 0; |
The above looks a little messy. next I will write a complete class
| The code is as follows: |
|
Set_time_limit (20000 );
Ini_set ('memory _ limit ','-1 ');
Require_once './PHPExcel. php ';
Require_once './PHPExcel/IOFactory. php ';
Require_once './PHPExcel/Reader/Excel5.php ';
// Use pdo to connect to the database
$ Dsn = "mysql: host = localhost; dbname = alumni ;";
$ User = "root ";
$ Password = "";
Try {
$ Dbh = new PDO ($ dsn, $ user, $ password );
$ Dbh-> query ('set names utf8 ;');
} Catch (PDOException $ e ){
Echo "connection failed". $ e-> getMessage ();
}
// Parameter binding operation for pdo
$ Stmt = $ dbh-> prepare ("insert into alumni (gid, student_no, name) values (: gid,: student_no,: name )");
$ Stmt-> bindParam (": gid", $ gid, PDO: PARAM_STR );
$ Stmt-> bindParam (": student_no", $ student_no, PDO: PARAM_STR );
$ Stmt-> bindParam (": name", $ name, PDO: PARAM_STR );
$ ObjReader = new PHPExcel_Reader_Excel5 (); // use excel2007
$ ObjPHPExcel = $ objReader-> load('bks.xls '); // specify the file
$ Sheet = $ objPHPExcel-> getSheet (0 );
$ HighestRow = $ sheet-> getHighestRow (); // gets the total number of rows.
$ HighestColumn = $ sheet-> getHighestColumn (); // gets the total number of columns
For ($ j = 1; $ j <= 10; $ j ++)
{
$ Student_no = $ objPHPExcel-> getActiveSheet ()-> getCell ("A". $ j)-> getValue (); // The first student ID
$ Name = $ objPHPExcel-> getActiveSheet ()-> getCell ("B". $ j)-> getValue (); // name of the second column
$ Gid = $ objPHPExcel-> getActiveSheet ()-> getCell ("C". $ j)-> getValue (); // The third column gid
}
// Insert the obtained excel content to the database
$ Stmt-> execute ();
?> |
Php-excel-reader:
1. dump (), which can output the excel content in html format:
Echo $ data-> dump (true, true );
2. Save the excel data to the array and use $ data-> sheet to print the following:
| The code is as follows: |
|
|
Array
(
[0] => Array
(
[Maxrow] => 0
[Maxcol] => 0
[NumRows] => 5
[NumCols] => 4
[Cells] => Array
(
[1] => Array
(
[1] => No.
[2] => Name
[3] => age
[4] => Student ID
)
[2] => Array
(
[1] => 1
[2] => Xiaohong
[3] => 22
[4] => a1000
)
[3] => Array
(
[1] => 2
[2] => Xiao Wang
[3] => 33
[4] => a1001
)
[4] => Array
(
[1] => 3
[2] => Xiao Hei
[3] => 44
[4] => a1002
)
[5] => Array
(
[2] =>
[3] => www.phpddt.com
)
)
[CellsInfo] => Array
(
[1] => Array
(
[1] => Array
(
[XfIndex] => 15
)
[2] => Array
(
[XfIndex] => 15
)
[3] => Array
(
[XfIndex] => 15
)
[4] => Array
(
[XfIndex] => 15
)
)
[2] => Array
(
[1] => Array
(
[String] => 1
[Raw] => 1
[Rectype] => unknown
[Format] => % s
[FormatIndex] => 0
[FontIndex] => 0
[FormatColor] =>
[XfIndex] => 15
)
[2] => Array
(
[XfIndex] => 15
)
[3] => Array
(
[String] => 22
[Raw] => 22
[Rectype] => unknown
[Format] => % s
[FormatIndex] => 0
[FontIndex] => 0
[FormatColor] =>
[XfIndex] => 15
)
[4] => Array
(
[XfIndex] => 15
)
)
[3] => Array
(
[1] => Array
(
[String] => 2
[Raw] => 2
[Rectype] => unknown
[Format] => % s
[FormatIndex] => 0
[FontIndex] => 6
[FormatColor] =>
[XfIndex] => 23
)
[2] => Array
(
[XfIndex] => 23
)
[3] => Array
(
[String] => 33
[Raw] => 33
[Rectype] => unknown
[Format] => % s
[FormatIndex] => 0
[FontIndex] => 6
[FormatColor] =>
[XfIndex] => 23
)
[4] => Array
(
[XfIndex] => 23
)
)
[4] => Array
(
[1] => Array
(
[String] => 3
[Raw] => 3
[Rectype] => unknown
[Format] => % s
[FormatIndex] => 0
[FontIndex] => 0
[FormatColor] =>
[XfIndex] => 15
)
[2] => Array
(
[XfIndex] => 15
)
[3] => Array
(
[String] => 44
[Raw] => 44
[Rectype] => unknown
[Format] => % s
[FormatIndex] => 0
[FontIndex] => 0
[FormatColor] =>
[XfIndex] => 15
)
[4] => Array
(
[XfIndex] => 15
)
)
[5] => Array
(
[2] => Array
(
[XfIndex] => 15
)
[3] => Array
(
[XfIndex] => 24
[Hyperlink] => Array
(
[Flags] => 23
[Desc] => www. bKjia. c0m
[Link] => http://www.phpddt.co
)
)
)
)
)
[1] => Array
(
[Maxrow] => 0
[Maxcol] => 0
[NumRows] => 0
[NumCols] => 0
)
[2] => Array
(
[Maxrow] => 0
[Maxcol] => 0
[NumRows] => 0
[NumCols] => 0
)
) |
Excel gets out of control, which makes everything easy. This generation...