要下载phpexcelreader,然后再看一个简单的把excel数据读取并显示出,是把excel数据导入到mysql数据,利用php 导入 csv到mysql数据库.
PHP实例代码如下:
- require_once './includes/reader.php';
-
- $data = new spreadsheet_excel_reader();
-
- $data->setoutputencoding('gbk');
-
- $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 ($j = 1; $j <= $data->sheets[0]['numcols']; $j++) {
- echo $data->sheets[0]['cells'][$i][$j].",";
- }
- echo "n";
-
-
-
- $data = new spreadsheet_excel_reader();
- $data->setoutputencoding('utf-8');
- $data->read('test.xls');
- 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);
-
-
- }
-
- 代码如下 复制代码
- 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."秒";
|