phpexcel使用方法
<?phpheader("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( 'A1:D1')->applyFromArray(
array(
'font' => array ('bold' => true)
)
);
// 字体大小
$objPHPExcel->getActiveSheet()->getStyle( 'A1:D1')->getFont()->setSize(14);
// 栏目背景颜色
$objPHPExcel->getActiveSheet()->getStyle( 'A1:D1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle( 'A1:D1')->getFill()->getStartColor()->setARGB('0c71c1');
//设置高度
$objActSheet->getDefaultRowDimension()->setRowHeight(30);
// 文字居中
$objStyleA1 = $objPHPExcel->getActiveSheet()->getStyle('A1:D100');
$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;
}
}
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOYAAADICAIAAABzrgqqAAATDUlEQVR4nO2d/28b533H+WfYwPrDsMAe4KWqDaQDiNqZgtpGkUZeA/+QZAmyDTDX1VmL0obt2JqsMJHD1kp9bt3IsxNHaafYWCwgS1wCyWLEaxbJHtpGyBKAE5NYiuWSpiiZkyXy+O2e/XDi8bl7nud43++5h58XHgTU6e456vzK8bnj875PDNnkjTfeUF/k8/nnnntueXmZutqV49vkuXGtTSb7TBqxdSYRS2TaP+SkeGwddWEmEYtLOe2XcSmnX4avkpPiWlf4a7JbVg+ZREwPvhYQPDG7G6jKLi0tSZI0OzvLWm0yuUXOSmqbTPbJc2eprTCTNlM2J8U1RTrCuV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WWZBjBktdRUD/MBpzel+Wk7lewt5egso0lOP32i4u6X0FbAcpaglNlQ6/7pX6wB+sEKGsJTpUFABagLBAxQFkgYoCyQMQAZYGIAcoCEQOUBSJGOMomdmwz/b31qhhAz+Gvsh+e/KbWEEKp3RvJRtsOlAWY2FbW+lSByWfuw3+89fG7qd0b65X3ClNnRp/apb4AZQG72FbW4oSsD0e+KRfOyoWz5axUmEnPXTty9+ZUavfGwtSZT94cTuzYpr4AZQG7OFHWSoTr/ZFtcvk63p7d81ep3RtHn9qV2LFNbaNP7Urt3kgb1+LKdg1X6Wb3UWYaAmLhRFlkIcL1/vFtcuEtvDUrN7WBQWLHNmsDAyvhKmwOde/OFekhHCqLukW4rhzfJs+Na20y2WfSiK1Zcatu4SpDqSNARBwq2zXCNZncImcltU0m++S5s9RWmEnbVNY0XNVZDcQVFifKWolwTTxzn5xNy9n0ZLJPfUE2hq/IbGBgHq7KSRI1KgMIhF93DC48/TV55shksk+eGaY2tq/I0uVX5zpLd/nVi8+i6DH8ui976tGNk8k+eeYItZn6ah94AlAv4eO3XxcSm02au75zUpz2NBagB4jstBjsBiz42lNEVlmgVwFlgYgBygIRA5QFIgYoC0QMUBaIGKAsEDH4zH55RJdvxWAieSThM/vlEaCsiPCZ/fIIUFZE+Mx+eQQoKyLcZr/iUmZ9FgFekgsz0H3BrY6ykBiLENxmv9pm4c8m1lWHcV9wq60sTAePFP8PpFXYiBHal1oAAAAASUVORK5CYII=
目录结构如图
在控制器调用exports方法传递需要显示的数据即可,需要根据具体情况修改
后来发现window下没问题,linux服务器就出现了问题,有个函数已经丢弃。:L $objPHPExcel->getActiveSheet()->getStyle('A1:D100')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('A1:D100')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
这个文本居中修正。 $_ci =& get_instance();
$_ci->load->library('phpexcel');
include_once './PHPExcel/Iofactory';
linux下有问题
构造函数有问题,使用相对路径引入实例化即可。 这个方法不智能,比如A,B,C之类的得自己写,麻烦死了!建议看下这个
http://blog.sina.com.cn/s/blog_7b88ff6d0101fu7d.html
页:
[1]