Home >Backend Development >PHP Tutorial >PHP example: Use PHPExcel to import Excel2003 documents and Excel2007 documents into the MySQL database, _PHP tutorial

PHP example: Use PHPExcel to import Excel2003 documents and Excel2007 documents into the MySQL database, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:44:361141browse

PHP Example: Use PHPExcel to import Excel2003 documents and Excel2007 documents into the MySQL database.

If you want to use phpExcelReader to import Excel data into the mysql database, please click on this article to view it.

Use phpExcelReader to import Excel data into mysql database.

Below we introduce another method to import Excel to MySQL database.

1. Go to the official website http://phpexcel.codeplex.com/ to download the PHPExcel class library. I am currently using version 1.7.9. As shown in the picture:

PHP example: Use PHPExcel to import Excel2003 documents and Excel2007 documents into the MySQL database, _PHP tutorial

2. Upload the code directly.

(1), conn.php file (no need to introduce this, you know it):

PHP CodeCopy content to clipboard
  1. $mysql=mysql_connect("localhost","root","root");
  2. mysql_select_db("test",$mysql);
  3. mysql_query("set names GBK");

(2), HTML page part: index.php file (form submission page):

XML/HTML CodeCopy content to clipboard
  1. form name="form2" method="post" enctype="multipart/form-data" action="upload_excel.php"> input type="hidden" name="leadExcel" value="true">
  2. table align="center" width="90% " border="0"> ;
  3. tr> td
  4. >  input
  5. type="file" name="inputExcel">input
  6. type=
  7. "submit" name="import" value="Import data"> td>
  8. tr
  9. >
  10. table
  11. >
  12. form
  13. > (3), form processing handler part: upload_excel.php file:
  14. PHP CodeCopy content to clipboard
    1. include("conn.php");
    2. include("function.php");
    3. if($_POST ['import']=="Import data"){
    4. $leadExcel=$_POST['leadExcel'];
    5. if($leadExcel == "true")
    6. {
    7. //echo "OK";die();
    8. //Get the uploaded file name
    9.  $filename = $HTTP_POST_FILES['inputExcel'] ['name']; 
    10. //The name of the temporary file uploaded to the server
    11.  $tmp_name = $_FILES ['inputExcel']['tmp_name']; 
    12.       
    13.  $msg = uploadFile($filename,$tmp_name); 
    14. echo $msg;
    15. }
    16. }

    (4), function part: function.php file:

    PHP CodeCopy content to clipboard
    1. //Import Excel file
    2. function uploadFile($file,$filetempname)
    3. {
    4. //The upload file storage path set by yourself
    5. $filePath = 'upFile/';
    6. $str = "";
    7. //The path below should be modified according to your PHPExcel path
    8. set_include_path('.'.PATH_SEPARATOR .'E:phpAppServwww91ctcStudyPHPExcelImportSQl2 PHPExcel' .PATH_SEPARATOR .get_include_path());
    9.  
    10. require_once 'PHPExcel.php';
    11. require_once 'PHPExcelIOFactory.php';
    12. //require_once 'PHPExcelReaderExcel5.php';//excel 2003
    13.  require_once 'PHPExcelReaderExcel2007.php';//excel 2007 
    14.  $filename=explode(".",$file);//Make the uploaded file name into an array based on ".". 
    15. $time=date("y-m-d-H-i- s");//Go to the current upload time
    16. $filename [0]=$time;//Replace with file name t
    17. $name=implode (".",$filename); //Uploaded file name
    18.  $uploadfile=$filePath.$name;//Uploaded file name address 
    19. //move_uploaded_file() function moves the uploaded file to a new location. If successful, return true, otherwise return false.
    20. $result=move_uploaded_file($filetempname,$uploadfile);//If uploaded to the current directory
    21. if($result) //If the file upload is successful, execute the import excel operation
    22. {
    23. // $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2003
    24.  $objReader = PHPExcel_IOFactory::createReader('Excel2007');//use excel2003 and 2007 format 
    25. // $objPHPExcel = $objReader->load($uploadfile); //This can easily cause httpd to crash
    26. $objPHPExcel = PHPExcel_IOFactory::load($uploadfile);//Just change it to this way of writing
    27. $sheet = $objPHPExcel- >getSheet(0);
    28. $highestRow = $sheet- >getHighestRow(); // Get the total number of rows
    29. $highestColumn = $sheet- >getHighestColumn(); // Get the total number of columns
    30. //Loop through the excel file, read one item, insert one item
    31.  for($j=2;$j$highestRow;$j) 
  15.                                                    
  16.  
  17. for($k='A';$k$highestColumn;$k ) 
  18.                                                                     
  19.            
  20. $str .= iconv(
  21. 'utf-8','gbk',$objPHPExcel->getActiveSheet()->getCell("$k $j")->getValue()).'\';//Read cell
  22.                                                                
  23. //explode: Function splits a string into an array.
  24. $strs =& ​​nbsp;explode("\",$str);
  25.                                                   
  26. //var_dump ($strs);
  27. //die();
  28.                $sql =&n bsp;"INSERT INTO z_test_importexcel(duty_date,name_am,name_pm) VALUES ('
  29. ".$strs[0]."','".$ strs[1]."','".$strs[2]."')"; 🎜> //echo $ sql; mysql_query ("set names GBK");//This is the specified database character set, usually placed after connecting to the database
  30. if(! mysql_query($sql)){
  31. & nbsp; return false;
  32.                                                                       $str =&n bsp;"
  33. ";
  34. }  
  35.   unlink ($uploadfile); //Delete the uploaded excel file
  36. $msg = "Import successful!
  37. ";
  38. }else{
  39. $msg = "Import failed!"; }
  40. return $msg;
  41. }
  42. Regarding this function, I refer to the blogger’s article at http://blog.csdn.net/grassroots20 11/article/details/8104604. However,
  43. I think there is a problem with the blogger’s writing method
  44. , at least, what I use $objPHPExcel = $objReader->load ($uploadfile);
  45. This sentence will appear when running:

Perhaps the PHPExcel class library has been officially upgraded later, and the calling method needs to be corrected. The author has not studied the details.

3. Through the above steps, readers can prepare an xls and xlsx document respectively. The system operation effect is:

Attachment download: Complete DEMO download (with PHPExcel class library) Attachment download: Complete DEMO download (with PHPExcel class library)

PHP example: Use PHPExcel to import Excel2003 documents and Excel2007 documents into the MySQL database, _PHP tutorial

http://www.bkjia.com/PHPjc/1048745.html

www.bkjia.com

PHP example: Use PHPExcel to import Excel2003 documents and Excel2007 documents into the MySQL database, _PHP tutorialtrue

http: //www.bkjia.com/PHPjc/1048745.html

TechArticle

PHP Example: Use PHPExcel to import Excel2003 documents and Excel2007 documents into the MySQL database. If you want to use phpExcelReader to import Excel data To the mysql database, please click on this article...
Statement:
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