: 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.