On the import of Excel data from PHP

Source: Internet
Author: User
Talking about PHP import Excel file

There are several open source files for PHP to import into Excel, and I use Phpexcelreader.

Here's how to go directly to the code:

$filename = $_files [' Excel '] [' name ']; $tmp _name = $_files [' Excel '] [' tmp_name '];//Save the upload file if (move_uploaded_file ($tmp _n Ame, ' upload/'. $filename)) {    inputexcel ($filename);} Call readerrequire_once ' reader.php ';//create reader$data = new Spreadsheet_excel_reader ();//Set Text output encoding $data-> Setoutputencoding (' utf-8 ');//Read Excel file $data->read (' upload/'. $filename);//$data->sheets[0][' numrows '] Number of lines for Excel//$data->sheets[0][' numcols '] for Excel columns for ($i = 2; $i <= $data->sheets[0][' numrows '); $i + +) {    //from single Get data $a prompt    = mysql_real_escape_string ($data->sheets [0] [' cells '] [$i] [1]);    $b = mysql_real_escape_string ($data->sheets [0] [' cells '] [$i] [2]);    $c = mysql_real_escape_string ($data->sheets [0] [' cells '] [$i] [3]);    $d = mysql_real_escape_string ($data->sheets [0] [' cells '] [$i] [4]);}

?

The function of this code is to upload an Excel file and get the data, it is worth explaining:

1, the Excel table does not have half-width (parentheses) and [brackets], because it gets the data is directly saved to an array, so that the (parentheses) and [brackets] will affect the format of the array, and raise an error, I would rather replace them with the perfect angle (parentheses) and "brackets."

2, if you want to remove the value from Excel and the database interaction, I here mysql_real_escape_string (data) directly escaped, to prevent the execution of the database because of some symbols caused by errors.

3, when the amount of data in Excel is large, when inserting into the database, note the length of the SQL statement, the length of the PHP string is limited to the configuration in php.ini and computer memory.

4, $data->setoutputencoding (' utf-8 ') can solve the problem of Chinese garbled.

?

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