PHPexcelエクスポート_結合セル/外枠付き背景/効果図ありthinkphp
30277 ワード
##
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;
}