PHPExcel导出

来源:互联网 发布:js 按轨迹移动 编辑:程序博客网 时间:2024/05/16 14:01
该方法是我根据自己项目需求写的,,有些细节地方需要根据情况修改。
我简单解说下参数:
data是数据列表
colums是要显示的列在data中对应的字段名

names是要显示的列的名称
name是Excel文件的文件名,

调用很简单 
如下:
$map=self::detail($startTime,$endTime, $detailType,$followerType, $pn==0?-1:$pn,$icon?50:20);
$user = $map['screenName'];
$time = $map['time'];
$list = $map['userList'];
$yellowV = 0;//黄V个数
$blueV = 0;//黄V个数
$other = 0;
$colums = array("screen_name",
array("user.created_at",function($created_at){
return date("Y-m-d H:i:s",strtotime($created_at));
}),
array("user","verified_reason"),
array("user.verified_type",function($type)use(&$yellowV,&$blueV,&$other){
$verfType = "";
if (0 == $type) {
$verfType = "黄V";
$yellowV++;
}else if($type>=1&&$type<=6){
$verfType = "蓝V";
$blueV++;
}else {
$other++;
}
return $verfType;
}),
array("user","statuses_count"),
array("user","followers_count"),
array("user","friends_count"),
array("user","description"),
array("user","location"),
array("user.id","user.name",function($id,$name)use($platform){
if ($platform==1){
return "http://weibo.com/".$id;
}else{
return "http://t.qq.com/".$name;
}
})
);

$names = array("微博名","注册日期","认证信息","认证类型","微博数",
"粉丝数","关注数","标签","地域","微博主页");
ExcelUtiles::output_Excel($list, $colums,$names,$user.$time,function($objPHPExcel,$i)use(&$yellowV,&$blueV,&$other){
$char = 'A';
$objPHPExcel->getActiveSheet()->setCellValue($char . $i,"黄V数");
$char = chr(ord($char)+1);
$objPHPExcel->getActiveSheet()->setCellValue($char . $i,$yellowV." ");
$char = chr(ord($char)+1);
$objPHPExcel->getActiveSheet()->setCellValue($char . $i,"蓝V数");
$char = chr(ord($char)+1);
$objPHPExcel->getActiveSheet()->setCellValue($char . $i,$blueV." ");
$char = chr(ord($char)+1);
$objPHPExcel->getActiveSheet()->setCellValue($char . $i,"其他用户");
$char = chr(ord($char)+1);
$objPHPExcel->getActiveSheet()->setCellValue($char . $i,$other." ");
});

//下面是个简答是class   ExcelUtiles.php
require_once __DIR__.'/Excel/Classes/PHPExcel.php' ;
class ExcelUtiles {
public static function create_excel(){
$objExcel = new PHPExcel();
return $objExcel;
}
public static function output_Excel($data,$colums,$names,$name= "未命名",$function = null){

$objPHPExcel = self::create_excel();
$objPHPExcel->getProperties()->setCreator($name);
$objPHPExcel->setActiveSheetIndex(0);
$char = "A";
foreach ($names as $c){
$objPHPExcel->getActiveSheet()->setCellValue($char."1",$c);
$char = chr(ord($char)+1);
}
$objPHPExcel->getActiveSheet()->getColumnDimension("A")->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension("B")->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension("C")->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension("D")->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension("E")->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension("F")->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension("G")->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension("H")->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension("I")->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension("J")->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension("K")->setWidth(20);
$i = 2;
foreach($data as $d){
$j = 0;
$char = "A";
foreach ($colums as $c){
if (is_array($c)){
$func = array_pop($c);
if(is_callable($func)){
foreach ($c as & $paths){
$paths = preg_match("/\./", $paths)?Arr::path($d,$paths):Arr::get($d, $paths);
}
$objPHPExcel->getActiveSheet()->setCellValue($char . $i,call_user_func_array($func, $c)." ");
}else{
$objPHPExcel->getActiveSheet()->setCellValue($char . $i,$d[$c[0]][$func]." ");
}
}else{
if(preg_match("/\./", $c))
$objPHPExcel->getActiveSheet()->setCellValue($char . $i,Arr::path($d, $c)." ");
else
$objPHPExcel->getActiveSheet()->setCellValue($char . $i,Arr::get($d, $c)." ");
}
$char = chr(ord($char)+1);
$j++;
}
$i++;
}
//如果有回调函数则执行。
if ($function){
$function($objPHPExcel,$i);
}
$objPHPExcel->getActiveSheet()->getStyle('A1:AE'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 设置页方向和规模
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel->setActiveSheetIndex(0);
$timestamp = time();
header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition:attachment;filename="'.$name.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');
// $objWriter = newPHPExcel_Writer_Excel2007($objPHPExcel);
// echo 'create weibo report success . count :'.($i-1);
$objWriter->save('php://output');
}
}
0 0
原创粉丝点击