使用PHPExcel导出Excel表
发布:smiling 来源: PHP粉丝网 添加日期:2018-10-19 13:07:24 浏览: 评论:0
本文实例为大家分享了PHPExcel导出Excel表的具体代码,供大家参考,具体内容如下:
- /**
- * Excel导出
- * @param $fileName(文件名)
- * @param $headArr (表头)
- * @param $data (每一行的数据)
- * @throws \PHPExcel_Exception
- * @throws \PHPExcel_Reader_Exception
- */
- function getExcel($fileName,$headArr,$data){
- include_once (THINK_PATH."Library/Vendor/phpexcel/PHPExcel.php");
- include_once (THINK_PATH."Library/Vendor/phpexcel/PHPExcel/Writer/Excel2007.php");
- include_once (THINK_PATH."Library/Vendor/phpexcel/PHPExcel/Writer/Excel5.php");
- include_once (THINK_PATH."Library/Vendor/phpexcel/PHPExcel/IOFactory.php");
- if(emptyempty($data) || !is_array($data)){
- die("data must be a array");
- }
- if(emptyempty($fileName)){
- exit;
- }
- $date = date("Y_m_d",time());
- $fileName .= "_{$date}.xlsx";
- //创建新的PHPExcel对象
- $objPHPExcel = new \PHPExcel();
- $objProps = $objPHPExcel->getProperties();
- //设置表头
- $key = ord("A");
- $key2 = ord("A");
- $colum2 = '';
- $objActSheet = $objPHPExcel->getActiveSheet();
- $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
- foreach($headArr as $v){
- $colum = chr($key);
- $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum2.$colum.'1', $v);
- if($key < 90){
- $key += 1;
- }else{
- $key = ord("A");
- $colum2 = chr($key2);
- $key2++;
- }
- }
- //exit;
- $column = 2;
- foreach($data as $key => $rows){ //行写入
- $span = ord("A");
- $span2 = ord("A");
- $j2 = '';
- foreach($rows as $keyName=>$value){// 列写入
- $j = chr($span);
- //$objActSheet->setCellValue($j.$column, $value);
- //把每个单元格设置成分文本类型
- //dump($j2.$j.$column);
- $objActSheet->setCellValueExplicit($j2.$j.$column,$value,\PHPExcel_Cell_DataType::TYPE_STRING);
- if($span < 90){
- $span += 1;
- }else{
- $span = ord("A");
- $j2 = chr($span2);
- $span2++;
- }
- }
- $column++;
- }
- // exit;
- $fileName = iconv("utf-8", "gb2312", $fileName);
- //重命名表
- $objPHPExcel->getActiveSheet()->setTitle('Simple');
- //设置活动单指数到第一个表,所以Excel打开这是第一个表
- $objPHPExcel->setActiveSheetIndex(0);
- //将输出重定向到一个客户端web浏览器(Excel2007)
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- header("Content-Disposition: attachment; filename=\"$fileName\"");
- header('Cache-Control: max-age=0');
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
- // if(!empty($_GET['excel'])){
- // $objWriter->save('php://output'); //文件通过浏览器下载
- // }else{
- // $objWriter->save($fileName); //脚本方式运行,保存在当前目录
- // } //phpfensi.com
- $objWriter->save('php://output');
- exit;
- }
Tags: PHPExcel 导出Excel表
相关文章
- ·PHP通过PHPExcel类导入导出excel(2014-06-25)
- ·PHPExcel生成和读取Excel文件实例程序(2014-07-18)
- ·Codeigniter利用PHPExcel导出Excel文件(2014-07-26)
- ·phpExcel数据内存溢出解决办法(2014-07-30)
- ·PHPEXCEL导入excel表格生成数组(2014-08-26)
- ·PHP中利用PHPExcel导出Excel示例(2014-08-27)
- ·php利用PHPExcel类导出导入Excel用法(2014-08-28)
- ·Yii中使用PHPExcel导出Excel实例代码(2014-09-10)
- ·PHPExcel读取excel并导入mysql数据库代码(2014-09-10)
- ·PHPExcel读取excel并导入数据库(2014-09-10)
- ·phpExcel导出大量数据出现内存溢出错误解决办法(2014-09-10)
- ·PHPEXCEL的用法与简介(2014-09-10)
- ·利用phpexcel读取excel实现代码(2014-09-11)
- ·PHPexcel生成复杂的报表表头类例子(2015-04-15)
- ·phpexcel导出与读取excel的经典实例(2015-04-15)
- ·PHPExcel导出excel入门实例教程(2016-08-25)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)