Home >Backend Development >PHP Tutorial >php excel import mysql_PHP tutorial
php excel import mysql First we need to download phpexcelreader, and then look at a simple way to read and display excel data. The second example is to import excel data into mysql data, and the third example is to use php to import csv into mysql database.
php tutorial excel import mysql tutorial
First, we need to download phpexcelreader, and then look at a simple tutorial on reading and displaying excel data. The second example is to import excel data into mysql data, and the third example is to use php to import csv to mysql database tutorial.
*/
require_once './includes/reader.php';
// excelfile($filename, $encoding);
$data = new spreadsheet_excel_reader();
// set output encoding.
$data->setoutputencoding('gbk');
//"data.xls" refers to the excel file to be imported into mysql
$data->read('date.xls');
@ $db = mysql_connect('localhost', 'root', '1234') or
die("could not connect to database.");//Connect to the database
mysql_query("set names 'gbk'");//Output Chinese
mysql_select_db('wenhuaedu'); //Select database
error_reporting(e_all ^ e_notice);
for ($i = 1; $i <= $data->sheets[0]['numrows']; $i++) {
//The following comment (of) for loop prints excel table data
for ($j = 1; $j <= $data->sheets[0]['numcols']; $j++) {
echo $data->sheets[0]['cells'][$i][$j].",";
}
echo "n";//ttp://sourceforge.net/projects/phpexcelreader
//The following is the import program. Note that the mysql database structure must be the same as the xls document structure.
$data = new spreadsheet_excel_reader();
$data->setoutputencoding('utf-8');//Set encoding
$data->read('test.xls');//excel, csv file location
error_reporting(e_all ^ e_notice);for ($i = 2; $i <= $data->sheets[0]['numrows']; $i++) {//插入数据库开始
$sql="insert into {$table}kehu (userid,sortid,danwei,xingming,zuncheng,tel,mobile,fax,email,website,qq,address,postcode,sex,beizhu,addtime) values('$_cookie[wecms_user_id]','$sortid',
'".$data->sheets[0]['cells'][$i][1]."',
'".$data->sheets[0]['cells'][$i][2]."',
'".$data->sheets[0]['cells'][$i][3]."',
'".$data->sheets[0]['cells'][$i][4]."',
'".$data->sheets[0]['cells'][$i][5]."',
'".$data->sheets[0]['cells'][$i][6]."',
'".$data->sheets[0]['cells'][$i][7]."',
'".$data->sheets[0]['cells'][$i][8]."',
'".$data->sheets[0]['cells'][$i][9]."',
'".$data->sheets[0]['cells'][$i][10]."',
'".$data->sheets[0]['cells'][$i][11]."',
'".$data->sheets[0]['cells'][$i][12]."',
'".$data->sheets[0]['cells'][$i][13]."',
'$nowtime')";
$res=$db->query($sql);//插入数据库结束
}
//把csv导入到数据库。
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
include ("connectdb.php");
function insert_data ($id,$summary,$description,$additional_information,$category)
{
$my_query1 = "insert into mantis_bug_text_table (id,description,additional_information)
values ('$id','$description','$additional_information')";
$first = mysql_query($my_query1);
$my_query2 = "insert into mantis_bug_table (id,project_id,summary,bug_text_id) values ('$id','$category','$summary','$id')";
$second = mysql_query($my_query2);
return;
}
$fp = fopen("test.csv","r");
while($data = fgetcsv($fp,'1000',',')){
insert_data ($data[0],$data[1],$data[2],$data[3],$data[4]);
echo "数据导入成功!
";
}
fclose ($fp);
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "程序执行时间:".$time."秒";