您好,欢迎访问一九零五行业门户网

PHPExcel用法详细的介绍

phpexcel用法有需要的朋友可以参考一下。
phpexcel下载地址:http://phpexcel.codeplex.com/
导入phpexcel.php或者其他文件
 代码如下 复制代码
require_once 'phpexcel.php'; 
////require_once'phpexcel/writer/excel5.php';     // 用于其他低版本xls 
// or 
////require_once'phpexcel/writer/excel2007.php'; // 用于excel-2007 格式 
// 创建一个处理对象实例 
$objexcel = new phpexcel(); 
// 创建文件格式写入对象实例, uncomment 
////$objwriter = newphpexcel_writer_excel5($objexcel);     // 用于其他版本格式  
// or 
////$objwriter = newphpexcel_writer_excel2007($objexcel); // 用于2007 格式 
$objprops = $objexcel->getproperties ();
//设置创建者
$objprops->setcreator ( 'xululu');
//设置最后修改者
$objprops->setlastmodifiedby(xululu);
//描述
$objprops->setdescription(摩比班级);
//设置标题
$objprops->settitle ( '管理器' );
//设置题目
$objprops->setsubject(officexls test document, demo);
//设置关键字
$objprops->setkeywords ( '管理器' );
//设置分类
$objprops->setcategory ( test);
//工作表设置
$objexcel->setactivesheetindex( 0 );
$objactsheet = $objexcel->getactivesheet ();
//单元格赋值   例:
$objactsheet->setcellvalue ( 'a1', 'id');
$objactsheet->setcellvalue ( 'b1', 'hashcode');
$objactsheet->setcellvalue ( 'c1', 'modelname');
$objactsheet->setcellvalue ( 'd1', 'indexname');
$objactsheet->setcellvalue('a1', '字符串内容');  // 字符串内容  
$objactsheet->setcellvalue('a2', 26);            // 数值 
$objactsheet->setcellvalue('a3', true);          // 布尔值 
$objactsheet->setcellvalue('a4', '=sum(a2:a2)'); // 公式
//自动设置单元格宽度   例:
$objactsheet->getcolumndimension('a')->setautosize(true);
//手动设置单元格的宽度   例:
//$objactsheet->getcolumndimension('a')->setwidth(10);
//导出的文件名
$outputfilename = iconv ( 'utf-8', 'gb2312', 'xululu_'. time() . '.xlsx' );
//直接导出文件
$objwriter->save ( $outputfilename );
//文件直接输出到浏览器
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-excel');
header ( 'content-type:application/octet-stream');
header ( 'content-type:application/download');
header ( 'content-disposition:attachment;filename='. $outputfilename );
header ( 'content-transfer-encoding:binary');
$objwriter->save ( 'php://output');
其他设置:
//显式指定内容类型  
$objactsheet->setcellvalueexplicit('a5','847475847857487584',
phpexcel_cell_datatype::type_string); 
//合并单元格  
$objactsheet->mergecells('b1:c22'); 
//分离单元格  
$objactsheet->unmergecells('b1:c22');
//得到单元格的样式
$objstylea5 = $objactsheet->getstyle('a5');
//设置字体 
$objfonta5 = $objstylea5->getfont();
$objfonta5->setname('courier new'); 
$objfonta5->setsize(10); 
$objfonta5->setbold(true); 
$objfonta5->setunderline(phpexcel_style_font::underline_single); 
$objfonta5->getcolor()->setargb('ff999999');  
//设置对齐方式
$objaligna5 = $objstylea5->getalignment(); 
$objaligna5->sethorizontal(phpexcel_style_alignment::horizontal_right); 
$objaligna5->setvertical(phpexcel_style_alignment::vertical_center);
//设置边框 
$objbordera5 = $objstylea5->getborders(); 
$objbordera5->gettop()->setborderstyle(phpexcel_style_border::border_thin); 
$objbordera5->gettop()->getcolor()->setargb('ffff0000');// color 
$objbordera5->getbottom()->setborderstyle(phpexcel_style_border::border_thin); 
$objbordera5->getleft()->setborderstyle(phpexcel_style_border::border_thin); 
$objbordera5->getright()->setborderstyle(phpexcel_style_border::border_thin); 
//设置填充颜色 
$objfilla5 = $objstylea5->getfill(); 
$objfilla5->setfilltype(phpexcel_style_fill::fill_solid); 
$objfilla5->getstartcolor()->setargb('ffeeeeee');
//从指定的单元格复制样式信息. 
$objactsheet->duplicatestyle($objstylea5,'b1:c22'); 
//*************************************
//添加图片 
$objdrawing = new phpexcel_worksheet_drawing(); 
$objdrawing->setname('zealimg'); 
$objdrawing->setdescription('image inserted byzeal');
$objdrawing->setpath('./zeali.net.logo.gif'); 
$objdrawing->setheight(36); 
$objdrawing->setcoordinates('c23'); 
$objdrawing->setoffsetx(10); 
$objdrawing->setrotation(15); 
$objdrawing->getshadow()->setvisible(true); 
$objdrawing->getshadow()->setdirection(36); 
$objdrawing->setworksheet($objactsheet);
//添加一个新的worksheet
$objexcel->createsheet(); 
$objexcel->getsheet(1)->settitle('测试2'); 
//保护单元格 
$objexcel->getsheet(1)->getprotection()->setsheet(true); 
$objexcel->getsheet(1)->protectcells('a1:c22','phpexcel');
phpexcel在cakephp中应用:
在vendors/下创建一个文件夹excel,将phpexcel的目录如下:
 在要调用的controller下的方法写如下代码:
 代码如下 复制代码
app::import ( 'vendor', 'excel', array ('file' =>'phpexcel.php' ) );
后,就可以实例化phpexcel;具体跟以上情况一样。
(这只是其中的一种方法,还有其他一些方法!)
另:导出excel表格的还有其他一些形式,如php自带的函数fputcsv();也可以导出csv格式的表格。
其它类似信息

推荐信息