对phpexcel的若干补充

导出excel属性设置
//Include class
require_once(‘Classes/PHPExcel.php‘);
require_once(‘Classes/PHPExcel/Writer/Excel2007.php‘);
$objPHPExcel = new PHPExcel();

//Set properties 设置文件属性
$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");

//Add some data 添加数据
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue(‘A1‘, ‘Hello‘);//可以指定位置
$objPHPExcel->getActiveSheet()->setCellValue(‘A2‘, true);
$objPHPExcel->getActiveSheet()->setCellValue(‘A3‘, false);
$objPHPExcel->getActiveSheet()->setCellValue(‘B2‘, ‘world!‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘B3‘, 2);
$objPHPExcel->getActiveSheet()->setCellValue(‘C1‘, ‘Hello‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘D2‘, ‘world!‘);

//循环
for($i = 1;$i<200;$i++) {
$objPHPExcel->getActiveSheet()->setCellValue(‘A‘ . $i, $i);
$objPHPExcel->getActiveSheet()->setCellValue(‘B‘ . $i, ‘Test value‘);
}

//日期格式化
$objPHPExcel->getActiveSheet()->setCellValue(‘D1‘, time());
$objPHPExcel->getActiveSheet()->getStyle(‘D1‘)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);

//Add comment 添加注释
$objPHPExcel->getActiveSheet()->getComment(‘E11‘)->setAuthor(‘PHPExcel‘);
$objCommentRichText = $objPHPExcel->getActiveSheet()->getComment(‘E11‘)->getText()->createTextRun(‘PHPExcel:‘);
$objCommentRichText->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getComment(‘E11‘)->getText()->createTextRun("\r\n");
$objPHPExcel->getActiveSheet()->getComment(‘E11‘)->getText()->createTextRun(‘Total amount on the current invoice, excluding VAT.‘);

//Add rich-text string 添加文字 可设置样式
$objRichText = new PHPExcel_RichText( $objPHPExcel->getActiveSheet()->getCell(‘A18‘) );
$objRichText->createText(‘This invoice is ‘);
$objPayable = $objRichText->createTextRun(‘payable within thirty days after the end of the month‘);
$objPayable->getFont()->setBold(true);
$objPayable->getFont()->setItalic(true);
$objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
$objRichText->createText(‘, unless specified otherwise on the invoice.‘);

//Merge cells 合并分离单元格
$objPHPExcel->getActiveSheet()->mergeCells(‘A18:E22‘);
$objPHPExcel->getActiveSheet()->unmergeCells(‘A18:E22‘);

//Protect cells 保护单元格
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);//Needs to be set to true in order to enable any worksheet protection!
$objPHPExcel->getActiveSheet()->protectCells(‘A3:E13‘, ‘PHPExcel‘);

//Set cell number formats 数字格式化
$objPHPExcel->getActiveSheet()->getStyle(‘E4‘)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
$objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle(‘E4‘), ‘E5:E13‘ );

//Set column widths 设置列宽度
$objPHPExcel->getActiveSheet()->getColumnDimension(‘B‘)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D‘)->setWidth(12);

//Set fonts 设置字体
$objPHPExcel->getActiveSheet()->getStyle(‘B1‘)->getFont()->setName(‘Candara‘);
$objPHPExcel->getActiveSheet()->getStyle(‘B1‘)->getFont()->setSize(20);
$objPHPExcel->getActiveSheet()->getStyle(‘B1‘)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle(‘B1‘)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
$objPHPExcel->getActiveSheet()->getStyle(‘B1‘)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);

//Set alignments 设置对齐
$objPHPExcel->getActiveSheet()->getStyle(‘D11‘)->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(‘A3‘)->getAlignment()->setWrapText(true);

//Set column borders 设置列边框
$objPHPExcel->getActiveSheet()->getStyle(‘A4‘)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle(‘A10‘)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle(‘E10‘)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle(‘D13‘)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
$objPHPExcel->getActiveSheet()->getStyle(‘E13‘)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);

//Set border colors 设置边框颜色
$objPHPExcel->getActiveSheet()->getStyle(‘D13‘)->getBorders()->getLeft()->getColor()->setARGB(‘FF993300‘);
$objPHPExcel->getActiveSheet()->getStyle(‘D13‘)->getBorders()->getTop()->getColor()->setARGB(‘FF993300‘);
$objPHPExcel->getActiveSheet()->getStyle(‘D13‘)->getBorders()->getBottom()->getColor()->setARGB(‘FF993300‘);
$objPHPExcel->getActiveSheet()->getStyle(‘E13‘)->getBorders()->getRight()->getColor()->setARGB(‘FF993300‘);

//Set fills 设置填充
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFill()->getStartColor()->setARGB(‘FF808080‘);

