PHP imports Excel (xls) files into MySQL database

Source: Internet
Author: User
Tags parse error php file php and mysql database

Last night a customer contacted me to do a website, put forward the requirement is to provide customers with the Excel file inside the data into the MySQL database, the most commonly used method is to export the XLS file in CSV format file, and then in the resolution CSV file import to the MySQL database. method is more redundant, and several steps, very inconvenient, broken bridge snow today introduced a method is to skip the middle link of CSV, directly to the Excel file into the MySQL database.

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

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

Then create a new PHP file to introduce reader.php, the code is as follows:

  code is as follows copy code

<?php
require_once ' excel/reader.php ';
$data = new Spreadsheet_excel_reader ();
$data->setoutputencoding (' GBK ');//set encoding here, typically GBK mode

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

Error_reporting (e_all ^ e_notice);
//Here I only output the contents of the Excel file, to the storage, as long as the output of the place, write a section of the 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][' CE Lls ' [$i] [$j]. "", ";
 }
 echo "n";
}

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

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

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

First make sure what your database is coded, take utf-8 as an example,
You first open the Excel file, then save, and select Save as a. csv file.
Then open the. csv file with a text editor and save as a utf-8 CSV
Then you write PHP that can be opened using PHP getcsv (This ensures that you have a field that contains a parse error) and then imports the parsed results into the database.


If it's in CSV format, I wouldn't have to bother.

  code is as follows copy code



//Connection database file Www.111cn.net
$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 an Excel file in the format of. csv
for ($i =0; $i <count ($temp); $i + +)
{
$string =explode (", ", $temp [$i])//By looping the value of each row in the Excel file
//inserting the value of each row 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);
}

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.