header导出Excel应用示例


Posted in PHP onJanuary 24, 2014
<?php 
class reportFormAction extends CommonAction{ public function index(){ 
if($_POST){ 
//@param $data array 需要导出的数据 
//@param $field string 导出csv文件的列名 
//@param $filename string 需要导出csv文件的名字 
$where = '1'; 
$order = 'creative_id desc'; 
if($_POST['crea_wh'] == 1) $order .= ""; 
if($_POST['crea_wh'] == 2) $order .= "creative_time desc"; 
$star = strtotime($_POST['control_star']); 
$end = strtotime($_POST['control_end']); 
if($star && $end) $where .= " and (create_time between $star and $end)"; 
if($_POST['creative_type'] == 1) $where .= " and creative_type =1"; 
if($_POST['creative_type'] == 2) $where .= " and creative_type =2"; 
if($_POST['creative_type'] == 3) $where .= " and creative_type =3"; 
if($_POST['sort_name'] == 1) $where .= " and sort_name = '技术类'"; 
if($_POST['sort_name'] == 2) $where .= " and sort_name = '包装类'"; 
if($_POST['sort_name'] == 3) $where .= " and sort_name = '产品类'"; 
if($_POST['sort_name'] == 4) $where .= " and sort_name = '营销类'"; 
if($_POST['sort_name'] == 5) $where .= " and sort_name = '其他类'"; 
if($_POST['two_status']==0) {$where .= " and two_status=0"; $fenlei1="一句话创意";} 
if($_POST['two_status']==1) {$where .= " and two_status=1"; $fenlei2="标准创意";} 
$csv = ''; 
$lists = M('creative')->where($where)->order($order)->select(); 
if(is_array($lists) && count($lists)>0) { 

if(empty($filename)) { 
$filename = date('Y-m-d',time()).'.csv'; 
} 
header('Content-type:application/vnd.ms-excel'); 
header('Content-Disposition:attachment;filename='.$filename); 
header('Pragma:no-cache'); 
header('Expires:0'); 
if($_POST['two_status']==0){ 
$csv= '分类,创意ID,创意类型,创意标签,创意标题,创意描述,评论数, 收藏数,投票数,提交人,作者'."\n"; 
}elseif($_POST['two_status']==1){ 
$csv= '分类,创意ID,创意类型,创意标签,创意标题,创意描述,评论数, 收藏数,投票数,提交人,作者,合作者,核心创新点说明,市场计划,已有类似商业化案例,相关专利状况,实现方式讨论,发表时间'."\n"; 
}else{ 
$csv= '分类,创意ID,创意类型,创意标签,创意标题,创意描述,评论数, 收藏数,投票数,提交人,作者,合作者,核心创新点说明,市场计划,已有类似商业化案例,相关专利状况,实现方式讨论,发表时间'."\n"; 
} 
foreach($lists as $list =>$v) { 
if($v['creative_type'] == 1){ 
$type = '问题'; 
}elseif($v['creative_type'] == 2){ 
$type = '解决方式'; 
}elseif($v['creative_type'] == 3){ 
$type = '新创意'; 
} 
if($v['two_status']==0) $fenlei="一句话创意"; 
if($v['two_status']==1) $fenlei="标准创意"; 
if($_POST['two_status']==0){ //一句话 
$csv .= $fenlei1.','.$v['creative_id'].",".$v['sort_name'].','.$type.','.$v['creative_name'].','.$v['creative_description'].','.$v['comment_num'].','.$v['collect_num'].','.$v['vote_num'].','.$v['submitter'].','.$v['author']."\n"; 
}elseif($_POST['two_status']==1){ //标准 
$csv .= $fenlei2.','.$v['creative_id'].",".$v['sort_name'].','.$type.','.$v['creative_name'].','.$v['creative_description'].','.$v['comment_num'].','.$v['collect_num'].','.$v['vote_num'].','.$v['submitter'].','.$v['author'].','.$v['collaborator'].','.$v['innovation_description'].','.$v['marketing_plan'].','.$v['business_case'].','.$v['patent_situation'].','.$v['discuss_ways'].','.strtotime($v['create_time'])."\n"; 
}else{ //全部 
$csv .= $fenlei.','.$v['creative_id'].",".$v['sort_name'].','.$type.','.$v['creative_name'].','.$v['creative_description'].','.$v['comment_num'].','.$v['collect_num'].','.$v['vote_num'].','.$v['submitter'].','.$v['author'].','.$v['collaborator'].','.$v['innovation_description'].','.$v['marketing_plan'].','.$v['business_case'].','.$v['patent_situation'].','.$v['discuss_ways'].','.strtotime($v['create_time'])."\n"; 
} 
} 
echo mb_convert_encoding($csv,"CP936","UTF-8"); 
}else{ 
$this->assign("msg",$msg); 
} 
exit; 
} 


$this->display(); 
} 

public function votecount(){ 
if($_POST){ 
//@param $data array 需要导出的数据 
//@param $field string 导出csv文件的列名 
//@param $filename string 需要导出csv文件的名字 
$where = '1'; 
if($_POST['crea_order'] == 1) $order = " c.vote_num desc"; 
$star = strtotime($_POST['control_star']); 
$end = strtotime($_POST['control_end']); 
if($star && $end) $where .= " and (c.vote_start_time between $star and $end) and (c.vote_end_time between $star and $end)"; 
$sql ="select c.sort_name,c.creative_id,c.creative_type,c.creative_name,c.creative_description,c.vote_num,c.comment_num,c.collect_num,c.create_time, u.username from cofco_creative as c left join cofco_userinfo as u on c.uid = u.uid where $where order by $order "; 
$lists = M()->query($sql); 
$csv = ''; 
if(is_array($lists) && count($lists)>0) { 

if(empty($filename)) { 
$filename = date('Y-m-d',time()).'.csv'; 
} 
header('Content-type:application/vnd.ms-excel'); 
header('Content-Disposition:attachment;filename='.$filename); 
header('Pragma:no-cache'); 
header('Expires:0'); 

$csv = '创意标签,提交者,创意分类,创意标题,创意简述,得票数,评论数, 收藏数,上传时间'."\n"; 
foreach($lists as $list =>$v) { 
if($v['creative_type'] == 1){ 
$type = '问题'; 
}elseif($v['creative_type'] == 2){ 
$type = '解决方式'; 
}elseif($v['creative_type'] == 3){ 
$type = '新创意'; 
} 
$time = date("Y-m-d H:i:s",$v['create_time']); 
$csv .= $v['sort_name'].','.$v['username'].','.$type.','.$v['creative_name'].','.$v['creative_description'].','.$v['vote_num'].','.$v['comment_num'].','.$v['collect_num'].','.$time."\n"; 
} 
echo mb_convert_encoding($csv,"CP936","UTF-8"); 
}else{ 
$this->assign("msg",$msg); 
} 
exit; 
} 
$this->display(); 
} 
} 
?>
PHP 相关文章推荐
PHP5在Apache下的两种模式的安装
Sep 05 PHP
php网站判断用户是否是手机访问的方法
Nov 01 PHP
Php header()函数语法及使用代码
Nov 04 PHP
php绘制一个矩形的方法
Jan 24 PHP
php实现过滤字符串中的中文和数字实例
Jul 29 PHP
PHP使用Pthread实现的多线程操作实例
Nov 14 PHP
jquery+thinkphp实现跨域抓取数据的方法
Oct 15 PHP
PHP以json或xml格式返回请求数据的方法
May 31 PHP
PHP实现的CURL非阻塞调用类
Jul 26 PHP
laravel-admin自动生成模块,及相关基础配置方法
Oct 08 PHP
通过实例解析PHP数据类型转换方法
Jul 11 PHP
PHP isset empty函数相关面试题及解析
Dec 11 PHP
使用openssl实现rsa非对称加密算法示例
Jan 24 #PHP
测试php连接mysql是否成功的代码分享
Jan 24 #PHP
php实现邮件发送并带有附件
Jan 24 #PHP
php中sql注入漏洞示例 sql注入漏洞修复
Jan 24 #PHP
php 发送带附件邮件示例
Jan 23 #PHP
php 获取页面中指定内容的实现类
Jan 23 #PHP
php 根据url自动生成缩略图并处理高并发问题
Jan 23 #PHP
You might like
php导出csv文件,可导出前导0实例代码
2016/11/16 PHP
PHP实现打包下载文件的方法示例
2017/10/07 PHP
Javascript变量函数浅析
2011/09/02 Javascript
JQuery操作iframe父页面与子页面的元素与方法(实例讲解)
2013/11/20 Javascript
js实现checkbox全选、不选与反选的方法
2015/02/09 Javascript
jquery+ajax+text文本框实现智能提示完整实例
2016/07/09 Javascript
JavaScript中Math对象的方法介绍
2017/01/05 Javascript
jQuery DateTimePicker 日期和时间插件示例
2017/01/22 Javascript
Angularjs上传图片实例详解
2017/08/06 Javascript
JavaScript调试之console.log调试的一个小技巧分享
2017/08/07 Javascript
Vue cli+mui 区域滚动的实例代码
2018/01/25 Javascript
Vue项目服务器部署之子目录部署方法
2019/05/12 Javascript
bootstrap Table实现合并相同行
2019/07/19 Javascript
[03:48]DOTA2完美大师赛主赛事第二日精彩集锦
2017/11/24 DOTA
Python中使用PyHook监听鼠标和键盘事件实例
2014/07/18 Python
初步介绍Python中的pydoc模块和distutils模块
2015/04/13 Python
Python爬取国外天气预报网站的方法
2015/07/10 Python
Python采用Django开发自己的博客系统
2020/09/29 Python
Python使用getpass库读取密码的示例
2017/10/10 Python
如何使用Python的Requests包实现模拟登陆
2018/04/27 Python
python 获取字符串MD5值方法
2018/05/29 Python
Python使用ConfigParser模块操作配置文件的方法
2018/06/29 Python
pytorch动态网络以及权重共享实例
2020/01/06 Python
世界上最大的冷却器制造商:Igloo Coolers
2019/07/23 全球购物
英国办公家具网站:Furniture At Work
2019/10/07 全球购物
中学教师岗位职责
2013/11/26 职场文书
物流毕业生个人的自我评价
2014/02/13 职场文书
《庐山的云雾》教学反思
2014/04/22 职场文书
竞选班干部演讲稿400字
2014/08/20 职场文书
中学生的1000字检讨书
2014/10/11 职场文书
三严三实民主生活会发言稿
2014/10/13 职场文书
2014年收银工作总结
2014/11/13 职场文书
幼儿教师年度个人总结
2015/02/05 职场文书
2016年秋季运动会通讯稿
2015/11/25 职场文书
笔记本自带的win11如何跳过联网激活?
2022/04/20 数码科技
讨论nginx location 顺序问题
2022/05/30 Servers