PHP Imports Excel into the database and export database data to Excel, excel data export _ PHP Tutorial

Source: Internet
Author: User
Tags import database
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...

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