|
<?php
header("Content-type: text/html;charset=utf-8");
class Excels
{
/**
*
*/
private $_ci;
private $data = array();
private $title = array();
public function __construct()
{
$_ci =& get_instance();
$_ci->load->library('phpexcel');
include_once './PHPExcel/Iofactory';
}
// 接收 数据数组
public function exports($data,$underling)
{
$objPHPExcel = new PHPExcel();
$this->data = $data;
$this->underling=$underling;
//设置属性
$objPHPExcel->getProperties()->setCreator("SinoGrid")
->setLastModifiedBy("SinoGrid")
->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("SinoGrid");
$objActSheet = $objPHPExcel->getActiveSheet();
foreach($this->underling as $key => $val){
//添加一个新的worksheet
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex($key);
$objActSheet = $objPHPExcel->getActiveSheet();
//设置当前活动sheet的名称
$objActSheet->setTitle($val["username"]);
//设置宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$objPHPExcel->getActiveSheet() ->getColumnDimension('B')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(115);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(115);
// 设置字体
$objPHPExcel->getActiveSheet()->getStyle( 'A11')->applyFromArray(
array(
'font' => array ('bold' => true)
)
);
// 字体大小
$objPHPExcel->getActiveSheet()->getStyle( 'A11')->getFont()->setSize(14);
// 栏目背景颜色
$objPHPExcel->getActiveSheet()->getStyle( 'A11')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle( 'A11')->getFill()->getStartColor()->setARGB('0c71c1');
//设置高度
$objActSheet->getDefaultRowDimension()->setRowHeight(30);
// 文字居中
$objStyleA1 = $objPHPExcel->getActiveSheet()->getStyle('A1100');
$objAlignA1 = $objStyleA1->getAlignment();
$objAlignA1->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objAlignA1->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//设置标题
$objActSheet->setCellValue('A1', '用户名');
$objActSheet->setCellValue('B1', '日报时间');
$objActSheet->setCellValue('C1', '本日总结');
$objActSheet->setCellValue('D1', '明日计划');
$num=2;
foreach($this->data as $k =>$v){
if($v["daily_user"]==$val["username"]){
$objActSheet->setCellValue("A".$num,$v["daily_user"]);
$objActSheet->setCellValue("B".$num,$v["daily_time"]);
$objActSheet->setCellValue("C".$num,$v["today"]);
$objActSheet->setCellValue("D".$num,$v["tomorrow"]);
$objActSheet ->getStyle("C".$num)->getAlignment()->setWrapText(true);//自动换行
$objActSheet ->getStyle("D".$num)->getAlignment()->setWrapText(true);//自动换行
$num++;
}
}
}
// 清除缓冲
ob_clean();
//操作句柄 输出文件
$objPHPExcel->setActiveSheetIndex(0);
$name = date("Y-m-d H:i");
$name .= "pdca.xls";
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Pragma:public');
header('Content-Type:application/x-msexecl;name="'.$name.'"');
header('Content-Disposition:inline;filename="'.$name.'"');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
}
目录结构如图
在控制器调用exports方法传递需要显示的数据即可,需要根据具体情况修改
|
|