How to import EXCEL into MYSQL using PHP

Source: Internet
Author: User
Tags php sample code

I searched the internet for a lot of such information and found that the excel file was saved as a csv file, and then imported from the csv file. Here is an example of importing an excel file to mysql. It took me one night to test the function. No garbled characters are displayed in simplified and traditional Chinese characters. It is very easy to use.

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

Note:

PHP imports EXCEL to the MYSQL test environment: the MYSQL database adopts utf8 encoding. The imported EXCEL file is in the xls format. After testing, the xlsx format [excel 2007] is also OK.

In this text, the red mark is worth attention. Replace it with your configured data, such as database configuration. Run http: // localost/test. php to import data.

Below is the detailed code I posted, where test. php is the test file I wrote, reader. php and oleread. inc files are downloaded from the web site provided above.

1. PHP sample code for importing EXCEL into MYSQL: test. php

 
 
  1. <? Php
  2. Require_once 'reader. php ';
  3. // ExcelFile ($ filename, $ encoding );
  4. $ Data = new Spreadsheet_Excel_Reader ();
  5. // Set output Encoding.
  6. $ Data-> setOutputEncoding ('gbk ');
  7. // ”Data.xls refers to the excel file to be imported to mysql.
  8. $ Data-> read('data.xls ');
  9. @ $ Db = mysql_connect ('localhost', 'root', '123') or
  10. Die ("cocould not connect to database."); // connect to the database
  11. Mysql_query ("set names 'gbk'"); // output Chinese
  12. Mysql_select_db ('mydb'); // select a database
  13. Error_reporting (E_ALL ^ E_NOTICE );
  14. For ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++ ){
  15. // Print the excel table data in a for loop with the following comments
  16. /*
  17. For ($ j = 1; $ j <= $ data-> sheets [0] ['numcols']; $ j ++ ){
  18. Echo ". $ data-> sheets [0] ['cells '] [$ I] [$ j]." ",";
  19. }
  20. Echo "n ";
  21. */
  22. // The following code inserts [3 fields] of the excel table data into mysql,
    Modify the following code based on the number of fields in your excel table!
  23. $ SQL = "INSERT INTO test VALUES ('".
  24. $ Data-> sheets [0] ['cells '] [$ I] [1]. "', '".
  25. $ Data-> sheets [0] ['cells '] [$ I] [2]. "', '".
  26. $ Data-> sheets [0] ['cells '] [$ I] [3]. "')";
  27. Echo $ SQL. '<br/> ';
  28. $ Res = mysql_query ($ SQL );
  29. }
  30. ?>

The above describes how PHP imports EXCEL into MYSQL. I hope many users can help me.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.