How to export an Excel file using PHP

Source: Internet
Author: User
: This article describes how to export an Excel file in PHP. if you are interested in the PHP Tutorial, refer to it. PHP open-source PHP Excel is used to export Excel files. some key code is shared with you. the specific content is as follows:

<? Phperror_reporting (E_ALL); date_default_timezone_set ('Asia/Shanghai'); require_once '. /Classes/PHPExcel. php '; $ data = array (0 => array ('id' => 1001, 'username' => 'Zhang FE', 'password' => '123 ', 'address' => 'room 250, Lane 101, high-tech village, Three Kingdoms '), 1 => array ('id' => 1002, 'username' => 'Guan yu ', 'password' => '1234568', 'address' => 'Three Kingdoms Huaguo Mountain '), 2 => array ('id' => 123456, 'username' => 'caocao', 'password' => '000000', 'address' => '3, Lane 123456, Yan'an West Road '), 3 => array ('id' => 1004, 'username' => 'Liu Bei ', 'password' => '123 ', 'address' => 'room 188, No. 3309 Yuanyuan road '); $ objPHPExcel = new PHPExcel (); $ objPHPExcel-> getProperties ()-> setCreator (' http://www.jb51.net ')-> SetLastModifiedBy (' http://www.jb51.net ')-> SetTitle ('Office 2007 XLSX document')-> setSubject ('Office 2007 XLSX document')-> setDescription ('document for Office 2007 XLSX, generated using PHP classes. ')-> setKeywords ('Office 2007 openxml php')-> setCategory ('result file'); $ objPHPExcel-> setActiveSheetIndex (0)-> setCellValue ('A1 ', 'id')-> setCellValue ('b1 ', 'Username')-> setCellValue ('C1', 'password')-> setCellValue ('d1 ', 'address'); $ I = 2; foreach ($ data as $ k = >$ v) {$ objPHPExcel-> setActiveSheetIndex (0)-> setCellValue ('A '. $ I, $ v ['id'])-> setCellValue ('B '. $ I, $ v ['username'])-> setCellValue ('C '. $ I, $ v ['password'])-> setCellValue ('D '. $ I, $ v ['address']); $ I ++ ;}$ objPHPExcel-> getActiveSheet ()-> setTitle ('class 2, grade 3 '); $ objPHPExcel-> setActiveSheetIndex (0); $ filename = urlencode ('student information Statistics '). '_'. date ('Y-m-dHis '); // Generate the xlsx file/* header ('content-Type: application/vnd. openxmlformats-officedocument.spreadsheetml.sheet '); header ('content-Disposition: attachment; filename = "'.w.filename.'.xlsx"'); header ('cache-Control: max-age = 0 '); $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel2007 '); * // Generate the xls file header ('content-Type: application/vnd. ms-excel '); header ('content-Disposition: attachment; filename = "'.w.filename.'.xls"'); header ('cache-Control: max-age = 0 '); $ objWriter = PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5'); $ objWriter-> save ('php: // output'); exit;

Note: If garbled characters occur during Chinese export, you can convert the string to gb2312. the code is similar to the following:

View code printing

$str=mb_convert_encoding("gb2312","UTF-8",$str);

The above is all the content of this article, hoping to help you learn.

The above section describes how to export an Excel file in PHP, including some content. if you are interested in the PHP Tutorial, please help.

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.