Introduction to importing EXCEL files in PHP

Source: Internet
Author: User
There are several open source files imported from PHP to EXCEL. I use phpExcelReader. Use the following code: $ filename $ _ FILES [excel] [name]; $ tmp_name $ _ FILES [excel] [tmp_name]; save the uploaded file if (move_uploaded_f introduction to importing an EXCEL file in PHP

PHP has several open source files for importing excel files. I use phpExcelReader.

The method is as follows:

$ Filename = $ _ FILES ['Excel '] ['name']; $ tmp_name = $ _ FILES ['Excel'] ['tmp _ name']; // Save the uploaded file if (move_uploaded_file ($ tmp_name, 'Upload /'. $ filename) {inputExcel ($ filename);} // call Readerrequire_once 'reader. php '; // create Reader $ data = new Spreadsheet_Excel_Reader (); // set the text output encoding $ data-> setOutputEncoding ('utf-8 '); // read the Excel file $ data-> read ('upload /'. $ filename ); // $ data-> sheets [0] ['numrows '] indicates the number of rows in Excel. // $ data-> sheets [0] ['numcols'] indicates the number of columns in Excel. for ($ I = 2; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++) {// obtain data from cells $ a = 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 obtain the data. it is worth noting that:

1. do not include square brackets (parentheses) or [brackets] in an excel table, because it is directly saved to an array when obtaining data, so the square brackets (parentheses) and [brackets] will affect the array format and cause an error. my approach is to replace them with full-angle (parentheses) and [brackets ].

2. if you want to interact the values retrieved from excel with the database, I will escape mysql_real_escape_string (data) to prevent errors caused by symbols when executing the database.

3. when there is a large amount of data in excel, when inserting data into the database, pay attention to the length of SQL statements, the length limit of PHP strings and php. the configuration in ini is related to the computer memory.

4. $ data-> setOutputEncoding ('utf-8') can solve Chinese garbled characters.

?

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.