header导出Excel应用示例

 更新时间:2014年01月24日 16:07:37   作者:  
本文为大家介绍下使用header导出Excel,具体实现代码如下,感兴趣的朋友不要错过

复制代码 代码如下:

<?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遍历文件夹与子目录的函数代码

    使用PHP遍历文件夹与子目录的函数代码

    最近对操作文件比较感兴趣的说,所以咱们来把文件夹给遍历了,顺便生成个树。
    2011-09-09
  • PHP中读取文件的几个方法总结(推荐)

    PHP中读取文件的几个方法总结(推荐)

    下面小编就为大家带来一篇PHP中读取文件的几个方法总结(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • 用PHP与XML联手进行网站编程代码实例

    用PHP与XML联手进行网站编程代码实例

    PHP对XML提供了的强大的支持。它使用了一个XML的“解析器”,并且为了支持这个解析器,它提供了20(PHP4)个XML的解析函数。下面是几个最常用的PHP解析函数。
    2008-07-07
  • PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法分析

    PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解

    这篇文章主要介绍了PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法,结合具体实例形式分析了preg_replace()报错的原因与相关解决技巧,需要的朋友可以参考下
    2017-02-02
  • PHP错误提示It is not safe to rely on the system……的解决方法

    PHP错误提示It is not safe to rely on the system……的解决方法

    今天小编就为大家分享一篇关于PHP错误提示It is not safe to rely on the system……的解决方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • PHP中使用php5-ffmpeg撷取视频图片实例

    PHP中使用php5-ffmpeg撷取视频图片实例

    这篇文章主要介绍了PHP中使用php5-ffmpeg撷取视频图片实例,本文使用一个AVI格式视频为例,讲解了如何撷取视频画面为图片,需要的朋友可以参考下
    2015-01-01
  • 老版本PHP转义Json里的特殊字符的函数

    老版本PHP转义Json里的特殊字符的函数

    本文给大家分享的是一则在老版本的php里使用的PHP转义Json里的特殊字符的函数,虽然不是很实用了,但还是记录一下,有需要的小伙伴可以参考下。
    2015-06-06
  • php实现的单一入口应用程序实例分析

    php实现的单一入口应用程序实例分析

    这篇文章主要介绍了php实现的单一入口应用程序,较为详细的分析了php实现单一入口应用程序的原理、功能、实现原理与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • PHP Ajax实现无刷新附件上传

    PHP Ajax实现无刷新附件上传

    这篇文章为大家详细主要介绍了PHP Ajax实现无刷新附件上传功能的具体代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • PHP抽象类与接口的区别详解

    PHP抽象类与接口的区别详解

    今天小编就为大家分享一篇关于PHP抽象类与接口的区别详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03

最新评论