Home >Backend Development >PHP Tutorial >PHP import excel file mysql database method_PHP tutorial
It should be said that it introduces the use of phpexcel plug-in to realize the import and import function of the database. This article mainly tells you how to import excel into the mysql database.
Download first
Download the phpexcel file at: phpexcel.codeplex.com/
Find the following similar code in the reader.php file (the first line is) and change it to the correct oleread.php path: require_once 'oleread.php';
Then create a new php file and introduce reader.php,
The code is as follows:
The code is as follows | Copy code | ||||||||
$data = new Spreadsheet_Excel_Reader(); $data->setOutputEncoding('gbk');//Set the encoding here, usually in gbk mode
|
The code is as follows | Copy code |
require_once 'phpexcel/Classes/PHPExcel.php'; require_once 'phpexcel/Classes/PHPExcel/IOFactory.php'; require_once 'phpexcel/Classes/PHPExcel/Reader/Excel5.php'; $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format $objPHPExcel = $objReader->load($filename); //$filename can be an uploaded file or a specified file $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); // Get the total number of rows $highestColumn = $sheet->getHighestColumn(); // Get the total number of columns $k = 0; //Loop through the excel file, read one item, and insert one item for($j=2;$j<=$highestRow;$j++)<🎜> {<🎜> <🎜> $a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//Get the value of column A $b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//Get the value of column B $sql = "INSERT INTO table VALUES(".$a.",".$b.")"; Mysql_query($sql); } |
Import code example cvs into database
Import csv into database.
The code is as follows
|
Copy code
|
||||
function getmicrotime(){ |