Use PHPExcel to export an excel table

Source: Internet
Author: User

Go to the official website to download PHPExcel. You can select this plug-in. This plug-in is very powerful. Basically all the table formats you need are supported. Here I will only talk about the experience of exporting tables. There are many examples in this section, open it and check whether you can debug it. Here is the simplest method.

The most common table format is the. Xls file type, so find the decompressed file Examples/02types-xls.php and copy it to the desired location. In this file, you only need to change it:

Require_once dirname (_ FILE _). './Classes/PHPExcel. Php ';

You can change it to your file storage directory.


The rest is to loop your data array to the table.


Foreach ($ arrayobj as $ key => $ val ){
$ Excelnum = $ key + 1;
//----------------------------
$ ObjPHPExcel-> getActiveSheet ()
-> SetCellValue ("A $ excelnum", $ val ['result _ Code'])
-> SetCellValue ("B $ excelnum", $ val ['orderstate'])
-> SetCellValue ("C $ excelnum", $ val ['trade _ type'])
-> SetCellValue ("D $ excelnum", $ val ['launchmoney']/100)
-> SetCellValue ("E $ excelnum", $ val ['total _ handler'])
-> SetCellValue ("F $ excelnum", "". $ val ['transaction _ id'])
-> SetCellValue ("G $ excelnum", "". $ val ['Order _ id'])
-> SetCellValue ("H $ excelnum", $ val ['time _ end']);
}


Here, we should note that the array starts from 0, but the table starts from 1. So here we need to add 1, ABCDEF... to represent the columns of the table.

Download my file and put it under the environment where php can be run. Open and run it, a table file will be generated under the source code directory. Here I post the data to my example. I commented out, and I wrote the data directly in it, which can be changed according to my own situation.

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.