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

php excel操作类phpExcel用法介绍

phpexcel是php中一个excel插件操作类,可以很好的解决在excel各种操作,包括如,读,写,删除,插入等excel操作,下面笔者来给各位同学介绍介绍phpexcel用法吧。
下面是总结的几个使用方法
include ‘phpexcel.php’;
include ‘phpexcel/writer/excel2007.php’;
//或者include ‘phpexcel/writer/excel5.php’; 用于输出.xls的
创建一个excel
$objphpexcel = new phpexcel();
保存excel—2007格式
$objwriter = new phpexcel_writer_excel2007($objphpexcel);
//或者$objwriter = new phpexcel_writer_excel5($objphpexcel); 非2007格式
$objwriter->save(”xxx.xlsx”);
直接输出到浏览器
$objwriter = new phpexcel_writer_excel5($objphpexcel);
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=”resume.xls”‘);
header(”content-transfer-encoding:binary”);
$objwriter->save(’php://output’);
——————————————————————————————————————–
设置excel的属性:
创建人
$objphpexcel->getproperties()->setcreator(”maarten balliauw”);
最后修改人
$objphpexcel->getproperties()->setlastmodifiedby(”maarten balliauw”);
标题
$objphpexcel->getproperties()->settitle(”office 2007 xlsx test document”);
题目
$objphpexcel->getproperties()->setsubject(”office 2007 xlsx test document”);
描述
$objphpexcel->getproperties()->setdescription(”test document for office 2007 xlsx, generated using php classes.”);
关键字
$objphpexcel->getproperties()->setkeywords(”office 2007 openxml php”);
种类
$objphpexcel->getproperties()->setcategory(”test result file”);
——————————————————————————————————————–
设置当前的sheet
$objphpexcel->setactivesheetindex(0);
设置sheet的name
$objphpexcel->getactivesheet()->settitle(’simple’);
设置单元格的值
$objphpexcel->getactivesheet()->setcellvalue(’a1′, ‘string’);
$objphpexcel->getactivesheet()->setcellvalue(’a2′, 12);
$objphpexcel->getactivesheet()->setcellvalue(’a3′, true);
$objphpexcel->getactivesheet()->setcellvalue(’c5′, ‘=sum(c2:c4)’);
$objphpexcel->getactivesheet()->setcellvalue(’b8′, ‘=min(b2:c5)’);
合并单元格
$objphpexcel->getactivesheet()->mergecells(’a18:e22′);
分离单元格
$objphpexcel->getactivesheet()->unmergecells(’a28:b28′);
生成excel常用方法
//设置phpexcel类库的include path
set_include_path('.'. path_separator .
'd:zealphp_libs' . path_separator .
get_include_path());
require_once 'phpexcel.php';
// uncomment
////require_once 'phpexcel/writer/excel5.php'; // 用于其他低版本xls
// or
////require_once 'phpexcel/writer/excel2007.php'; // 用于 excel-2007 格式
// 创建一个处理对象实例
$objexcel = new phpexcel();
// 创建文件格式写入对象实例, uncomment
////$objwriter = new phpexcel_writer_excel5($objexcel); // 用于其他版本格式
// or
////$objwriter = new phpexcel_writer_excel2007($objexcel); // 用于 2007 格式
//$objwriter->setoffice2003compatibility(true);
//*************************************
//设置文档基本属性
$objprops = $objexcel->getproperties();
$objprops->setcreator(zeal li);
$objprops->setlastmodifiedby(zeal li);
$objprops->settitle(office xls test document);
$objprops->setsubject(office xls test document, demo);
$objprops->setdescription(test document, generated by phpexcel.);
$objprops->setkeywords(office excel phpexcel);
$objprops->setcategory(test);
//*************************************
//设置当前的sheet索引,用于后续的内容操作。
//一般只有在使用多个sheet的时候才需要显示调用。
//缺省情况下,phpexcel会自动创建第一个sheet被设置sheetindex=0
$objexcel->setactivesheetindex(0);
$objactsheet = $objexcel->getactivesheet();
//设置当前活动sheet的名称
$objactsheet->settitle('测试sheet');
//*************************************
//设置单元格内容
//
//由phpexcel根据传入内容自动判断单元格内容类型
$objactsheet->setcellvalue('a1', '字符串内容'); // 字符串内容
$objactsheet->setcellvalue('a2', 26); // 数值
$objactsheet->setcellvalue('a3', true); // 布尔值
$objactsheet->setcellvalue('a4', '=sum(a2:a2)'); // 公式
//显式指定内容类型
$objactsheet->setcellvalueexplicit('a5', '847475847857487584',
phpexcel_cell_datatype::type_string);
//合并单元格
$objactsheet->mergecells('b1:c22');
//分离单元格
$objactsheet->unmergecells('b1:c22');
//*************************************
//设置单元格样式
//
//设置宽度
$objactsheet->getcolumndimension('b')->setautosize(true);
$objactsheet->getcolumndimension('a')->setwidth(30);
$objstylea5 = $objactsheet->getstyle('a5');
//设置单元格内容的数字格式。
//
//如果使用了 phpexcel_writer_excel5 来生成内容的话,
//这里需要注意,在 phpexcel_style_numberformat 类的 const 变量定义的
//各种自定义格式化方式中,其它类型都可以正常使用,但当setformatcode
//为 format_number 的时候,实际出来的效果被没有把格式设置为0。需要
//修改 phpexcel_writer_excel5_format 类源代码中的 getxf($style) 方法,
//在 if ($this->_biff_version == 0x0500) { (第363行附近)前面增加一
//行代码:
//if($ifmt === '0') $ifmt = 1;
//
//设置格式为phpexcel_style_numberformat::format_number,避免某些大数字
//被使用科学记数方式显示,配合下面的 setautosize 方法可以让每一行的内容
//都按原始内容全部显示出来。
$objstylea5
->getnumberformat()
->setformatcode(phpexcel_style_numberformat::format_number);
//设置字体
$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 by zeal');
$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');
//*************************************
//输出内容
//
$outputfilename = output.xls;
//到文件
////$objwriter->save($outputfilename);
//or
//到浏览器
////header(content-type: application/force-download);
////header(content-type: application/octet-stream);
////header(content-type: application/download);
////header('content-disposition:inline;filename='.$outputfilename.'');
////header(content-transfer-encoding: binary);
////header(expires: mon, 26 jul 1997 05:00:00 gmt);
////header(last-modified: . gmdate(d, d m y h:i:s) . gmt);
////header(cache-control: must-revalidate, post-check=0, pre-check=0);
////header(pragma: no-cache);
////$objwriter->save('php://output');
?>
其它类似信息

推荐信息