header导出Excel应用示例

2015-01-24信息快讯网

本文为大家介绍下使用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(); 
} 
} 
?> 

phpmailer在服务器上不能正常发送邮件的解决办法
采用memcache在web集群中实现session的同步会话
CodeIgniter实现更改view文件夹路径的方法
PHP关于htmlspecialchars、strip_tags、addslashes的解释
PHP+Memcache实现wordpress访问总数统计(非插件)
php+memcache实现的网站在线人数统计代码
CodeIgniter安全相关设置汇总
PHP中遇到BOM、<feff>编码导致json_decode函数无法解析问题
php foreach正序倒序输出示例代码
PHPMailer发送HTML内容、带附件的邮件实例
PHP使用GIFEncoder类处理gif图片实例
PHP使用GIFEncoder类生成gif动态滚动字幕
ThinkPHP之foreach标签使用概述
修改ThinkPHP缓存为Memcache的方法
ThinkPHP2.0读取MSSQL提示Incorrect syntax near the keyword 'AS'的解决方法
Thinkphp中Create方法深入探究
PHP中多维数组的foreach遍历示例
PHP弹出提示框并跳转到新页面即重定向到新页面
使用openssl实现rsa非对称加密算法示例
解决php接收shell返回的结果中文乱码问题
php多种形式发送邮件(mail qmail邮件系统 phpmailer类)
使用php伪造referer的方法 利用referer防止图片盗链
用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤
修改apache配置文件去除thinkphp url中的index.php
PHP获取php,mysql,apche的版本信息示例代码
windows下PHP_intl.dll正确配置方法(apache2.2+php5.3.5)
php中filter函数验证、过滤用户输入的数据
memcache命令启动参数中文解释
codeigniter框架批量插入数据
eaglephp使用微信api接口开发微信框架
php Calender(日历)代码分享
codeigniter使用技巧批量插入数据实例方法分享
php安装xdebug/php安装pear/phpunit详解步骤(图)
php用header函数实现301跳转代码实例
php堆排序(heapsort)练习
php生成EAN_13标准条形码实例
©2014-2024 dbsqp.com