1.第一种
调用方法:
<?php
$str = '';
$str = '<table border = "2">
<thead>
<tr>
<th>顶顶顶顶顶顶顶顶顶顶顶顶顶顶</th>
<th>2</th>
<th>3</th>
<th>3</th>
</tr>
</thead>
<thead>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
<th>3</th>
</tr>
</thead>
';
$filename = '11111111';
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=".$filename.".xls");
echo $str;
?>
2.第二种,封装为一个类
<?php
/**
* @copyright 2014 SYSTEM_77NETADMIN
* @description: 输出为execl类
* @file: cls_output_excl.php
* @author: Liu
* @charset: UTF-8
* @time: 2014-09-01 14:07:37
* @version 1.0.0
**/
class cls_output_execl {
public $inEncode; //页面编码
public $outEncode; //Excel文件的编码
public function __construct(){
}
/**
* @name: set_encode
* @description: 设置编码
* @scope: public
* @param: string 页面编码
* @param: string Excel文件的编码
* @return: mixed
* @author: Liu
* @create: 2014-09-01 14:07:37
**/
public function set_encode($incode,$outcode){
$this->inEncode=$incode;
$this->outEncode=$outcode;
}
/**
* @name: set_title
* @description: 设置Execl标题栏
* @scope: public
* @param: array
* @return: array
* @author: Liu
* @create: 2014-09-01 14:07:37
**/
public function set_title($titlearr){
$title="";
foreach($titlearr as $v){
if($this->inEncode!=$this->outEncode){
$title.=iconv($this->inEncode,$this->outEncode,$v)."\t";
}
else{
$title.=$v."\t";
}
}
$title.="\n";
return $title;
}
/**
* @name: set_title
* @description: 设置Execl内容
* @scope: public
* @param: array
* @return: array
* @author: Liu
* @create: 2014-09-01 14:07:37
**/
public function set_row($array){
$content="";
foreach($array as $k => $v){
foreach($v as $vs){
if($this->inEncode!=$this->outEncode){
$content.=iconv($this->inEncode,$this->outEncode,$vs)."\t";
}
else{
$content.=$vs."\t";
}
}
$content.="\n";
}
return $content;
}
/**
* @name: set_title
* @description: 生成并自动下载Excel,文件名称 (为空,已当前日期为名称)
* @scope: public
* @param: array
* @return: mixed
* @author: Liu
* @create: 2014-09-01 14:07:37
**/
public function get_excel($titlearr,$array,$filename=''){
if($filename==''){
$filename=date('YmdHis');
}
$title=$this->set_title($titlearr);
$content=$this->set_row($array);
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=".$filename.".xls");
echo $title;
echo $content;
die;
}
}
调用方法:
//输出到execl
if($output_op == 'output'){
g('output_execl') -> set_encode('utf-8','utf-8');
$title = array('消息序号','商家名称','商铺名称','消息标题','消息发送时间','发送的人数');
$contentarr = $data['data'];
if(is_array($contentarr) && count($contentarr)){
foreach ($contentarr as $key => &$val){
unset($val['ms_desc']);
unset($val['ms_issend']);
unset($val['pus_read']);
unset($val['bui_call']);
if($val['ms_send_time']){
$contentarr[$key]['ms_send_time'] = date('Y-m-d H:i:s',$val['ms_send_time']);
}
}
}
g('output_execl') -> get_excel($title,$contentarr);
}