PHP Imports Excel into the database and the database data to Excel, and exports excel data. PHP Imports Excel into the database and the database data to Excel. export excel data 1. to import data, you need to use the Excel-readable components. There are also good online components. here, we will share with PHP the Excel import database and database data exported to Excel, excel data exported
1. import
To import data, you must use a component that can read Excel. There are also good components on the internet. here, I will share my use: Download extraction code: vxyn. (Note that there is a reference relationship between the two files)
SetOutputEncoding ('utf-8'); // you can specify the character encoding $ data-> read ("data/Excel/{$filename}.xls") after reading the Excel content "); $ db = mysql_connect ('localhost', 'username', 'password') or die ("cocould not connect to database. "); // connect to the database mysql_query (" set names 'uft8' "); // output the Chinese mysql_select_db ('database name '); // select the database error_reporting (E_ALL ^ E_NOTICE); for ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++) {echo $ data-> sheets [0] ['cells '] [$ I] [number of columns]; // you can set Insert the values of a row of corresponding columns into the database, for example,/* $ SQL = "insert" table name "values (corresponding item ...) "; mysql_query ($ SQL); error judgment can be added */}?>
In short, you can read the value of the corresponding column $ data-> sheet [0] [row] [column] in each row in the table, and the insert operation is easy.
II. export
You can use the MIME protocol to easily export table files without relying on any components. Set the header in the following format to export the Excel file and download it in the browser.
Header ('content-type: text/html; charset = utf-8 '); header ("Content-type: application/vnd. ms-excel; charset = UTF-8 "); // application/vnd. ms-excel specifies the output Excel format header ("Content-Disposition: filename .xls"); // The name of the output table
The complete code is as follows:
Here \ t is actually a change, \ n is a line feed. Set the php file link in a webpage. when you click it, the browser automatically saves the uploaded stream as an Excel file.
Tip 1. you need to use a component that can read Excel to import data. There are also good components on the internet. here, we will share them...