header导出Excel应用示例

yipeiwu_com5年前PHP代码库

复制代码 代码如下:

<?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在字符断点处截断文字的实现代码

复制代码 代码如下: //所谓断字 (word break),即一个单词可在转行时断开的地方。这一函数将在断字处截断字符串。 // Please acknowledge use of t...

PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程

PHP+Ajax检测用户名或邮件注册时是否已经存在是论坛或会员系统中常见的一个重要功能。本文就以实例形式简单描述这一功能的实现方法。具体步骤如下: 一、PHP检测页面 check.php...

php 命名空间(namespace)原理与用法实例小结

本文实例讲述了php 命名空间(namespace)原理与用法。分享给大家供大家参考,具体如下:命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会...

用php实现的下载css文件中的图片的代码

作为一个资深并且专业的扒皮人员,在我从初三开始投入伟大的互联网中到现在积累了丰富的扒皮经验。我相信每个做web的程序员也都会有类似的经历。 在扒皮过程中,必不可少的需要下载样式文件中的图...

set_include_path在win和linux下的区别

刚刚调式程序,本来在服务器上好好的程序到了win下居然出错。 后来仔细调式才发现是set_include_path的问题。 在win下,当你要include多个路径的...