PHP匯入Excel到MySQL的方法,php匯入excelmysql

來源:互聯網
上載者:User

PHP匯入Excel到MySQL的方法,php匯入excelmysql

PHP-ExcelReader,: http://sourceforge.net/projects/phpexcelreader

注意點:

reader.php 中的下面這行要修改 
1、將 require_once ‘Spreadsheet/Excel/Reader/OLERead.php’;改為 require_once ‘oleread.inc’;

2、require_once ‘oleread.inc’也可以和拷貝出來放到reader的檔案最前面合并為一個檔案

3、$data->setOutputEncoding(‘utf-8’)也可以這樣用,我的項目都是utf-8的編碼,開始用的就是上面的寫法$data->setOutputEncoding(’CP936′);結果是,匯入資料庫老是說編碼錯誤。最後設成utf-8解決了。

上傳Excel介面代碼:

upExcel.php

<?phpheader("Content-Type:text/html;charset=utf-8"); //設定字型編碼,避免中文亂碼?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>匯入測試</title> </head> <body> <script> function import_check(){     var f_content = form1.file.value;     var fileext=f_content.substring(f_content.lastIndexOf("."),f_content.length)         fileext=fileext.toLowerCase()      if (fileext!='.xls')         {          alert("對不起,匯入資料格式必須是xls格式檔案哦,請您調整格式後重新上傳,謝謝 !");                     return false;         } } </script> <table width="98%" border="0" align="center" style="margin-top:20px; border:1px solid #9abcde;">     <form id="form1" name="form1" enctype="multipart/form-data" method="post" action="insert.php">         <tr>             <td width="18%" height="50"> 選擇你要匯入的資料表:</td>             <td width="82%"><label> <input name="file" type="file" id="file" size="50" /> </label>                 <label> <input name="button" type="submit" class="nnt_submit" id="button" value="匯入資料"  onclick="import_check();"/>                 </label> </td>         </tr> </form></table> </body> </html> 


實現上傳寫入資料庫的代碼insert.php

<?phpheader("Content-Type:text/html;charset=utf-8"); //設定字型編碼,避免中文亂碼require_once("../db_config.php");require_once 'Excel/reader.php'; error_reporting(E_ALL ^ E_NOTICE); //ini_set('max_execution_time', '100');   //php已耗用時間為30秒,當資料量大時,會出現逾時而導致無法全部匯入的情況。if($_POST){ $Import_TmpFile = $_FILES['file']['tmp_name']; //$Import_TmpFile = 'http://test998-merchphoto.stor.sinaapp.com/test.xls'; $data = new Spreadsheet_Excel_Reader(); $data->setOutputEncoding('utf-8'); $data->read($Import_TmpFile);     $count =0;    for($i= 1; $i<= $data->sheets[0]['numRows']; $i++){ $sql= "INSERT INTO test_xls(id,tm,name) VALUES('". $data->sheets[0]['cells'][$i][1]."','". $data->sheets[0]['cells'][$i][2]."','". $data->sheets[0]['cells'][$i][3]."')"; echo $sql."</br>";if(mysql_query($sql)){$count++;}}     echo "<script>alert('成功匯入".$count."條資料');</script>"; } ?> 


mysql 表:



excel:



參考資料:

1、http://jason2016.blog.51cto.com/892969/289411

2、http://www.cnblogs.com/phpzxh/archive/2009/09/16/1568133.html

3、http://blog.csdn.net/china_skag/article/details/7098473



怎使用php實現將資料從excel表匯入到mysql中?

把excel 改為 cvs檔案
<?
//串連資料庫檔案
$connect=mysql_connect("localhost","admin","admin") or die("連結資料庫失敗!");
//串連資料庫(test)
mysql_select_db("testcg",$connect) or die (mysql_error());
$temp=file("test.csv");//串連EXCEL檔案,格式為了.csv
for ($i=0;$i <count($temp);$i++)
{
$string=explode(",",$temp[$i]);//通過迴圈得到EXCEL檔案中每行記錄的值
//將EXCEL檔案中每行記錄的值插入到資料庫中
$q="insert into ceshi (name,num,dom) values('$string[0]','$string[1]','$string[2]');";
mysql_query($q) or die (mysql_error());

if (!mysql_error());
{
echo " 成功匯入資料!";
}
echo $string[4]."\n";
unset($string);
}
?>
 
怎通過PHP實現EXCEL與MYSQL之間的匯入匯出

下面是我最初研究產生Excel檔案的代碼,後來我用物件導向重寫,你先看看這個代碼吧,很簡單的,想要物件導向的再聯絡!
<?php
$DB_Server = "localhost";
$DB_Username = "root";
$DB_Password = "root";
$DB_DBName = "lost_property";
$DB_TBLName = "wuping";
$savename = date("YmjHis");
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");
mysql_query("Set Names 'gbk'");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type;charset=big5");
header("Content-Disposition: attachment; filename=".$savename.".$file_ending");
//header("Pragma: no-cache");

$now_date = date("Y-m-j H:i:s");
$title = "資料庫名:$DB_DBName,資料表:$DB_TBLName,備份日期:$now_date";

$sql = "Select * from $DB_TBLName";
$ALT_Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database");
$result = @mysql_query($sql,$Connect) or die(mysql_error());

echo("$title\n");
$sep = "\t";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.