Home Backend Development PHP Tutorial PHP excel format (csv) data import and export (example)

PHP excel format (csv) data import and export (example)

Jul 25, 2016 am 08:55 AM

  1. <?php
  2. $fname = $_FILES['MyFile']['name'];
  3. $do = copy($_FILES['MyFile']['tmp_name'],$fname);
  4. if ($do){
  5. echo"导入数据成功<br>";
  6. }else{
  7. echo "";
  8. }
  9. ?>
  10. <form ENCTYPE="multipart/form-data" ACTION="<?php echo"".$_SERVER["PHP_SELF"].""; ?>" METHOD="POST">
  11. <p>导入CVS数据 <input NAME="MyFile" TYPE="file"> <input VALUE="提交" TYPE="submit">
  12. </p>
  13. </form>
  14. <?php
  15. error_reporting(0);//导入CSV格式的文件
  16. $connect=mysql_connect("localhost","a0530093319","123456") or die("could not connect to database");
  17. mysql_select_db("a0530093319",$connect) or die (mysql_error());
  18. $fname = $_FILES['MyFile']['name'];
  19. $handle=fopen("$fname","r");
  20. while($data=fgetcsv($handle,10000,",")){
  21. $q="insert into test (code,name,date) values ('$data[0]','$data[1]','$data[2]')";
  22. mysql_query($q) or die (mysql_error());
  23. }
  24. fclose($handle);
  25. ?>
复制代码

用php将数据库导出成excel,测试成功。

测试代码:

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


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles