多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### Excel 导出 jig 2018-3-3 **位置:** Common\Tools\ExcelTools.class.php **参数:** * @param $title array 标题 * @param $data array 内容 二维数组 * @param $filename string 文件名 **调用:** ~~~ use Common\Tools\ExcelTools; // 调用封装Excel工具类 public function export() { $title = array('姓名', '城市', '手机'); $data = array( array('李明', '北京', '13812345678'), array('王伟', '深圳', '15887654321') ); $filename = '同学通讯录'; $e = new ExcelTools(); $e->exportExcel($title, $data, $filename); } ~~~ **完整代码:** ~~~ /* * 导出Excel * * jig 2018-3-3 * @param $title array 标题 * @param $data array 内容 二维数组 * @param $filename string 文件名 * return resource; */ public function exportExcel($title, $data, $filename) { $this->_title = $title; $phpExcel = new \PHPExcel(); $active_sheet = $phpExcel->getActiveSheet(); if (!is_null($this->getTitle())) { $i='A'; $j = 1; foreach ($this->getTitle() as $key => $value) { $active_sheet->setCellValue($i . $j, $value); $i++; } $j++; } foreach ($data as $key => $value) { $i = 'A'; foreach ($value as $k => $v) { // setCellValueExplicit 修改单元格的值为文本格式,解决首位0被去掉的BUG lanson 2017-11-21 if (preg_match("/^[0]+\d+$/", $v)) { $active_sheet->setCellValueExplicit($i . $j, $v, 'str'); } elseif(preg_match("/^\d{12,100}$/", $v)) { $active_sheet->setCellValueExplicit($i . $j, $v, 'str'); } else { $active_sheet->setCellValue($i . $j, $v); } $i++; } $j++; } // $objWrite = \PHPExcel_IOFactory::createWriter($phpExcel,"Excel2007"); $objWrite = \PHPExcel_IOFactory::createWriter($phpExcel, "Excel5"); // $objWrite->save($filename); header("Content-type:application/vnd.ms-excel"); //lanson 2017-03-17 新加这个 header("Content-Disposition:attachment;filename=$filename.xls"); header("Content-Type:application/octet-stream"); header("Content-Transfer-Encoding:binary"); header("Pragma:no-cache"); $objWrite->save('php://output'); exit(); } ~~~