PHPExcel的使用与手册说明
1、下载PHPExcel插件
2、解压后提取classes文件夹到工作目录,并重命名为PHPExcel
3、引入与实例化
require_once("PHPExcel/PHPExcel.php");
$objPHPExcel = new PHPExcel();
print_r($objPHPExcel);
4、导出步骤
新建一个excel表格
实例化PHPExcel类
创建sheet(内制表)
createSheet()方法
setActiveSheetIndex()方法
getActiveSheet方法
填充数据
setCellValue()方法
保存文件
PHPExcel_IOFactory::createWriter()方法
save()方法
5.直接输出到浏览器
function brower_export($type,$filename) { if ($type == "Excel5") { header('Content-Type: application/vnd.ms-excel');//20003 }else{ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//2007 } header('Content-Disposition: attachment;filename="'.$filename.'"');//输出文件的名称 header('Cache-Control: max-age=0');//禁止缓存 } brower_export('Excel5','fjds.xls'); $objWriter->save('php://output');//输出
把save替换成以上
6、设置格式
1、合并单元格:分离 合并单元格 $objPHPExcel->getActiveSheet()->mergeCells('A18:E22'); 分离单元格 $objPHPExcel->getActiveSheet()->unmergeCells('A28:B28'); 设置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'); 保护cell $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 formatsecho date('H:i:s') . " Set cell number formats\n"; $objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' ); 设置宽width // Set column widths$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12); 设置font $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); $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE); $objPHPExcel->getActiveSheet()->getStyle('D13')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('E13')->getFont()->setBold(true); 设置align $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); 设置column的border $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); $objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 设置border的color $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()->getTop()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300'); 设置填充颜色 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF808080'); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->setARGB('FF808080'); 加图片 $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Logo'); $objDrawing->setDescription('Logo'); $objDrawing->setPath('./images/officelogo.jpg'); $objDrawing->setHeight(36); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Paid'); $objDrawing->setDescription('Paid'); $objDrawing->setPath('./images/paid.png'); $objDrawing->setCoordinates('B15'); $objDrawing->setOffsetX(110); $objDrawing->setRotation(25); $objDrawing->getShadow()->setVisible(true); $objDrawing->getShadow()->setDirection(45); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); //处理中文输出问题 需要将字符串转化为UTF-8编码,才能正常输出,否则中文字符将输出为空白,如下处理: $str = iconv('gb2312', 'utf-8', $str); 或者你可以写一个函数专门处理中文字符串: function convertUTF8($str) { if(empty($str)) return ''; return iconv('gb2312', 'utf-8', $str); } /创建人$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"); 复制代码 也可用下面这种方式 $objPHPExcel->getProperties()->setCreator("ctos") ->setLastModifiedBy("ctos") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); 设置当前的sheet $objPHPExcel->setActiveSheetIndex(0); 设置sheet的标题 $objPHPExcel->getActiveSheet()->setTitle('Simple'); 设置单元格宽度 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); 设置单元格高度 $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(40); 合并单元格 $objPHPExcel->getActiveSheet()->mergeCells('A18:E22'); 拆分单元格 $objPHPExcel->getActiveSheet()->unmergeCells('A28:B28'); 设置保护cell,保护工作表 $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); $objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel'); 设置格式 $objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' ); 设置加粗 $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); 设置水平对齐方式(HORIZONTAL_RIGHT,HORIZONTAL_LEFT,HORIZONTAL_CENTER,HORIZONTAL_JUSTIFY) $objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 设置垂直居中 $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); 设置字号 $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10); 设置边框 $objPHPExcel->getActiveSheet()->getStyle('A1:I20')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN); 设置边框颜色 $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()->getTop()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300'); 插入图像 $objDrawing = new PHPExcel_Worksheet_Drawing(); /*设置图片路径 切记:只能是本地图片*/ $objDrawing->setPath('图像地址'); /*设置图片高度*/ $objDrawing->setHeight(180);//照片高度$objDrawing->setWidth(150); //照片宽度 /*设置图片要插入的单元格*/$objDrawing->setCoordinates('E2'); /*设置图片所在单元格的格式*/$objDrawing->setOffsetX(5); $objDrawing->setRotation(5); $objDrawing->getShadow()->setVisible(true); $objDrawing->getShadow()->setDirection(50); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); 设置单元格背景色 $objPHPExcel->getActiveSheet(0)->getStyle('A1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet(0)->getStyle('A1')->getFill()->getStartColor()->setARGB('FFCAE8EA'); 最后输入浏览器,导出Excel $savename='导出Excel示例'; $ua = $_SERVER["HTTP_USER_AGENT"]; $datetime = date('Y-m-d', time()); if (preg_match("/MSIE/", $ua)) { $savename = urlencode($savename); //处理IE导出名称乱码} // excel头参数 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$savename.'.xls"'); //日期为文件名后缀 header('Cache-Control: max-age=0'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //excel5为xls格式,excel2007为xlsx格式 $objWriter->save('php://output'); 比如身份证不现实科学基数 使用setCellValueExplicit(,,PHPExcel_Cell_DataType::TYPE_STRING)//多加一个参数,其他跟setCellValue一样
可以自定义等
7、生成图表
只能使用2007
<?php $dir=dirname(__FILE__);//得到当前脚本所在路径 require $dir."/PHPExcel/PHPExcel.php";//进入文件 $objPHPExcel = new PHPExcel();//实例化 相当于创建一个excel表格 $objSheet = $objPHPExcel -> getActiveSheet();//获得当前活动sheet的操作对象 $objSheet -> setTitle('index');//给当前的sheet设置一个名字 // $objSheet -> setCellValue("A1","姓名") -> setCellValue("B1","分数");//给当前活动sheet填充数据 // $objSheet -> setCellValue("A2","张三") -> setCellValue("B2","85"); $array = array( array('','一班','二班','三班'),//代表第一行 array('不及格','20' , '30','40'), array('良好','20' , '50','55'), array('优秀' , '15','17','20') ); $objSheet->fromArray($array);//直接加载数组 // 生成图表开始 $labels=array( new PHPExcel_Chart_DataSeriesValues('String','Worksheet!$B$1',null,1),//一班 new PHPExcel_Chart_DataSeriesValues('String','Worksheet!$C$1',null,1),//二班 new PHPExcel_Chart_DataSeriesValues('String','Worksheet!$D$1',null,1),//三班 );//先取得绘制图表的标签 $xLabels=array( new PHPExcel_Chart_DataSeriesValues('String','Worksheet!$A$2:$A$4',null,3)//取得图表X轴的刻度 ); $datas=array( new PHPExcel_Chart_DataSeriesValues('Number','Worksheet!$B$2:$B$4',null,3),//取一班的数据 new PHPExcel_Chart_DataSeriesValues('Number','Worksheet!$C$2:$C$4',null,3),//取二班的数据 new PHPExcel_Chart_DataSeriesValues('Number','Worksheet!$D$2:$D$4',null,3)//取三班的数据 );//取得绘图所需的数据 $series=array( new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_LINECHART, PHPExcel_Chart_DataSeries::GROUPING_STANDARD, range(0,count($labels)-1), $labels, $xLabels, $datas ) );//根据取得的东西做出一个图表的框架 $layout=new PHPExcel_Chart_Layout(); $layout->setShowVal(true); $areas=new PHPExcel_Chart_PlotArea($layout,$series); $legend=new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT,$layout,false); $title=new PHPExcel_Chart_Title("高一学生成绩分布"); $ytitle=new PHPExcel_Chart_Title("value(人数)"); $chart=new PHPExcel_Chart( 'line_chart', $title, $legend, $areas, true, false, null, $ytitle );//生成一个图标 $chart->setTopLeftPosition("A7")->setBottomRightPosition("K25");//给定图表所在表格中的位置 $objSheet->addChart($chart);//将chart添加到表格中 // 生成图表结束 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007");//按照指定格式生成excel文件 // $objWriter -> save($dir."/index1.xlsx"); // $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); // function brower_export($filename) { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//2007 header('Content-Disposition: attachment;filename="'.$filename.'"');//输出文件的名称 header('Cache-Control: max-age=0');//禁止缓存 } brower_export('index.xlsx'); $objWriter->save('php://output');//输出 ?>
8、thinkphp中的应用
9、导入excel
<?php header("content-type:text/html:charset=utf-8"); $dir=dirname(_FILE_); require $dir ."/PHPExecl/IOFactory.php";//引入读取excel的类文件 $filename=$dir."/export_1.xls"; $objPHPExcel=PHPExcel_IOFactory::load($filename);//加载文件 /**$sheetCount=$objPHPEcel->getSheetCount();//获取excel文件里有多少个sheet for($i=0;$i<$sheetCount;$i++) { $data=$objPHPExcel->getSheet($i)->toArray();//读取每一个sheet里的数据 全部放入到数组中 }**/ foreach($objPHPExcel->getWordsheetIterator() as $sheet){//循环取sheet foreach(sheet->getRowIterator() as $row){//逐行读取 foreach($row->getCellIterator() as $cell){//逐列读取 $data=$cell->getValue();//获取单元格 } } }
10、 PHP-ExcelReader
参考:
11、开启memcached的缓存