php excel導入mysql 先們要下載phpexcelreader,然後再看一個簡單的把excel數據讀取並顯示出,實例二是把excel數據導入到mysql數據,實例三是利用php 導入 csv到mysql數據庫。
php教程 excel導入mysql教程
先們要下載phpexcelreader,然後再看一個簡單的把excel數據讀取並顯示出,實例二是把excel數據導入到mysql數據,實例三是利用php 導入 csv到mysql數據庫教程。
*/
require_once './includes/reader.php';
// excelfile($filename, $encoding);
$data = new spreadsheet_excel_reader();
// set output encoding.
$data->setoutputencoding('gbk');
//"data.xls"是指要導入到mysql中(的)excel文件
$data->read('date.xls');
@ $db = mysql_connect('localhost', 'root', '1234') or
die("could not connect to database.");//連接數據庫
mysql_query("set names 'gbk'");//輸出中文
mysql_select_db('wenhuaedu'); //選擇數據庫
error_reporting(e_all ^ e_notice);
for ($i = 1; $i <= $data->sheets[0]['numrows']; $i++) {
//以下注釋(的)for循環打印excel表數據
for ($j = 1; $j <= $data->sheets[0]['numcols']; $j++) {
echo $data->sheets[0]['cells'][$i][$j].",";
}
echo "n";//ttp://sourceforge.net/projects/phpexcelreader
//下面為導入程序,注明,mysql數據庫結構要與xls文檔的結構一樣就可以了。
$data = new spreadsheet_excel_reader();
$data->setoutputencoding('utf-8');//設置編碼
$data->read('test.xls');//excel,csv文件位置
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 "<font color = #ff0000 size = 20>數據導入成功!</font><br><br>";
}
fclose ($fp);
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "程序執行時間:".$time."秒";