PHP import Excel (xls) file into MySQL database _php tutorial

Source: Internet
Author: User
Excel import MySQL database I have organized two ways, one is to use Php-excelreader to import, the other is to convert Excel into a CSV file directly using PHP related functions to import.

Last night a customer contacted me to do the site, the request is to be provided by the customer's Excel file inside the data into the MySQL database, the most common way is to first export the XLS file into a CSV format file, and then in the resolution CSV format file import into the MySQL database. Method compared redundancy, and divided into several steps, very inconvenient, broken bridge today a method is to skip the intermediate link of CSV directly, import Excel file into MySQL database.

First we need to download Php-excelreader This is an open source project, mainly to parse Excel file, download address: http://sourceforge.net/projects/phpexcelreader, unzip after download, The main use of the Excel folder inside the two files reader.php and oleread.php (this file default is Oleread.inc, not clear why, a bunch of e-wen, did not see, directly renamed can be).

Find the following similar code in the reader.php file (the first line is both), change to the correct oleread.php path: require_once ' oleread.php ';

Then create a new PHP file to introduce reader.php with the following code:

copy code

"!--? php
require_once ' Excel/reader. PHP ';
$data = new Spreadsheet_excel_reader ();
$data->setoutputencoding (' GBK ');//set encoding here, generally GBK mode

$data->read (' Book1.xls ');//file path bkjia.c0m

Error_reporting (e_all ^ e_notice);
//Here I just loop out the contents of the Excel file, to the storage, as long as the output of the place, write a MySQL statement can ~
for ($i = 1; $i <= $data->sheets[0][' numrows '); $i + +) {
for ($j = 1; $j <= $data->sheets[0][' numcols '); $j + +) {
echo "". $data->sheets[0][' cells '] [$i] [$j]. "," ;
}
Echo "n";
}
?

Note: Please do not use the Php-excelreader compression package inside the XLS test, broken snow found that the file is not only using Excel can not open, so it is wrong.

Broken Bridge using the above method to resolve a 1.4M data, are displayed normal, so you can rest assured that the use of

Phpexcel compared to resources, but not all Excel can read we can convert to CSV to operate

First confirm what your database is encoded, take utf-8 as an example,
You first open the Excel file and then save it and select Save As. csv file.
Then open the. csv file with a text editor and save as Utf-8 CSV
Then you write PHP can be opened using PHP getcsv (This ensures that you have the fields contained in the resulting parsing error), and then import the results of the resolution into the database.


If it's in CSV format, I don't have to be in such trouble.

code as follows copy code


"!--?
//Connect database file www.bKjia.c 0m
$connect =mysql_connect ("localhost", "admin", "admin") or Die ("link database failed! ");
//Connection database (test)
mysql_select_db ("TESTCG", $connect) or Die (Mysql_error ());

$temp =file ("test.csv");//Connect Excel file, format in order to. csv
for ($i =0; $i {
$string =explode (",", $temp [$i]);//loop to get the value of each row of records in the Excel file
//Insert the value of each row of records in the Excel file into the database
$q = "INSERT into Ceshi (name,num,dom) VALUES (' $ String[0] ', ' $string [1] ', ' $string [2] '); ";
mysql_query ($q) or Die (Mysql_error ());

If (!mysql_error ());
{
echo "successfully imported data!";
}
Echo $string [4]. " n ";
Unset ($string);
}
?

http://www.bkjia.com/PHPjc/632909.html www.bkjia.com true http://www.bkjia.com/PHPjc/632909.html techarticle Excel import MySQL database I have organized two ways, one is to use Php-excelreader to import, the other is to convert Excel into a CSV file directly using PHP related functions to import. Last night ...

  • 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.