1 /**
2 * 导出xls文件
3 * @param $columns 列数
4 * @param $data 二维数组数据
5 * @param string $title 工作表名称
6 * @param string $file_name 下载名称
7 */
8 function export_excel($columns, $data, $title='sheet', $file_name='download_xls') {
9 $rows = count($data);
10 $cell_name = array();
11 for ($i=0; $i<$columns; $i++) {
12 if ($i<26) {
13 $cell_name[$i] = chr($i + 65);
14 } else {
15 $cell_name[$i] = chr(intval($i/26)+64) . chr($i%26+65);
16 }
17 }
18 vendor("PHPExcel.PHPExcel");
19 $obj = new \PHPExcel();
20 $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
21 \PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
22 //mergeCells 合并单元格
23 for ($i=0; $i<$rows; $i++) {
24 for ($j=0; $j<$columns; $j++) {
25 if (isset($data[$i][$j])) {
26 $obj->setActiveSheetIndex(0)->setCellValue($cell_name[$j].($i+1), $data[$i][$j]);
27 }
28 }
29 }
30 $obj->getActiveSheet()->setTitle($title);
31
32 header('Content-Type: applicationnd.ms-excel');
33 header('Content-Disposition: attachment;filename='.$file_name.'.xls');
34 header('Cache-Control: max-age=0');
35 $objWriter = \PHPExcel_IOFactory::createWriter($obj, 'Excel5');
36 $objWriter->save('php://output');
37 exit;
38 }