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