PHPExcel导出

原创 2016年05月31日 14:21:04
该方法是我根据自己项目需求写的,,有些细节地方需要根据情况修改。
我简单解说下参数:
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 = new PHPExcel_Writer_Excel2007($objPHPExcel);
// echo 'create weibo report success . count :'.($i-1);
$objWriter->save('php://output');
}
}

phpexcel数据导出分多个工作区(sheet)

1、数据源 $expTableData1 = [ [['name' => 'ddd' , 'age' => 12] , ['name' => 'yyy' , 'age' =>...
  • jjl123jjl123
  • jjl123jjl123
  • 2017年03月13日 09:51
  • 326

PHPExcel操作excel、csv格式的文件

/** * * @author Sam@ozchamp.net * */ class Excel { public $currentSheet; public $filePath; ...
  • renzhenhuai
  • renzhenhuai
  • 2014年04月11日 11:20
  • 6515

PHP:使用PHPExcel完成电子表格文件的导出下载和导入操作

使用PHPExcel完成电子表格文件的导出下载和导入操作
  • zyb_icanplay7
  • zyb_icanplay7
  • 2013年08月02日 15:12
  • 1505

thinkphp安装插件phpExcel实现导出导入数据实例教程

插件phpExcel 版本:1.8.0 下载地址:http://www.chtml.cn/topic/show/40 实例使用的thinkphp版本3.2.3 适用个版本的th...
  • hj960511
  • hj960511
  • 2016年07月14日 10:30
  • 2192

使用phpexcel导出到xls文件的时候出现乱码解决【已解决】

  • github_26672553
  • github_26672553
  • 2015年06月04日 10:36
  • 927

phpexcel 不确定结束单元格的导出并循环

当不确定结束单元格的时候,可以通过开始的单元格,进行字符格式转换计算出结束单元格并进行导出...
  • phperguan
  • phperguan
  • 2016年06月16日 11:17
  • 1361

PHP使用PHPExcel导出,导入数据总结

导出excel: header("Content-type: text/html; charset=utf-8"); include_once dirname(dir...
  • hwhjava
  • hwhjava
  • 2015年08月27日 10:20
  • 3530

PHPexcel导出_带合并单元格/边框背景/_有效果图_thinkphp

##供稿单位导出 public function gonggao_excel(){ $color='0xCC000000'; //查询所有供稿单位 ...
  • fengyu19880219
  • fengyu19880219
  • 2016年02月04日 14:52
  • 6006

PHPExcel生成图表

PHPExcel下载 自带的例子: 生成折线图示例:
  • qq_20480611
  • qq_20480611
  • 2015年08月26日 15:14
  • 3051

phpexcel 导出 基本设置

**//引入类** ----- header('Content-type: text/html; charset=utf-8'); import('Org.Util.PH...
  • sinat_33750162
  • sinat_33750162
  • 2016年07月11日 10:01
  • 1439
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:PHPExcel导出
举报原因:
原因补充:

(最多只允许输入30个字)