Using PHPExcel to export data in batches to an excel table-php Tutorial

Source: Internet
Author: User
The following section describes how to use PHPExcel to batch export data to an excel table (required ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following small series for you to use PHPExcel to export data in batches into an excel table (mandatory ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.

First, you need to download the PHPExecel class file. for help documentation, refer to PHPExcel Chinese help manual | PHPExcel usage.

The following example shows a simple example of exporting data in batches as excel.

Front-end pageIt is relatively simple, that is, a hyperlink, jump to the processing page, the hyperlink can also be followed by some parameters (see requirements )!


Export an excel table

Background Process. php page


/*** Export data in batches ** @ param $ arr query data from the database, that is, the data to be exported * $ name excel table song name */function expExcel ($ arr, $ name) {require_once 'phpexcel. php '; // instantiate $ objPHPExcel = new PHPExcel ();/* right-click the information displayed in the attribute */$ objPHPExcel-> getProperties ()-> setCreator ("zxf ") // Author-> setLastModifiedBy ("zxf") // The last warranty period-> setTitle ('export data EXCEL ') // title-> setSubject ('export data EXCEL ') // topic-> setDescription ('export data') // description-> setKeywords ("excel") // tag-> setCategory ("result file "); // category // set the current table $ objPHPExcel-> setActiveSheetIndex (0); // set the content displayed in the first row of the table $ objPHPExcel-> getActiveSheet () -> setCellValue ('A1', 'owner name')-> setCellValue ('b1 ', 'password')-> setCellValue ('C1', 'mobile phone number ') -> setCellValue ('d1 ', 'address') // set the first row of red font-> getStyle ('A1: D1')-> getFont ()-> getColor () -> setARGB (PHPExcel_Style_Color: COLOR_RED); $ key = 1;/* The following figure shows how to process the data in Excel and obtain the data horizontally */foreach ($ arr as $ v) {// Set the loop starting from the second row $ key ++; $ objPHPExcel-> getActiveSheet () // Column A in Excel. name is the key value field of the array you have found, and so on-> setCellValue ('A '. $ key, $ v ['name'])-> setCellValue ('B '. $ key, $ v ['pwd'])-> setCellValue ('C '. $ key, $ v ['phone'])-> setCellValue ('D '. $ key, $ v ['address']);} // set the current table $ objPHPExcel-> setActiveSheetIndex (0); ob_end_clean (); // clear the buffer, avoid garbled header ('content-Type: application/vnd. ms-excel '); // file type header ('content-Disposition: attachment; filename = "'.w.name.'.xls"'); // file name header ('cache-Control: max-age = 0'); header ('content-Type: text/html; charset = utf-8 '); // code $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5'); // excel 2003 $ objWriter-> save ('php: // output'); exit ;} /**********************************/header ("Content -type: text/html; charset = utf-8 "); // link database $ link = @ mysql_connect ('localhost', 'root ','') or die ('database connection failed'); mysql_select_db ('test', $ link); mysql_query ('set names utf8 '); // Obtain data first $ SQL = "select * from house"; $ res = mysql_query ($ SQL); $ arr = array (); // transfer $ res => $ arr to an array. while ($ row = mysql_fetch_assoc ($ res) {$ arr [] = $ row ;} // excel table name $ name = "user table"; // call expExcel ($ arr, $ name)

Data export using PHPExcel is now complete. For more information about using PHPExcel to import data to a database, see using PHPExcel to batch upload data to a database.

The preceding section describes how to use PHPExcel to export data in batches to an excel table. For more information, see other related articles in the first PHP community!

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.