码迷,mamicode.com
首页 > Web开发 > 详细

PHPexcel导出

时间:2020-02-28 19:01:31      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:限制   cti   sql查询   导出   ati   时间   office   orm   ast   

写过很多次的导出,依旧每次导出的时候都会忘记一些必要参数,趁着有时间记录下

参数设置 $header 列头

    $datas 数据
    $fileName 文件名称
    $sheet sheet名称
    $isDown 文件保存到指定文件还是直接输出
 
 public  function excelout($header,$datas,$fileName,$sheet,$isDown=false)
{
ini_set ("memory_limit","-1"); //内存无限制
$obj = new PHPExcel();
$obj->setActiveSheetIndex(0); //激活默认sheet
$obj->getActiveSheet()->setTitle($sheet); //设置激活sheet的名称
//头部及列设置
$jisuan = 0;
foreach ($header as $key=>$headerlist ){
if($key>25){ //当导出列大于25列时处理
$list[]= chr(65).chr(65+$jisuan);
$obj->getActiveSheet()->getColumnDimension(chr(65).chr(65+$jisuan))->setWidth(20);
$obj->getActiveSheet()
->setCellValue($list[$key] . 1, $headerlist);
}else{
$list[]=chr(65+$jisuan);
$obj->getActiveSheet()->getColumnDimension(chr(65+$jisuan))->setWidth(20);
$obj->getActiveSheet()
->setCellValue($list[$key] . 1, $headerlist);
}
if($jisuan == 25){
$jisuan = 0;
}else{
$jisuan++;
}
}
//渲染数据部分
$i = 2;
foreach ($datas as $data){
$j=0;
foreach ($data as $key=>$datalist){
$obj->getActiveSheet()
->setCellValue($list[$j] . $i,$datalist);
$j++;
}
$i++;
}
// header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet‘);
header(‘Content-Disposition: attachment;filename="‘ . $fileName . ‘.xls‘);
header(‘Cache-Control: max-age=1‘);
header("Content-type: application/vnd.ms-excel");
header("Content-Type: application/force-download");
header("Expires: 0");
header("Pragma: no-cache");
$objWriter = \PHPExcel_IOFactory::createWriter($obj, ‘Excel5‘);
if($isDown){ //下载文件到服务器
$url = Core::Instance()->getAppPath();
$file = ‘/temp/‘.$fileName.‘.xlsx‘;
$_savePath = $url.$file;
$mode = 0777;
if (!is_dir($url)){//判断路径是否存在 不存在则创建
@mkdir($url, $mode);
@chmod($url,0777);
if(is_file($_savePath)){//判断文件是否存在,存在则删除
unlink($_savePath);
}
$objWriter->save($_savePath);
return $file;
} else{
if(is_file($_savePath)){
unlink($_savePath);
}
$objWriter->save($_savePath);//保存文件到指定路径
return $file;
}

}else{
header("Content-Disposition:attachment;filename=$fileName.xls");
$objWriter->save(‘php://output‘);exit; 直接输出到浏览器
}

}


//导出单一查询数据量过大时查询分sheet
 ini_set ("memory_limit","-1");
$name = trim( $this->request("name"));
$staffname = trim( $this->request("staffname"));
$start_time = trim($this->request("start_time"));
$end_time = trim($this->request("end_time"));
$header = [‘序列号‘,‘姓名‘]; //列表头
$total = 10000; //总数据条数
$obj = new PHPExcel(); //实例化
$obj->removeSheetByIndex(0); //避免缓存
$sheet = ‘sheet名称‘;
$fileName = "文件名称";
for ($i = 0; $i<=intval($total/100);$i++){//每一百条为一个sheet
$datast = ‘sql查询出来的数据‘;
// var_dump($datast);die;
$obj->createSheet($i); //创建sheet
$obj->setActiveSheetIndex($i);//激活sheet
$obj->getActiveSheet()->setTitle($sheet.$i); //给sheet命名

//头部及列设置
foreach ($header as $key=>$headerlist ){
$list[]=chr(65+$key);
$obj->getActiveSheet()->getColumnDimension(chr(65+$key))->setWidth(20);
$obj->getActiveSheet()
->setCellValue($list[$key] . 1, $headerlist);
}

foreach ($datast[1] as $key=>$value){
$datas[$key][‘id‘]=$value[‘id‘]??‘‘;
$datas[$key][‘name‘]=$value[‘name‘]??‘‘;

}
//内容部分
$n = 2;
foreach ($datas as $data){
$j=0;
foreach ($data as $datalist){
$obj->getActiveSheet()
->setCellValue($list[$j] . $n,$datalist);
$j++;
}
$n++;
}
}

$obj->setActiveSheetIndex(0);
header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet‘);
header("Content-type: application/vnd.ms-excel");
header("Content-Type: application/force-download");
header(‘Content-Disposition: attachment;filename="‘ . $fileName . ‘.xls‘);
header(‘Cache-Control: max-age=1‘);
header("Expires: 0");
header("Pragma: no-cache");
$objWriter = \PHPExcel_IOFactory::createWriter($obj, ‘Excel5‘);
$objWriter->save(‘php://output‘); //浏览器输出



PHPexcel导出

标签:限制   cti   sql查询   导出   ati   时间   office   orm   ast   

原文地址:https://www.cnblogs.com/lucky2017/p/12378325.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!