咨询邮箱 咨询邮箱:mail@qrkj.com.cn 咨询热线 咨询热线:400-0934-799 庆软科技微博 庆软科技微信
您的位置:主页 > 新闻资讯 > 网站优化 >
phpExcel列合并 mergeCellsByColumnAndRow Thinkphp案例
发表日期:2016-07-12 11:11   文章编辑:admin    浏览次数:
 效果图


Thinkphp代码块
Vendor('PHPExcel.PHPExcel');
Vendor('PHPExcel.PHPExcel/Writer/Excel2007');
// include 'PHPExcel/Writer/Excel2007.php';
//或者include 'PHPExcel/Writer/Excel5.php'; 用于输出.xls的
$objExcel = new PHPExcel();
 
$objWriter = new PHPExcel_Writer_Excel2007($objExcel);
//或者$objWriter = new PHPExcel_Writer_Excel5($objExcel); 非2007格式
$objWriter = new PHPExcel_Writer_Excel5($objExcel);
header('Pragma: public');
header('Expires: 0');
header('Cache-Control:must-revalidate, post-check=0, pre-check=0');
header('Content-Type:application/force-download');
header('Content-Type:application/vnd.ms-execl');
header('Content-Type:application/octet-stream');
header('Content-Type:application/download');
header('Content-Disposition:attachment;filename=示范性划到簿.xls');
header('Content-Transfer-Encoding:binary');
 
$objExcel->setActiveSheetIndex(0);
$objActSheet = $objExcel->getActiveSheet();
 
//设置当前活动sheet的名称
//$objActSheet->setTitle('月增减变动报表');
 
 
//*************************************
//
//设置宽度,这个值和EXCEL里的不同,不知道是什么单位,略小于EXCEL中的宽度
$objActSheet->getColumnDimension('A')->setWidth(6);
$objActSheet->getColumnDimension('B')->setWidth(10);
$objActSheet->getColumnDimension('C')->setWidth(8);
$objActSheet->getColumnDimension('D')->setWidth(20);
$objActSheet->getColumnDimension('E')->setWidth(18);
$objActSheet->getColumnDimension('F')->setWidth(16);
$objActSheet->getColumnDimension('G')->setWidth(13);
$objActSheet->getColumnDimension('H')->setWidth(13);
$objActSheet->getColumnDimension('I')->setWidth(13);
$objActSheet->getColumnDimension('J')->setWidth(13);
 
$objActSheet->getRowDimension(1)->setRowHeight(30);
$objActSheet->getRowDimension(2)->setRowHeight(16);
$objActSheet->getRowDimension(3)->setRowHeight(16);
 
$objActSheet->mergeCellsByColumnAndRow(0,1,9,1);
$objActSheet->mergeCellsByColumnAndRow(0,2,9,2);
$objActSheet->mergeCellsByColumnAndRow(0,3,0,4);
$objActSheet->mergeCellsByColumnAndRow(1,3,1,4);
$objActSheet->mergeCellsByColumnAndRow(2,3,2,4);
$objActSheet->mergeCellsByColumnAndRow(3,3,3,4);
$objActSheet->mergeCellsByColumnAndRow(4,3,4,4);
$objActSheet->mergeCellsByColumnAndRow(5,3,5,4);
 
$objActSheet->mergeCellsByColumnAndRow(6,3,7,3);
$objActSheet->mergeCellsByColumnAndRow(8,3,9,3);
 
//设置居中对齐
$objActSheet->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('B3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('C3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('D3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('E3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('F3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('G3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('H3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('I3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('G4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('H4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('I4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objActSheet->getStyle('J4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
 
$objActSheet->getStyle('A1')->applyFromArray(
array(
'font' => array(
'name'         => 'Times New Roman',
'bold'         => true,
'italic'    => false,
'size'        => 20
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical'   => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'wrap'       => true
)
)
);
 
$objActSheet->getStyle('A2')->applyFromArray(
array(
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
'vertical'   => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'wrap'       => true
)
)
);
 
$objActSheet->duplicateStyleArray(
array(
'font' => array(
'name'         => 'Times New Roman',
'bold'         => false,
'italic'    => false,
'size'        => 12
),
'borders' => array(
'top'        => array('style' => PHPExcel_Style_Border::BORDER_THIN),
'bottom'    => array('style' => PHPExcel_Style_Border::BORDER_THIN),
'left'        => array('style' => PHPExcel_Style_Border::BORDER_THIN),
'right'        => array('style' => PHPExcel_Style_Border::BORDER_THIN)
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical'   => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'wrap'       => true
)
),
'A3:J4'
);
 
$objActSheet->SetCellValue('A1', '庆软科技农村创业和技能带头人示范性培训人员划到簿');
$objActSheet->SetCellValue('A2', '年           月          日');
$objActSheet->SetCellValue('A3', '序号');
$objActSheet->SetCellValue('B3', '姓名');
$objActSheet->SetCellValue('C3', '性别');
$objActSheet->SetCellValue('D3', '身份证号码');
$objActSheet->SetCellValue('E3', '家庭地址');
$objActSheet->SetCellValue('F3', '联系电话');
$objActSheet->SetCellValue('G3', '上   午');
$objActSheet->SetCellValue('I3', '下  午');
$objActSheet->SetCellValue('G4', '10:00');
$objActSheet->SetCellValue('H4', '12:00');
$objActSheet->SetCellValue('I4', '13:00');
$objActSheet->SetCellValue('J4', '15:00');

//循环遍历数据填充到excel
//...................

//另存excel
$objWriter->save('php://output');

 

标签:phpExcel,列合并    
如没特殊注明,文章均为资源网络原创,转载请注明来自http://www.qrkj.com.cn/news/wangzhanyouhua/2016/0712/42.html
相关文章推荐