How to export an Excel instance in PHP and how to export an excel instance. The PHP Excel export example explains how to export an excel file using PHP open source program PHPExcel. some key code is shared with you, the following describes how to export an Excel file by using ph PHP and an excel file.
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.bkjia.com ')-> SetLastModifiedBy (' http://www.bkjia.com ')-> 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.
Articles you may be interested in:
- How to export a database to excel using php
- PHP exports MySQL data to an Excel file (fputcsv)
- How to solve memory overflow error when exporting a large amount of data in phpExcel
- Small Excel export experience based on PHP perfectly solves the garbled problem
- Php-based export to Excel or CSV (with utf8 and gbk encoding conversion)
- Php import and export excel instances
- Use phpExcel to import and export Excel data (detailed analysis in full steps)
- Use phpexcel to import excel into the database and export the database to excel.
- ThinkPHP uses PHPExcel to import and export Excel data to a complete instance
Export This PHP export Excel file using PHP open source program PHPExcel, some key code to share with you, the specific content is as follows ph...