记得在以前写了个导出excel结果实现了导出excel但是导出来的excel的格式非常的难看没有办法进行美化的。最原始的导出excel的办法就是利用header()函数进行文件。其实phpexcel也是利用header()函数进行导出的。只不过是多了美国的过程而以。不罗嗦了贴出代码。
html部分
代码如下 | 复制代码 |
<div><a href="/esoxue.php/Article/download/"/>导出Excel</a></div> |
执行代码部分
代码如下 | 复制代码 |
public function download() { import("ORG.Excel.PHPExcel"); $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); $list=M("Test"); $rs=$list->select(); $i=2; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '姓名') ->setCellValue('B1', '年龄') ->setCellValue('C1', '性别') ->setCellValue('D1', '身份证号码') ->setCellValue('E1', '国籍') ->setCellValue('F1', '民族') ->setCellValue('G1', '详细地址'); $objPHPExcel->setActiveSheetIndex(0); foreach($rs as $k=>$v){ $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$i, $v['name']) ->setCellValue('B'.$i, $v['old']) ->setCellValue('C'.$i, $v['sex']) ->setCellValue('D'.$i, $v['sid']) ->setCellValue('E'.$i, $v['guoji']) ->setCellValue('F'.$i, $v['minzu']) ->setCellValue('G'.$i, $v['address']); $i++; } $objPHPExcel->getActiveSheet()->setTitle('student');//设置sheet标签的名称 $objPHPExcel->setActiveSheetIndex(0); ob_end_clean(); //清空缓存 header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate,post-check=0,pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download"); header('Content-Disposition:attachment;filename=11.xls');//设置文件的名称 header("Content-Transfer-Encoding:binary"); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; } |
效果图