PHP exports data to an instance (speculative) in an Excel table, and the data is exported to an excel

Source: Internet
Author: User

PHP exports data to an instance (speculative) in an Excel table, and the data is exported to an excel

1. Introduction

How can I write data into an Excel file using the simplest, rough, and violent method?

Because documents in ms word and excel support html text format, we can use html text format to output data based on this principle.

In html, we only need to put the data in the corresponding html table in the desired order.

We use PHP to obtain and sort the data and construct the corresponding html text. Finally, we use byte stream output to download it to the user's local location.

2. Code

Code directly. This is a very simple program with comments in it.

ExportExcel. class. php file

<? Phpclass ExportExcel {/*** @ desc export data to Excel * @ param $ data array set table data * @ param $ titlename string set head * @ param $ title string set Header * @ param $ filename: Set the default file name * @ return to output the string, the output byte stream downloads the Excel file */public function excelData ($ data, $ titlename, $ title, $ filename) {# xmlns is the xml namespace $ str = "
<? Php $ obj = new ExportExcel (); $ data = array ('a11', 'a22', 'a33'), array ('b11', 'b22 ', 'b33'), array ('c11', 'c22', 'c33'), array ('d11', 'd22', 'd33 '), array ('e11', 'e22', 'e33'), array ('f11', 'f22', 'f33 '),); $ excelHead = "this is the title of an Excel table"; $ title = "My Excel table "; # file name $ headtitle = "<tr> <th colspan = '3' >{$ excelHead} </th> </tr> "; $ titlename = "<tr> <th style = 'width: 70px; '> Table 1 </th> <th style = 'width: 70px; '> Table 2 </th> <t H style = 'width: 70px; '> Table 3 </th> </tr> "; $ filename = $ title. ". xls "; $ obj-> excelData ($ data, $ titlename, $ headtitle, $ filename);?>

3. Test

Click to access:

Download the Excel file

View the file after the operation is successful:

After entering, the Excel file prompts that the file format is inconsistent with the extension name. This also indirectly shows that the Excel file we export is only an Excel file (essentially an html file ), the format is not an Excel file.

Click here to view the content. It looks like Excel, haha. Taipa

Change the suffix html to View Details:

You see, the essence is html files, but Excel only supports this format.

The above example of exporting data to an Excel table (speculative type) in PHP is all the content shared by the editor. I hope to give you a reference and support for the customer's house.

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.