excel的编写与生成操作:
复制代码代码如下所示:
include'phpexcel PHP的;
include'phpexcel /作家/和PHP的;
包括or'phpexcel /作家/ Excel5。PHP / .xls输出;
include'phpexcel / iofactory。php; / / PHPExcel工厂类
创建一个excel
objphpexcel美元=新PHPExcel();
保存Excel - 2007格式
objwriter =新phpexcel_writer_excel2007美元(美元objphpexcel);
也可以使用
/ / $ objwriter = phpexcel_iofactory::createwriter($ objphpexcel,和);
/ /或objwriter =新phpexcel_writer_excel5美元(美元objphpexcel);非2007格式
objwriter美元->保存(XXX。xlsx );
直接输出到浏览器
objwriter =新phpexcel_writer_excel5美元(美元objphpexcel);
标题(杂注:公);
标题(过期:0 );
标题(缓存控制:必须重新验证,检查= 0,后预检查= 0 );
页眉(内容类型:应用程序/下载);
标题(内容类型:应用程序/越南盾。微软EXECL );
标题(内容类型:应用程序/字节流);
标题(内容类型:应用程序下载);;
头('content-disposition:依恋;文件名=简历.xls);
标题(内容传输编码:二进制);
objwriter美元->保存('php: / /输出);
直接文件
objwriterr美元->保存(文件名);
设置Excel:
创建
objphpexcel美元-> getProperties()-> setcreator(马腾balliauw );
/最后一个要改变的人
objphpexcel美元-> getProperties()-> setlastmodifiedby(马腾balliauw );
标题
objphpexcel美元-> getProperties()-> settitle(Office 2007 xlsx测试文档);
/主题
objphpexcel美元-> getProperties()-> setsubject(Office 2007 xlsx测试文档);
描述
objphpexcel美元-> getProperties()-> setDescription(测试文档的Office 2007 xlsx,用PHP生成类。);
关键字
objphpexcel美元-> getProperties()-> setkeywords(Office 2007 OPENXML PHP );
类型
objphpexcel美元-> getProperties()-> setcategory(测试结果文件);
设置当前工作表
objphpexcel美元-> setactivesheetindex(0);
设置表名称
objphpexcel美元-> getactivesheet()-> settitle();
设置单元格的值
objphpexcel美元-> getactivesheet()-> setcellvalue(步骤'String');
objphpexcel美元-> getactivesheet()-> setcellvalue('a2 ',12);
objphpexcel美元-> getactivesheet()-> setcellvalue('a3 ',真的);
objphpexcel美元-> getactivesheet()-> setcellvalue('c5 '、' =总和(C2:C4));
objphpexcel美元-> getactivesheet()-> setcellvalue('b8 '、' = min(B2:C5));
合并单元格
objphpexcel美元-> getactivesheet()->设定是否合并内容相同的单元格('a18:E22);
细胞分离
objphpexcel美元-> getactivesheet()-> unmergecells('a28:B28);
细胞保护
objphpexcel美元-> getactivesheet(-> getprotection)(-> setsheet)(真); / /需要被设置为true,以使任何工作表保护!
objphpexcel美元-> getactivesheet()->保护('a3:E13,'phpexcel);
格式
单元格号格式
回声日期(H:我:S)。设置单元格编号格式;
objphpexcel美元-> getactivesheet()-> getstyle('e4)-> getnumberformat()-> setformatcode(phpexcel_style_numberformat::format_currency_eur_simple);
objphpexcel美元-> getactivesheet()()-> duplicatestyle($ objphpexcel -> getactivesheet()-> getstyle('e4),'e5:E13);
设置宽度宽度
列宽度
objphpexcel美元-> getactivesheet()-> getcolumndimension(B)-> setautosize(真的);
objphpexcel美元-> getactivesheet()-> getcolumndimension(会)-> setwidth(12);
设置字体
objphpexcel美元-> getactivesheet()-> getstyle('b1)->获得当前字体()-> setName('candara);
objphpexcel美元-> getactivesheet()-> getstyle('b1)->获得当前字体()->组量(20);
objphpexcel美元-> getactivesheet()-> getstyle('b1)->获得当前字体()-> setbold(真的);
objphpexcel美元-> getactivesheet()-> getstyle('b1)->获得当前字体()-> setunderline(phpexcel_style_font::underline_single);
objphpexcel美元-> getactivesheet()-> getstyle('b1)->获得当前字体()->色鬼()-> setargb(phpexcel_style_color::color_white);
objphpexcel美元-> getactivesheet()-> getstyle('e1)->获得当前字体()->色鬼()-> setargb(phpexcel_style_color::color_white);
objphpexcel美元-> getactivesheet()-> getstyle('d13)->获得当前字体()-> setbold(真的);
objphpexcel美元-> getactivesheet()-> getstyle('e13)->获得当前字体()-> setbold(真的);
设置/对齐
objphpexcel美元-> getactivesheet()-> getstyle('d11)-> getalignment()-> sethorizontal(phpexcel_style_alignment::horizontal_right);
objphpexcel美元-> getactivesheet()-> getstyle('d12)-> getalignment()-> sethorizontal(phpexcel_style_alignment::horizontal_right);
objphpexcel美元-> getactivesheet()-> getstyle('d13)-> getalignment()-> sethorizontal(phpexcel_style_alignment::horizontal_right);
objphpexcel美元-> getactivesheet()-> getstyle('a18)-> getalignment()-> sethorizontal(phpexcel_style_alignment::horizontal_justify);
垂直中心
objphpexcel美元-> getactivesheet()-> getstyle('a18)-> getalignment()-> setvertical(phpexcel_style_alignment::vertical_center);
设置列边框
objphpexcel美元-> getactivesheet()-> getstyle('a4)-> getborders()->公司-> setborderstyle()(phpexcel_style_border::border_thin);
objphpexcel美元-> getactivesheet()-> getstyle('b4)-> getborders()->公司-> setborderstyle()(phpexcel_style_border::border_thin);
objphpexcel美元-> getactivesheet()-> getstyle('c4)-> getborders()->公司-> setborderstyle()(phpexcel_style_border::border_thin);
objphpexcel美元-> getactivesheet()-> getstyle('d4)-> getborders()->公司-> setborderstyle()(phpexcel_style_border::border_thin);
objphpexcel美元-> getactivesheet()-> getstyle('e4)-> getborders()->公司-> setborderstyle()(phpexcel_style_border::border_thin);
设置边框的颜色
objphpexcel美元-> getactivesheet()-> getstyle('d13)-> getborders()-> getLeft()->色鬼()-> setargb('ff993300);
objphpexcel美元-> getactivesheet()-> getstyle('d13)-> getborders()->公司-> GetColor()()-> setargb('ff993300);
objphpexcel美元-> getactivesheet()-> getstyle('d13)-> getborders()-()- > > getbottom色鬼()-> setargb('ff993300);
objphpexcel美元-> getactivesheet()-> getstyle('e13)-> getborders()->公司-> GetColor()()-> setargb('ff993300);
objphpexcel美元-> getactivesheet()-> getstyle('e13)-> getborders()-()- > > getbottom色鬼()-> setargb('ff993300);
objphpexcel美元-> getactivesheet()-> getstyle('e13)-> getborders()-> GetRight()->色鬼()-> setargb('ff993300);
设置填充颜色
objphpexcel美元-> getactivesheet()-> getstyle(步骤)-> getfill()-> setfilltype(phpexcel_style_fill::fill_solid);
objphpexcel美元-> getactivesheet()-> getstyle(步骤)-> getfill()-> getstartcolor()-> setargb('ff808080);
objphpexcel美元-> getactivesheet()-> getstyle('b1)-> getfill()-> setfilltype(phpexcel_style_fill::fill_solid);
objphpexcel美元-> getactivesheet()-> getstyle('b1)-> getfill()-> getstartcolor()-> setargb('ff808080);
添加图片
objdrawing美元=新phpexcel_worksheet_drawing();
objdrawing美元-> setName('logo);
objdrawing美元-> setDescription('logo);
objdrawing美元-> SETPATH('。 /图像/ officelogo .jpg);
objdrawing美元-> SetHeight(36);
objdrawing -> setworksheet美元(美元objphpexcel -> getactivesheet());
objdrawing美元=新phpexcel_worksheet_drawing();
objdrawing美元-> setName('paid);
objdrawing美元-> setDescription('paid);
objdrawing美元-> SETPATH('。 /图像/支付。png);
objdrawing美元-> setcoordinates('b15);
objdrawing美元-> setoffsetx(110);
objdrawing美元->设定(25);
objdrawing美元-> getshadow()-> setVisible(true);
objdrawing美元-> getshadow()-> setdirection(45);
objdrawing -> setworksheet美元(美元objphpexcel -> getactivesheet());
汉语/加工输出问题
需要 / /串成UTF-8编码,可以正常输出,否则输出汉字将是一片空白,如下:
$str = iconv('gb2312 ','utf-8,$str);
或者你可以编写一个专门处理中文字符串的函数:
功能convertutf8($str)
{
如果(空($))返回;
返回iconv('gb2312 ','utf-8,$str);
}

读取Excel
1、导入Excel最简单的方法是使用phpexel IO的工厂,并调用类的静态负荷phpexcel_iofactory。它可以自动识别文件格式,包括Excel2007,excel2003xml,oocalcsylk,Gnumeric和CSV返回PHPExcel实例。
复制代码代码如下所示:
工厂类加载
include'phpexcel / iofactory PHP的;
/ / xls文件路径读取
inputfilename美元= '。 / /那XLS的样本数据;
采用负载phpexcel_iofactory / * * / Excel操作对象
objphpexcel美元= phpexcel_iofactory::负载($ inputfilename);
/ /获取当前的活动形式,叫toArray方法得到的二维数组的形式
sheetdata美元=美元objphpexcel -> getactivesheet()-> toArray(空的,真的,真的,真的);
var_dump($ sheetdata);

1。创建一个excelreader加载一个Excel文件
如果您知道这个Excel文档的格式,您可以创建一个相应的读取器来加载要读取的Excel文档,但是如果加载错误的文档类型,则会产生不可预知的错误。
复制代码代码如下所示:
inputfilename美元= '。 / /那XLS的样本数据;
创建一个新的Excel5读者* / / * *
objreader美元=新phpexcel_reader_excel5();
/ / $ objreader =新(phpexcel_reader_excel2007);
/ / $ objreader =新(phpexcel_reader_excel2003xml);
/ / $ objreader =新(phpexcel_reader_oocalc);
/ / $ objreader =新(phpexcel_reader_sylk);
/ / $ objreader =新(phpexcel_reader_gnumeric);
/ / $ objreader =新(phpexcel_reader_csv);
负载inputfilename美元到PHPExcel对象 / * /
objphpexcel美元=美元objreader ->负荷($ inputfilename);
获取当前表的活动
cursheet美元=美元objphpexcel -> getactivesheet();
将此表单返回到二维数组/表单数据
sheetdata美元=美元cursheet -> toArray(空的,真的,真的,真的);
var_dump($ sheetdata);

你也可以使用phpexcel_iofactory的createreader方法得到读者对象无需知道文件读取的格式。
复制代码代码如下所示:
inputfiletype美元= 'excel5;
/ / $ inputfiletype = 'excel2007;
/ / $ inputfiletype = 'excel2003xml;
/ / $ inputfiletype = 'oocalc;
/ / $ inputfiletype = 'sylk;
/ / $ inputfiletype = 'gnumeric;
/ / $ inputfiletype = 'csv;
inputfilename美元= '。 / /那XLS的样本数据;
创建一个新的读者 / *类型中定义的inputfiletype美元** /
objreader美元= phpexcel_iofactory::createreader($ inputfiletype);
负载inputfilename美元到PHPExcel对象 / * /
objphpexcel美元=美元objreader ->负荷($ inputfilename);
获取当前表的活动
cursheet美元=美元objphpexcel -> getactivesheet();
将此表单返回到二维数组/表单数据
sheetdata美元=美元cursheet -> toArray(空的,真的,真的,真的);
var_dump($ sheetdata);

如果读取文件之前的文件格式是未知的,你可以通过识别的文件类型()方法iofactory,然后穿读者通过createreader()方法。
复制代码代码如下所示:
inputfilename美元= '。 / /那XLS的样本数据;
确定输入文件的格式***
inputfiletype美元= phpexcel_iofactory::识别($ inputfilename);
读者应该戴一个相对的***。
objreader美元= phpexcel_iofactory::createreader($ inputfiletype);
加载文件以读取***
objphpexcel美元=美元objreader ->负荷($ inputfilename);

2。设置Excel的阅读选项
在使用加载()方法加载文件之前,您可以设置读取选项来控制负载的行为。
2.1.readingonly数据从电子表格文件
setreaddataonly()方法的配置,读者不注意的表数据的数据类型和字符串格式返回
复制代码代码如下所示:
inputfiletype美元= 'excel5;
inputfilename美元= '。 / /那XLS的样本数据;
创建一个新的读者 / *类型中定义的inputfiletype美元** /
objreader美元= phpexcel_iofactory::createreader($ inputfiletype);
单元格字符串中的配置数据**
objreader美元-> setreaddataonly(真的);
负载inputfilename美元到PHPExcel对象 / * /
objphpexcel美元=美元objreader ->负荷($ inputfilename);
sheetdata美元=美元objphpexcel -> getactivesheet()-> toArray(空的,真的,真的,真的);
var_dump($ sheetdata);

返回数据:
复制代码代码如下所示:
数组(8){
{ 1 } >
数组(6){
{ }
字符串(15)整数
{B}
字符串(3)123
{
字符串(3)234
{d}
字符串(4)345
{e}
字符串(3)456
{f}
无效的
}
{ 2 } >
数组(6){
{ }
字符串(22)浮pointnumbers
{B}
字符串(4)1.23
{
字符串(5)23.45
{d}
字符串(10)0.00e + 0
{e}
字符串(6)45.68
{f}
字符串(7和56.78)
}
{ 3 } >
数组(6){
{ }
字符串(7)字符串
{B}
字符串(5)hello
{
字符串(5)世界
{d}
无效的
{e}
字符串(8)PHPExcel
{f}
无效的
}
{ 4 } >
数组(6){
{ }
字符串(8)布尔
{B}
布尔(真的)
{
Bool(假)
{d}
无效的
{e}
无效的
{f}
无效的
}
{ 5 } >
数组(6){
{ }
字符串(5)日期
{B}
字符串(16)1960年12月19日
{
字符串(15)2010年10月10日
{d}
无效的
{e}
无效的
{f}
无效的
}
{ 6 } >
数组(6){
{ }
字符串(5)时间
{B}
字符串(4)9:30
{
字符串(5)23:59
{d}
无效的
{e}
无效的
{f}
无效的
}
{ 7 } >
数组(6){
{ }
字符串(8)公式
{B}
字符串(3)468
{
字符串(7)20.998
{d}
无效的
{e}
无效的
{f}
无效的
}
{ 8 } >
数组(6){
{ }
字符串(6)错误
{B}
字符串(4)# N /
{
字符串(7)# DIV / 0!
{d}
无效的
{e}
无效的
{f}
无效的
}
}
如果没有设置,返回:
数组(8){
{ 1 } >
数组(6){
{ }
字符串(15)整数
{B}
浮子(123)
{
浮子(234)
{d}
浮子(- 345)
{e}
浮子(456)
{f}
无效的
}
{ 2 } >
数组(6){
{ }
字符串(22)浮点数
{B}
浮子(1.23)
{
浮子(23.45)
{d}
浮(3.45e-6)
{e}
浮子(- 45.678)
{f}
浮子(56.78)
}
{ 3 } >
数组(6){
{ }
字符串(7)字符串
{B}
字符串(5)hello
{
字符串(5)世界
{d}
无效的
{e}
字符串(8)PHPExcel
{f}
无效的
}
{ 4 } >
数组(6){
{ }
字符串(8)布尔
{B}
布尔(真的)
{
Bool(假)
{d}
无效的
{e}
无效的
{f}
无效的
}
{ 5 } = >
数组(6){
{ }
字符串(5)日期
{B}
浮子(22269)
{
浮子(40461)
{d}
无效的
{e}
无效的
{f}
无效的
}
{ 6 } >
数组(6){
{ }
字符串(5)时间
{B}
浮子(0.39583333333333)
{
浮子(0.99930555555556)
{d}
无效的
{e}
无效的
{f}
无效的
}
{ 7 } >
数组(6){
{ }
字符串(8)公式
{B}
浮子(468)
{
浮子(- 20.99799655)
{d}
无效的
{e}
无效的
{f}
无效的
}
{ 8 } >
数组(6){
{ }
字符串(6)错误
{B}
字符串(4)# N /
{
字符串(7)# DIV / 0!
{d}
无效的
{e}
无效的
{f}
无效的
}
}

从spreadsheetfile适用于读者的阅读资料:
是的是的是的excel2003xml Excel2007 Excel5
oocalc是SYLK Gnumeric是没有
CSV没有

2.2.readingonly命名文件中的工作表
setloadsheetsonly(),设置工作表读取,并接受该工作表的名称作为参数。
复制代码代码如下所示:
phpexcel_iofactory / * * /
include'phpexcel / iofactory PHP的;
inputfiletype美元= 'excel5;
/ / $ inputfiletype = 'excel2007;
/ / $ inputfiletype = 'excel2003xml;
/ / $ inputfiletype = 'oocalc;
/ / $ inputfiletype = 'gnumeric;
inputfilename美元= '。 / /那XLS的样本数据;
sheetname美元=数据表# 2;

echo'loading文件',是($ inputfilename,pathinfo_basename),使用iofactory与definedreader型',';
objreader美元= phpexcel_iofactory::createreader($ inputfiletype);
echo'loading表,sheetname美元,只有;
objreader -> setloadsheetsonly美元(美元sheetname);
objphpexcel美元=美元objreader ->负荷($ inputfilename);
回声';
(echo $ objphpexcel -> getsheetcount),'表'(($ objphpexcel -> getsheetcount)=(1):','装' ');
loadedsheetnames美元=美元objphpexcel -> getsheetnames();
foreach(loadedsheetnames美元美元美元sheetindex = > loadedsheetname){
回声sheetindex美元,loadedsheetname美元,->;
}

如果您想读取多个工作表,可以传递数组。
复制代码代码如下所示:
inputfiletype美元= 'excel5;
inputfilename美元= '。 / /那XLS的样本数据;
sheetnames美元=阵列(数据表# 1,数据表# 3);
创建一个新的读者 / *类型中定义的inputfiletype美元** /
objreader美元= phpexcel_iofactory::createreader($ inputfiletype);
通知读者我们要加载的工作表是什么
objreader -> setloadsheetsonly美元(美元sheetnames);
负载inputfilename美元到PHPExcel对象 / * /
objphpexcel美元=美元objreader ->负荷($ inputfilename);

如果你想读的所有工作表,你可以叫setloadallsheets()。