Before using Phpexcel to export data to an Excel file, this article describes how to use Phpexcel to import Excel files into a database.
Here is the phpexcel, please download this class library file before doing the following, official website:
Http://www.codeplex.com/PHPExcel
The following is the first time to say the import of the Excel file data format, screenshot as follows:
The following is the specific code that imports the data from the Excel file into the database:
<?php require_once "classes/phpexcel.php";
Require_once "classes/phpexcel/iofactory.php";
Require_once "classes/phpexcel/reader/excel5.php"; $objReader =phpexcel_iofactory::createreader ("Excel5");//use excel2007 for 2007 format $objPHPExcel = $objReader-> Load ($file _url);//$file _url the path $sheet = Excel file = $objPHPExcel->getsheet (0);//get first worksheet $highestRow = $sheet-> Gethighestrow () and/or obtaining the total number $highestColumn = $sheet->gethighestcolumn ();
Gets the total number of columns//loops read an Excel file, reads one, inserts a for ($j =2; $j <= $highestRow; $j + +) {//reads data from the first row $str = ""; for ($k = "A"; $k <= $highestColumn $k + +) {//Read data from column A//This method is simple, but it is not appropriate to combine an array with "\ \", and then split \ To insert a field value into the database, measured in Excel, if a cell's value package Data containing \ \ Import will be empty $str. = $objPHPExcel->getactivesheet ()->getcell ("$k $j")->getvalue (). "
\ \ \/Read cell}//explode: Function splits strings into arrays.
$strs =explode ("\", $STR); $sql = "INSERT into". Tb_prefix. "
Business ' (' username ', ' password ', ' company ', ' Prov ', ' address ', ' btime ', ' phone ', ' email ', ' name ') VALUES ("{$strs [0]}", ' {$strs [1]} ', ' {$strs [2]} ', ' {$stRS[3]} ', ' {$strs [4]} ', ' {$strs [5]} ', ' {$strs [6]} ', ' {$strs [7]} ', ' {$strs [8]} ']; $db->query ($sql);//Here is the Insert database operation} unlink ($file _url); Delete Excel files
In addition, for how to export Excel files using Phpexcel, please refer to this site article:
PHP Export Excel File Summary