Php method for importing Excel to MySQL

Source: Internet
Author: User
Tags import database

Php-excelreader,: http://sourceforge.net/projects/phpexcelreader

Note the point :

The following line in the reader.php to be modified
1, will require_once ' spreadsheet/excel/reader/oleread.php '; instead of require_once ' oleread.inc ';

2, require_once ' Oleread.inc ' can also be copied out to the reader in front of the file merged into a file

3. $data->setoutputencoding (' Utf-8 ') can also be used in this way, my project is Utf-8 coding, began to use the above writing $data->setoutputencoding (' cp936′ '); As a result, the import database always says coding errors. Finally set up to Utf-8 solved.

Upload Excel Interface code:

upexcel.php

<?phpheader ("Content-type:text/html;charset=utf-8"); Set font encoding to avoid Chinese garbled characters? ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 


Implementing code that uploads to the database insert.php

<? Phpheader ("Content-type:text/html;charset=utf-8"); Set the font encoding to avoid Chinese garbled require_once (".. /db_config.php "); require_once ' excel/reader.php '; Error_reporting (e_all ^ e_notice);   Ini_set (' max_execution_time ', ' 100 '); PHP runs for 30 seconds, and when the amount of data is large, a time-out occurs that prevents all imports. 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 (' successful import '). $count." Data ');</script> "; }?> 


MySQL table:



Excel:



Resources:

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 method for importing Excel to MySQL

Related Article

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.