Export excel data using php

Source: Internet
Author: User

Two excel export methods are provided: 1. the easiest way to export an excel header ('content-Type: application/vnd. ms-excel '); // you can set the file type to vnd. change ms-excel 'to xml (export xml file) header ('content-Disposition: attachment; filename = "cloud platform user report .xls "'); // set the exported excel name header ('cache-Control: max-age = 0'); echo iconv ("UTF-8", "gbk ", "time \ t error code \ t occurrence times \ t error code meaning \ n"); // \ t is a Tab character \ n is a linefeed foreach ($ arr as $ key => $ val) {// $ arr is the data to be exported echo iconv ("UTF-8", "gbk", date ("Y-m-d", $ val ["tim E "]). "\ t ". $ val ["error_code"]. "\ t ". $ val ["num"]. "\ t ". $ val ["code_mean"]. "\ n");} 2. use phpexcel to export an excel file and download the phpexcel package. put the php and phpexcel packages in your project: require_once ('phpexcel. php '); // reference the two files require_once ('phpexcel/Writer/Excel2007.php'); // export excel $ objExcel = new PHPExcel (); $ objProps = $ objExcel-> getProperties (); $ objProps-> setCreator ("Zeal Li"); $ objProps-> setLastModifiedBy ("Zeal Li"); $ objP Rops-> setTitle ("Office XLS Test Document"); $ objProps-> setSubject ("Office XLS Test Document, Demo"); $ objProps-> setDescription ("Test document, generated by PHPExcel. "); $ objProps-> setKeywords (" office excel PHPExcel "); $ objProps-> setCategory (" Test "); $ objProps = $ objExcel-> getProperties (); $ objProps-> setCreator ("Zeal Li"); $ objProps-> setLastModifiedBy ("Zeal Li"); $ objProps-> setTitle ("Office XLS Tes T Document "); $ objProps-> setSubject (" Office XLS Test Document, Demo "); $ objProps-> setDescription (" Test document, generated by PHPExcel. "); $ objProps-> setKeywords (" office excel PHPExcel "); $ objProps-> setCategory (" Test "); //*************************************/ /set the current sheet index, used for subsequent content operations. // Display the call only when multiple sheets are used. // By default, PHPExcel will automatically create the first sheet with the SheetIndex set to 0 $ objExcel-> setActiveSheetIndex (0); $ objActSheet = $ objExcel-> getActiveSheet (); // online games // set the name of the current activity sheet $ objActSheet-> setTitle ('Online games class'); $ objActSheet-> setCellValue ('a1', 'game name '); // The first row of data in the first sheet in the generated excel $ objActSheet-> setCellValue ('b1 ', 'specific domain name '); $ objActSheet-> setCellValue ('c1 ', 'server description'); $ objActSheet-> setCellValue ('d1', 'Line attribute '); $ objActSheet-> setCellValue ('e1 ', 'submitted Server IP address'); $ objActSheet-> setCellValue ('f1', 'corresponding to the Optimized route entry '); $ objActSheet-> setCellValue ('g1 ', 'keyword optimization'); $ objActSheet-> setCellValue ('h1', 'Optimized line '); $ objActSheet-> setCellValue ('i1', 'Optimized status'); foreach ($ result_webgame as $ k1 => $ v1) {// traverse the data read from the database $ objActSheet-> setCellValue ('A '. ($ k1 + 2), $ v1 ["gamename"]); $ objActSheet-> setCellValue ('B '. ($ k1 + 2), $ v1 ['domain ']); $ objActSheet-> setCellValue ('C '. ($ k1 + 2), $ v1 ['gameserver ']); $ objActSheet-> setCellValue ('D '. ($ k1 + 2), $ v1 ['line _ attribute']); $ objActSheet-> setCellValue ('E '. ($ k1 + 2), $ v1 ['server _ ip']); $ objActSheet-> setCellValue ('F '. ($ k1 + 2), $ v1 ['optimize _ route ']); $ objActSheet-> setCellValue ('G '. ($ k1 + 2), $ v1 ['keyword']); $ objActSheet-> setCellValue ('H '. ($ k1 + 2), $ v1 ['line']); $ objActSheet-> setCellValue ('I '. ($ k1 + 2), $ v1 ['optimize _ status']);} // website class // Add a new worksheet $ objExcel-> createSheet (); $ reource_ready = $ objExcel-> getSheet (1)-> setTitle ('website class'); $ reource_ready-> setCellValue ('a1', 'website name '); $ reource_ready-> setCellValue ('b1 ', 'specific domain name'); $ reource_ready-> setCellValue ('c1', 'Line attribute '); $ reource_ready-> setCellValue ('d1 ', 'IP CIDR Block'); $ reource_ready-> setCellValue ('e1 ', 'Optimized line '); $ reource_ready-> setCellValue ('f1', 'Optimized status'); foreach ($ result_website as $ k1 =>$ v2) {$ reource_ready-> setCellValue ('A '. ($ k1 + 2), $ v2 ["name"]); $ reource_ready-> setCellValue ('B '. ($ k1 + 2), $ v2 ['domain ']); $ reource_ready-> setCellValue ('C '. ($ k1 + 2), $ v2 ['line _ attribute']); $ reource_ready-> setCellValue ('D '. ($ k1 + 2), $ v2 ['IP']); $ reource_ready-> setCellValue ('E '. ($ k1 + 2), $ v2 ['line']); $ reource_ready-> setCellValue ('F '. ($ k1 + 2), $ v2 ['optimize _ status']);} // you can export an excel file containing multiple sheets.

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.