PHP操作Excel – PHPExcel 基本用法详解-来源diandian_520

以下文章来源:diandian_520

http://blog.csdn.net/diandian_520/article/details/7827038

1.header

header("Content-Type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=sample.xls");
header("Pragma:no-cache");
header("Expires:0"); 

2.PHPExcel

开发包Tests目录有详细使用实例  支持中文,注意文件编码   文件保存为utf-8

写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__));

  

读excel

//Include class
require_once(‘Classes/PHPExcel/Reader/Excel2007.php‘);
$objReader = new PHPExcel_Reader_Excel2007;
$objPHPExcel = $objReader->load("05featuredemo.xlsx");

  

读写csv

require_once("05featuredemo.inc.php");
require_once(‘Classes/PHPExcel/Writer/CSV.php‘);
require_once(‘Classes/PHPExcel/Reader/CSV.php‘);
require_once(‘Classes/PHPExcel/Writer/Excel2007.php‘);
//Write to CSV format 写
$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
$objWriter->setDelimiter(‘;‘);
$objWriter->setEnclosure(‘‘);
$objWriter->setLineEnding("\r\n");
$objWriter->setSheetIndex(0);
$objWriter->save(str_replace(‘.php‘, ‘.csv‘, __FILE__));
//Read from CSV format 读
$objReader = new PHPExcel_Reader_CSV();
$objReader->setDelimiter(‘;‘);
$objReader->setEnclosure(‘‘);
$objReader->setLineEnding("\r\n");
$objReader->setSheetIndex(0);
$objPHPExcelFromCSV = $objReader->load(str_replace(‘.php‘, ‘.csv‘, __FILE__));
//Write to Excel2007 format
$objWriter2007 = new PHPExcel_Writer_Excel2007($objPHPExcelFromCSV);
$objWriter2007->save(str_replace(‘.php‘, ‘.xlsx‘, __FILE__));

  

写html

require_once("05featuredemo.inc.php");
require_once(‘Classes/PHPExcel/Writer/HTML.php‘);
//Write to HTML format
$objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
$objWriter->setSheetIndex(0);
$objWriter->save(str_replace(‘.php‘, ‘.htm‘, __FILE__));

  

写pdf

require_once("05featuredemo.inc.php");
require_once(‘Classes/PHPExcel/IOFactory.php‘);
//Write to PDF format
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘PDF‘);
$objWriter->setSheetIndex(0);
$objWriter->save(str_replace(‘.php‘, ‘.pdf‘, __FILE__));
//Echo memory peak usage
echo date(‘H:i:s‘) . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";

  

时间: 2024-10-10 13:02:49

PHP操作Excel – PHPExcel 基本用法详解-来源diandian_520的相关文章

PHP操作Excel – PHPExcel 基本用法详解

导出excel属性设置//Include classrequire_once('Classes/PHPExcel.php');require_once('Classes/PHPExcel/Writer/Excel2007.php');$objPHPExcel = new PHPExcel(); //Set properties 设置文件属性$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");$objPH

Java操作Redis之Jedis用法详解

Redis(Remote Dictionary Server,远程数据字典服务器)是一个开源的高性能内存数据库,常用作缓存缓存服务器使用,也已做消息队列使用.因其高性能.丰富的数据类型.可扩展等特性受开发者青睐,这里介绍在java中使用Jedis操作Redis的基本用法. 1. 字符串String. package com.zws.redis.examples; import java.util.concurrent.TimeUnit; import redis.clients.jedis.Je

PHP操作Excel – PHPExcel 基本用法

1.header [php] header("Content-Type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=sample.xls"); header("Pragma:no-cache"); header("Expires:0"); 2.PHPExcel http://www.codeplex.com/PHPExc

PHP之——PHPExcel 基本用法详解(插曲)

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46044301 1.header header("Content-Type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=sample.xls"); header("Pragma:no-cache"); header("

WordPress 数据库操作WPDB对象($wpdb)用法详解【转载】

使用wordpress的时候,如果想直接使用WP里封装的数据库操作的类(wp-db.php),将wp-blog-header.php包含到代码中就可以使用了. define(‘PATH’, dirname(dirname(__FILE__)).‘/’); require_once(PATH . ‘../wp-blog-header.php’); global $wpdb; 插入数据时,其中一种方法是使用wp-db类中的insert()函数. $table = "test_table";

分布式任务队列celery用法详解

celery基础介绍:这个图我们可以看出,celery基本结构也就是三部分1 第一部分 broker也就是中间件消息队列,作用就是用来接收应用的请求这一部分常见玩法可以是rabbitmq和redis等2 第二部分 worker 也就是工作队列 也就是celery本身的任务队列服务,一般情况下大型的生产应用我们会结合supervisor来管理这么多的worker3 第三部分 result 存储,就是把执行的结果,状态等信息进行存储,常规用法我们可以用rabbitmq redis,mysql,mon

jQuery 事件用法详解

jQuery 事件用法详解 目录 简介 实现原理 事件操作 绑定事件 解除事件 触发事件 事件委托 事件操作进阶 阻止默认事件 阻止事件传播 阻止事件向后执行 命名空间 自定义事件 事件队列 jquery中文文档 简介 jquery 之所以成为最受欢迎的前端库,很大一部分是得益于它的事件具有良好的语义,优秀的兼容性,并且便于管理和扩展. 在这里我会介绍 jquery 事件的一些比较基础的用法. 实现原理 jquery 事件脱胎于浏览器的 addEventListener (W3) 和 attac

Nmap用法详解

nmap是一个网络探测和安全扫描程序,系统管理者和个人可以使用这个软件扫描大型的网络,获取那台主机正在运行以及提供什么服务等信息.nmap支持很多扫描技术,例如:UDP.TCP connect().TCP SYN(半开扫描).ftp代理(bounce攻击).反向标志.ICMP.FIN.ACK扫描.圣诞树(Xmas Tree).SYN扫描和null扫描.从扫描类型一节可以得到细节.nmap还提供了一些高级的特征,例如:通过TCP/IP协议栈特征探测操作系统类型,秘密扫描,动态延时和重传计算,并行扫

C# ListView用法详解

一.ListView类 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLines:设置行和列之间是否显示网格线.(默认为false)提示:只有在Details视图该属性才有意义. (3)AllowColumnReorder:设置是否可拖动列标头来对改变列的顺序.(默认为false)提示:只有在Details视图该属性才有意义. (4)View:获取或设置项在控件中的显示方式,包括D