Export database data to an Excel file using native PHP

Source: Internet
Author: User

Recently encountered a requirement in your work, you need to export the data in the database to an Excel file, and download the Excel file. Because have not done before, so Baidu a bit,

Most of the online is to use the Phpexcel class to manipulate Excel files, which also to download the class to use, and I just want to use the native PHP, not so troublesome, fortunately

Also have the netizen to say about the native PHP to generate Excel file method, actually is very simple, below put me to combine the code of the net the practice of oneself to share a bit.

In general, the operation of this kind of data is through the user on the page page click on a button to trigger the corresponding JS method, and then request the PHP interface to achieve, so there are two main

method to accomplish this requirement.

Method 1: Directly in the JS code using window.open () to open the URL of the PHP interface, you can download the PHP generated Excel file.

The PHP interface code is as follows:

$mysqli = Mysqli_connect (' localhost ', ' root ', ' 123456 ', ' test '); $sql = ' select * from country '; $res = Mysqli_query ($mysqli , $sql); Header ("Content-type:application/vnd.ms-excel"); Header ("Content-disposition:filename=country.xls"); echo "code\t"; echo "name\t"; echo "population\t\n"; if (Mysqli_num_rows ($res) > 0) {while    ($row = Mysqli_fetch_ Array ($res)) {        echo $row [' Code ']. " \ t ";        echo $row [' name ']. " \ t ";        echo $row [' population ']. " \t\n ";    }}

Method 2:php the generated Excel file in the server first, then return the file path to Js,js and then use window.open () to open the file path to download.

The PHP interface code is as follows:

$mysqli = Mysqli_connect (' localhost ', ' root ', ' 123456 ', ' test '); $sql = ' select * from country '; $res = Mysqli_query ($mysqli , $sql); $file = fopen ('./country.xls ', ' W '); Fwrite ($file, "code\tname\tpopulation\t\n"); if (Mysqli_num_rows ($res) > 0) {    while ($row = Mysqli_fetch_array ($res)) {        fwrite ($file, $row [' Code ']. \ t ". $row [' name ']." \ t ". $row [' population ']." \t\n ");}    } Fclose ($file); Echo ' Http://www.jtw.com/....../country.xls ';//return file path to JS here

The two methods are similar, can be implemented to export the data in the database into an Excel file and download the file, the final file is as follows:

Export database data to an Excel file using native PHP

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.