381ebf3fe5151ef3d5aaebda74aecf77.png

下载链接:放到tp3的vendor 下

https://github.com/PHPOffice/PHPExcel

控制器中:
public function get_excel(){
Vendor('phpexcel.PHPExcel'); //七牛入口文件引入
$excel = new PHPExcel();
//Excel表格式,这里简略写了8列
$letter = array('A','B','C','D','E','F','F','G');
//第一个sheet
$excel->setactivesheetindex(0);
//设置sheet标题
$title = '测试';
$excel->getActiveSheet()->setTitle($title);
$data = array(
array('班级','姓名','教室'),
array('1班','bob','301'),
array('2班','selina','302')
);
//填充表格信息
$excel->getActiveSheet()->setCellValue("A1",$title."统计报表");
for ($i = 2;$i <= count($data) + 1;$i++) {
$j = 0;
foreach ($data[$i-2] as $key=>$value) {
if(!strpos($value,'Public/Uploads')){
//文字生成
$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
}else{
// 图片生成
$objDrawing[$key] = new PHPExcel_Worksheet_Drawing();
$objDrawing[$key]->setPath($value);
// 设置宽度高度
$objDrawing[$key]->setHeight(100);//照片高度
//$objDrawing[$k]->setWidth(80); //照片宽度
/*设置图片要插入的单元格*/
$objDrawing[$key]->setCoordinates("$letter[$j]$i");
// 图片偏移距离
$objDrawing[$key]->setOffsetX(50);
$objDrawing[$key]->setOffsetY(10);
$objDrawing[$key]->setWorksheet($excel->getActiveSheet());
}
$j++;
}
}
$write = new PHPExcel_Writer_Excel5($excel);
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="'.$title.'.xls"');
header("Content-Transfer-Encoding:binary");
$write->save('php://output');
}

细节设置:

在get_excel中添加
//设置单元格合并
$excel->getActiveSheet()->mergeCells('A1:F1');
//设置高度
$excel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
//设置宽度
$excel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
//居中(全部)
$excel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$excel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//设置font(字体大小、加粗)
$excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);
$excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
//设置边框
$styleThinBlackBorderOutline = ['borders' => ['allborders' => ['style' => PHPExcel_Style_Border::BORDER_THIN]]];
$excel->getActiveSheet()->getStyle( 'A1:F27')->applyFromArray($styleThinBlackBorderOutline);多表导出(例子):
public function get_excel(){
Vendor('phpexcel.PHPExcel'); //七牛入口文件引入
$excel = new PHPExcel();
//Excel表格式,这里简略写了8列
$letter = array('A','B','C','D','E','F','F','G');
//第一个表
$excel->setactivesheetindex(0);
//设置sheet标题
$title = '测试1';
$excel->getActiveSheet()->setTitle($title);
//表格数据
$data1 = array(
array('班级1','姓名','教室'),
array('1班','bob','301'),
array('2班','selina','302')
);
//填充表格信息
$excel->getActiveSheet()->setCellValue("A1",$title."统计报表");
for ($i = 2;$i <= count($data1) + 1;$i++) {
$j = 0;
foreach ($data1[$i-2] as $key=>$value) {
$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
$j++;
}
}
//第二个表
$excel->createSheet();
$excel->setactivesheetindex(1);
//设置sheet标题
$title = '测试2';
$excel->getActiveSheet()->setTitle($title);
//表格数据
$data2 = array(
array('班级2','姓名','教室'),
array('1班','bob','301'),
array('2班','selina','302')
);
//填充表格信息
$excel->getActiveSheet()->setCellValue("A1",$title."统计报表");
for ($i = 2;$i <= count($data2) + 1;$i++) {
$j = 0;
foreach ($data2[$i-2] as $key=>$value) {
$excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
$j++;
}
}
//导出表格
$write = new PHPExcel_Writer_Excel5($excel);
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="'.$title.'.xls"');
header("Content-Transfer-Encoding:binary");
$write->save('php://output');
}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