PHP exports Excel Import Database and database data to Excel

Source: Internet
Author: User
Tags import database

One. Import

Import needs to use the components that can read Excel, there are better components on the net, here to share I use: Download extract code: Vxyn. (Note that two files have a citation relationship)

 
  Setoutputencoding (' Utf-8 ');    Sets the character encoding output after reading the contents of Excel    $data->read ("data/excel/{$filename}.xls");    $db = mysql_connect (' localhost ', ' username ', ' password ') or Die ("Could does connect to database."); Connect database       mysql_query ("Set names ' Uft8 '");//Output Chinese       mysql_select_db (' database name ');//Select 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];        Here you can insert the value of the corresponding column of each row into the database, such as:/        *        $sql = "Insert" Table name "values (corresponding items ...)";        mysql_query ($sql);        Can be wrongly judged        */     }  ?>        

In short, you can read the values of the corresponding column $data->sheets[0][[column] in each row in the table, and the insert operation will be done.

Two. Export

Export can easily export tabular files without relying on any components using the HTTP protocol. You can export Excel by setting the header in the following format, while the browser downloads

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= table file name. xls"); Table name of the output

The complete code is as follows:

 
  

In fact, \ t is a change of grid, \ nthe line. Set up a link to this PHP file in a Web page, and the browser will automatically save the streamed stream as an Excel file when clicked.

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