Using PHP to import Excel (xls) files into MySQL database _php tutorial

Source: Internet
Author: User
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:

Require_once ' excel/reader.php ';
$data = new Spreadsheet_excel_reader ();
$data->setoutputencoding (' GBK '); \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ GBK

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

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 sentence 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

Original: http://www.js8.in/618.html

http://www.bkjia.com/PHPjc/364687.html www.bkjia.com true http://www.bkjia.com/PHPjc/364687.html techarticle last night a customer contacted me to do the site, the request is to be provided by the customer Excel file inside the data into the MySQL database, the most common method is to first export the XLS file as ...

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