//Add a hyperlink to the sheet 添加链接
$objPHPExcel->getActiveSheet()->setCellValue(‘E26‘, ‘www.phpexcel.net‘);
$objPHPExcel->getActiveSheet()->getCell(‘E26‘)->getHyperlink()->setUrl(‘http://www.phpexcel.net‘);
$objPHPExcel->getActiveSheet()->getCell(‘E26‘)->getHyperlink()->setTooltip(‘Navigate to website‘);
$objPHPExcel->getActiveSheet()->getStyle(‘E26‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

//Add a drawing to the worksheet 添加图片
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName(‘Logo‘);
$objDrawing->setDescription(‘Logo‘);
$objDrawing->setPath(‘./images/officelogo.jpg‘);
$objDrawing->setHeight(36);
$objDrawing->setCoordinates(‘B15‘);
$objDrawing->setOffsetX(110);
$objDrawing->setRotation(25);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(45);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

//Play around with inserting and removing rows and columns
$objPHPExcel->getActiveSheet()->insertNewRowBefore(6, 10);
$objPHPExcel->getActiveSheet()->removeRow(6, 10);
$objPHPExcel->getActiveSheet()->insertNewColumnBefore(‘E‘, 5);
$objPHPExcel->getActiveSheet()->removeColumn(‘E‘, 5);

//Add conditional formatting
$objConditional1 = new PHPExcel_Style_Conditional();
$objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
$objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN);
$objConditional1->setCondition(‘0‘);
$objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$objConditional1->getStyle()->getFont()->setBold(true);

//Set autofilter 自动过滤
$objPHPExcel->getActiveSheet()->setAutoFilter(‘A1:C9‘);

//Hide "Phone" and "fax" column 隐藏列
$objPHPExcel->getActiveSheet()->getColumnDimension(‘C‘)->setVisible(false);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D‘)->setVisible(false);

//Set document security 设置文档安全
$objPHPExcel->getSecurity()->setLockWindows(true);
$objPHPExcel->getSecurity()->setLockStructure(true);
$objPHPExcel->getSecurity()->setWorkbookPassword("PHPExcel");

//Set sheet security 设置工作表安全
$objPHPExcel->getActiveSheet()->getProtection()->setPassword(‘PHPExcel‘);
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);// This should be enabled in order to enable any of the following!
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);

//Calculated data 计算
echo ‘Value of B14 [=COUNT(B2:B12)]: ‘ . $objPHPExcel->getActiveSheet()->getCell(‘B14‘)->getCalculatedValue() . "\r\n";

//Set outline levels
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E‘)->setOutlineLevel(1);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E‘)->setVisible(false);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E‘)->setCollapsed(true);

//Freeze panes
$objPHPExcel->getActiveSheet()->freezePane(‘A2‘);

//Rows to repeat at top
$objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);

//Set data validation 验证输入值
$objValidation = $objPHPExcel->getActiveSheet()->getCell(‘B3‘)->getDataValidation();
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE );
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setErrorTitle(‘Input error‘);
$objValidation->setError(‘Number is not allowed!‘);
$objValidation->setPromptTitle(‘Allowed input‘);
$objValidation->setPrompt(‘Only numbers between 10 and 20 are allowed.‘);
$objValidation->setFormula1(10);
$objValidation->setFormula2(20);
$objPHPExcel->getActiveSheet()->getCell(‘B3‘)->setDataValidation($objValidation);

//Create a new worksheet, after the default sheet 创建新的工作标签
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);

//Set header and footer. When no different headers for odd/even are used, odd header is assumed. 页眉页脚
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(‘&C&HPlease treat this document as confidential!‘);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter(‘&L&B‘ . $objPHPExcel->getProperties()->getTitle() . ‘&RPage &P of &N‘);

//Set page orientation and size 方向大小
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);

//Rename sheet 重命名工作表标签
$objPHPExcel->getActiveSheet()->setTitle(‘Simple‘);

//Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

//Save Excel 2007 file 保存
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace(‘.php‘, ‘.xlsx‘, __FILE__));

//Save Excel 5 file 保存
require_once(‘Classes/PHPExcel/Writer/Excel5.php‘);
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save(str_replace(‘.php‘, ‘.xls‘, __FILE__));

//1.6.2新版保存
require_once(‘Classes/PHPExcel/IOFactory.php‘);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007‘);
$objWriter->save(str_replace(‘.php‘, ‘.xls‘, __FILE__));
时间: 2024-10-08 15:46:32

对phpexcel的若干补充的相关文章

.htaccess rewrite 规则详细说明

