php excel格式(csv)資料匯入匯出(樣本)

來源:互聯網
上載者:User
  1. $fname = $_FILES['MyFile']['name'];
  2. $do = copy($_FILES['MyFile']['tmp_name'],$fname);
  3. if ($do){
  4. echo"匯入資料成功
    ";
  5. }else{
  6. echo "";
  7. }
  8. ?>
  9. error_reporting(0);//匯入CSV格式的檔案
  10. $connect=mysql_connect("localhost","a0530093319","123456") or die("could not connect to database");
  11. mysql_select_db("a0530093319",$connect) or die (mysql_error());
  12. $fname = $_FILES['MyFile']['name'];
  13. $handle=fopen("$fname","r");
  14. while($data=fgetcsv($handle,10000,",")){
  15. $q="insert into test (code,name,date) values ('$data[0]','$data[1]','$data[2]')";
  16. mysql_query($q) or die (mysql_error());
  17. }
  18. fclose($handle);
  19. ?>
複製代碼

用php將資料庫匯出成excel,測試成功。

測試代碼:

  1. $DB_Server = "localhost";
  2. $DB_Username = "root";
  3. $DB_Password = "";
  4. $DB_DBName = "ishop";
  5. $DB_TBLName = "oi_mall_payment";
  6. $savename = date("YmjHis");
  7. $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");
  8. mysql_query("Set Names gbk"); $file_type = "vnd.ms-excel";
  9. $file_ending = "xls";
  10. header("Content-Type: application/$file_type;charset=big5");
  11. header("Content-Disposition: attachment; filename=".$savename.".$file_ending");
  12. //header("Pragma: no-cache");
  13. $now_date = date("Y-m-j H:i:s");
  14. $title = "資料庫名:$DB_DBName,資料表:$DB_TBLName,備份日期:$now_date";
  15. $sql = "Select * from $DB_TBLName";
  16. $ALT_Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database");
  17. $result = @mysql_query($sql,$Connect) or die(mysql_error());
  18. echo("$title\n");
  19. $sep = "\t";
  20. for ($i = 0; $i < mysql_num_fields($result); $i++){
  21. echo mysql_field_name($result,$i)."\t";
  22. }
  23. print("\n");
  24. $i = 0;
  25. while($row = mysql_fetch_row($result)){
  26. $schema_insert = "";
  27. for($j=0; $jif(!isset($row[$j]))
  28. $schema_insert .= "NULL".$sep;
  29. elseif ($row[$j] != "")
  30. $schema_insert .= "$row[$j]".$sep;
  31. else
  32. $schema_insert .= "".$sep;
  33. }
  34. $schema_insert = str_replace($sep."$", "", $schema_insert);
  35. $schema_insert .= "\t";
  36. print(trim($schema_insert));
  37. print "\n";
  38. $i++;
  39. }
  40. return (true);
  41. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.