永发信息网

thinkphp 导出excel 怎么合并单元格

答案:2  悬赏:10  手机版
解决时间 2021-02-22 10:20
thinkphp 导出excel 怎么合并单元格
最佳答案
PHPexcel导出_带合并单元格/边框背景/_有效果图_thinkphp
##供稿单位导出
public function gonggao_excel(){
$color='0xCC000000';
//查询所有供稿单位
$order='paixu asc,id desc';
$where=array('token'=>$this->token,'status'=>'1');
$data=M('gonggao')->where($where)->order($order)->select();
//增加供稿单位的文章数量
foreach ($data as $key => $value) {
$data[$key]['shuliang']=M('img')->where(array('gonggaoid'=>$value['id']))->count();
$data[$key]['title']=M('img')->where(array('gonggaoid'=>$value['id']))->getfield('title',true);
}
// //查询出所有行数
// $all_count=0;
// foreach ($data as $key => $value) {
// if(!empty($value['title'])){
// $all_count=$all_count+count($value['title']);
// }else{
// $all_count++;
// }
//重组数组
foreach ($data as $key => $value) {
if(!empty($value['title'])){
foreach ($value['title'] as $k => $v) {
$newData[]=$v;
}

}else{
$newData[]='';
}
}

//dump($newData);
//dump($data);
//die;

// 引用phpexcel类
import('Lib/ORG/PHPExcel');
// 创建对象
$objPHPExcel = new PHPExcel();
// 显示错误信息
error_reporting(E_ALL);
// Set properties
$objPHPExcel->getProperties()->setCreator("赵英杰")
->setLastModifiedBy("赵英杰")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// 设置宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(60);
// 设置行高度
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20); //设置默认行高
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30); //第一行行高
$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20); //第二行行高
// 字体和样式
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(12); //字体大小
$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getFont()->setBold(false); //第二行是否加粗
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); //第一行是否加粗
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16); //第一行字体大小
// 设置垂直居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

//边框样式
$styleArray = array(
'borders' => array(
'allborders' => array(
//'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框
'color' => array('argb' => $color),
),
),
);
$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->applyFromArray($styleArray);
//边框
//$objPHPExcel->getActiveSheet()->getStyle('A2:D2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
// 设置水平居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

// 合并
$objPHPExcel->getActiveSheet()->mergeCells('A1:D1');

// 表头
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '供稿单位文章统计')
->setCellValue('A2', '编号')
->setCellValue('B2', '供稿单位')
->setCellValue('C2', '文章数量')
->setCellValue('D2', '文章名称');
// 内容
// for ($i = 0, $len = count($data); $i < $len; $i++) {
// $objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($i + 3), $i);
// $objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($i + 3), $data[$i]['gonggao']);
// $objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($i + 3), $data[$i]['shuliang']);
// $objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($i + 3), $data[$i]['status']);
// $objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
// $objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
// $objPHPExcel->getActiveSheet()->getRowDimension($i + 3)->setRowHeight(16);
// }

//重构内容-文章名称录入
for ($i = 0, $len = count($newData); $i < $len; $i++) {
$objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($i + 3), $newData[$i]);
$objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//左对齐
$objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
//边框设置
//$objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
// $objPHPExcel->getActiveSheet()->getRowDimension($i + 3)->setRowHeight(16); //重构行高
$objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 3) . ':D' . ($i + 3))->applyFromArray($styleArray);
}

// //查询出所有行数
// $all_count=0;
// foreach ($data as $key => $value) {
// if(!empty($value['title'])){
// $all_count=$all_count+count($value['title']);
// }else{
// $all_count++;
// }

//合并对应单元格并录入编号/供稿单位/数量。
$ii=3;//从第三行开始的
for ($i = 0, $len = count($data); $i < $len; $i++) {
if(count($data[$i]['title'])>0){
//合并 注意-1,因为当前单位有内容且是第一篇
$objPHPExcel->getActiveSheet()->mergeCells('A' . ($ii) . ':A' . ($ii-1+count($data[$i]['title']) ));
$objPHPExcel->getActiveSheet()->mergeCells('B' . ($ii) . ':B' . ($ii-1+count($data[$i]['title']) ));
$objPHPExcel->getActiveSheet()->mergeCells('C' . ($ii) . ':C' . ($ii-1+count($data[$i]['title']) ));

//录入记录
$objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($ii), $i+1);
$objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($ii), $data[$i]['gonggao']);
$objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($ii), count($data[$i]['title']));

//居中
$objPHPExcel->getActiveSheet()->getStyle('A' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

$ii=$ii-1+count($data[$i]['title']);
$ii++;
}else{
//录入记录
$objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($ii), $i+1);
$objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($ii), $data[$i]['gonggao']);
$objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($ii), count($data[$i]['title']));

//居中
$objPHPExcel->getActiveSheet()->getStyle('A' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C' . ($ii))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$ii++;
}
}

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('供稿单位文章统计');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// 输出
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . '供稿单位' . '.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
全部回答
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
构建模式图,探究地理基本原理、过程、成因及
符号学与现象学的关系怎样
建德市洪鼎贸易有限公司地址在什么地方,想过
儿童换多少颗牙
芝罘区城市管理行政执法大队机动中队地址有知
北京邮电大学-体育部在哪里啊,我有事要去这
为什么IE浏览器安全级别不能设置为低
东边套高层容易漏水还是底层吗
临床应用研究栏目论文能否晋级副高
石鼓镇贾寨村卫生室这个地址在什么地方,我要
在古代有一个七岁的小孩,听到了大人的说话,
农村信用社贷款100W还不上能给定什么罪,定罪
(每空1分,共8分)在下面的横线上写出概念图
知柏地黄丸女人能吃吗
急需一组与渔舟有关的古诗词,意境深幽的那种
推荐资讯
一百万韩元等于多少人民币?
厦门超鼎科技有限公司怎么样?
我明天要到理发店学习,首先第一步做的是什么
从宾王客用站到义乌稠州北路怎么走?
精华机械厂在哪里啊,我有事要去这个地方
我家小孩2016年10月满6岁,可以上一年级了吗?
城区人民医院中心门诊部在什么地方啊,我要过
芦荟要不要浇水
英语语法问题I'm going to go cry和I&ap
阜德园1~4幢东门地址在哪,我要去那里办事
中山到清远佛岗有高铁或地铁吗
小学生自已去商店买东西的感想
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?