rewrite的语法格式: RewriteEngine On #要想rewrite起作用,必须要写上哦 RewriteBase url-path #设定基准目录,例如希望对根目录下的文件rewrtie,就是”/” RewriteCond test-string condPattern #写在RewriteRule之前,可以有一或N条,用于测试rewrite的匹配条件,具体怎么写,后面会详细说到. RewriteRule Pattern Substitution #规则 RewriteEngine

apache重写字段详细说明

用Apache虚拟主机的朋友很多,apache提供的.htaccess模块可以为每个虚拟主机设定rewrite规则,这对网站SEO优化相当有用,同时也改善了用户体验.国内的虚拟机一般不提供.htaccess功能(据我所知,discuz的主机好像提供此功能),而在国外主机中,.htaccess功能似乎是标配,笔者的Blog架在MT上,支持.htaccess,每次看到一堆别人写好了的.htaccess设置,很多命令都不甚了了,查看.修改起来很不方便,痛定思痛,潜心学习一下,知其所以然嘛- 学习前提:

.htaccess 详解

.htaccess文件(或者"分布式配置文件")提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作为用户,所能使用的命令受到限制.管理员可以通过Apache的AllowOverride指令来设置. 概述来说,htaccess文件是Apache服务器中的一个配置文件,它负责相关目录下的网页配置.通过htaccess文件,可以帮我们实现:网页301重定向.自定义404错误页面.改变文件扩展名.允许/阻止特定的用户或

1.RapidIO协议概述

转自https://www.cnblogs.com/liujinggang/p/9925859.html 一.RapidIO背景介绍 RapidIO是由Motorola和Mercury等公司率先倡导的一种高性能. 低引脚数. 基于数据包交换的互连体系结构,是为满足和未来高性能嵌入式系统需求而设计的一种开放式互连技术标准.RapidIO主要应用于嵌入式系统内部互连,支持芯片到芯片.板到板间的通讯,可作为嵌入式设备的背板(Backplane)连接. RapidI0采用三层分级体系结构,该分级结的如下

thinkphp上传至服务器后PhpExcel导入导出的若干问题

在将tp5项目上传到服务器后,原来的系统有部分功能无法实现,在批量到如时报错 mkdir(): Device or resource: 查看了一下这是权限的问题,因此打开服务器中的项目,将controller控制器中的导入代码的文件位置进行修改: 这里改为你自己定义的文件夹位置. 点开文件夹属性->安全 设置此文件夹可以让所有用户读取和写入. 设置完成再次点击导入,发现报错: Warning: "continue" targeting switch is equivalent t

C++ Primer Plus的若干收获--(八)

接下我会比较系统的介绍OOP的核心--类这个概念.可能会写的比较繁琐,比较多,但是自己理解总是好的.还记得上学期刚开始的时候老师讲的数据结构,一上来就来个LIst的一个大类,结果全班就傻了,完全不知所云(ps:博主是数学系,只学过C),然后自己就坐在图书馆借了两大本C++书开始啃了起来,大概啃了两个月才把C++学了个大概,尤其是读到类这块都是三四百行的常常的代码,看的那叫一个痛苦啊.不过自此之后就真正喜欢上这门语言了,再到之后能用C++写上个一两千行的代码,才发现之前的努力还是有收获的.好了,废

Ubuntu12.04下载Android4.0.1源码全过程,附若干问题解决[转]

学校里一直在做应用层开发,考虑到日后就业问题,这次决定研究源码和驱动,并进行编译.没想到就下载源码这一步折腾了我整整两天,期间遇到很多问题,哎,记录于此,希望日后再下源码的人不要再走无谓的弯路了.事实上可以在这里http://zhu.im/Android/下载源码,但是google推荐用repo来下载,为了多学东西就学下repo吧,毕竟下现成的也太么含量了.最初我参考的是老罗的博客http://blog.csdn.net/luoshengyang/article/details/6559955

关于logrotate的额外补充

http://chenx1242.blog.51cto.com/10430133/1942514 里面已经简单介绍了logrotate命令,这里还有一些额外补充的东西: 1)查看logrotate对log文件的具体执行情况的语句是#cat /var/lib/logrotate.status,效果如图: 2)使用-v或-d参数时,显示"log does not need rotating",这是因为logrotate在对status未记录的文件进行转储时,会在status添加一条该文件的

phpExcel导出大量数据出现内存溢出错误的解决方法

phpExcel将读取的单元格信息保存在内存中,我们可以通过 代码如下:PHPExcel_Settings::setCacheStorageMethod() 来设置不同的缓存方式,已达到降低内存消耗的目的! 1.将单元格数据序列化后保存在内存中 代码如下: PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized; 2.将单元格序列化后再进行Gzip压缩,然后保存在内存中 代码如下: PHPExcel_CachedObject