2011
Sep
09
PHPExcel下载
宣告Excel 物件
Example
- require_once('../PHPExcel/PHPExcel.php');require_once('../PHPExcel/PHPExcel/IOFactory.php');$PHPExcel=new PHPExcel();
- //作者
- $PHPExcel->getProperties()->setCreator("Jackie");
- //标题
- $PHPExcel->getProperties()->setTitle("Jackie的Excel试玩");
- //Excel 描述
- $PHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
简单的编辑
Example
- //指定目前要编辑的工作表 ,预设0是指第一个工作表
- $PHPExcel->setActiveSheetIndex(0);
- //在栏位A1 写入文字
- $PHPExcel->getActiveSheet()->setCellValue('A1', '测试');
- //建立下一个工作表$PHPExcel->createSheet();
- //指定开始改第二个工作表
- $PHPExcel->setActiveSheetIndex(1);
- //在栏位 (1,2) 写入文字
- $PHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1,2 '测试');
输出Excel
Example
- $PHPExcelWriter = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel5');
- $PHPExcelWriter->save('filename.xls');
- header("Content-type: application/force-download");
- header("Content-Disposition: attachment; filename=\"filename.xls\"");
- header("Content-Length: ".filesize('filename.xls'));
- @readfile('filename.xls');
天数转日期
Example
- //PHPExcel有自动将天数转成日期的功能
- require_once '../PHPExcel/PHPExcel/Cell/AdvancedValueBinder.php';
- PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );
- $PHPExcel->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
- $PHPExcel->getActiveSheet()->setCellValue('D1', 40813);
- //1900/1/1 经过40813天后 = 2011/09/27
设定色彩,样式
Example
- //设定 style
- //ARGB : alpha透明 +RGB
- $PHPExcel->getActiveSheet()->getStyle('B3')->getFont()->getColor()->setARGB('50ff0000');
- $PHPExcel->getActiveSheet()->setCellValue('B3', 'red');
- $PHPExcel->getActiveSheet()->getStyle('B4')->getFont()->getColor()->setRGB('0000FF');
- $PHPExcel->getActiveSheet()->setCellValue('B4', 'blue');
- //设定背景色
- $PHPExcel->getActiveSheet()->getStyle('B2')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
- $PHPExcel->getActiveSheet()->getStyle('B2')->getFill()->getStartColor()->setARGB('FFFF0000');
- //用Array设定样式
- /*
- font 设定字型与颜色
- fill 填满色彩
- */
- $styleArray = array( 'font' => array( 'bold' => true, 'color'=> array( 'argb' => "ff00f0f0" ) ), 'fill' => array( 'type' => PHPExcel_Style_Fill::FILL_SOLID, 'rotation' => 90, 'startcolor' => array( 'argb' => 'FFc5c5c5', ) ));
- $PHPExcel->getActiveSheet()->getStyle('D')->applyFromArray($styleArray);
- $PHPExcel->getActiveSheet()->setCellValue('D3', 'test');
- //设定预设样式
- $PHPExcel->getDefaultStyle()->getFont()->setName('Arial');
- $PHPExcel->getDefaultStyle()->getFont()->setSize(12);
- $PHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(12);//设定栏位宽度
- $PHPExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(130);
- //合并储存格
- $PHPExcel->getActiveSheet()->mergeCells('A7:E7');
- $PHPExcel->getActiveSheet()->setCellValue('A7','test');
- //贴上图片
- $objDrawing = new PHPExcel_Worksheet_Drawing();
- $objDrawing->setName('Logo');$objDrawing->setDescription('Logo');
- $objDrawing->setPath('test.jpg');$objDrawing->setHeight(86);
- $objDrawing->setWidth(86);$objDrawing->setCoordinates('D5');
- $objDrawing->setWorksheet($PHPExcel->getActiveSheet());
- 禁止修改Excel: $PHPExcel->getActiveSheet()->getProtection()->setSheet(true);
Excel 转 Array
Example
- require('PHPExcel/PHPExcel.php');
- require_once 'PHPExcel/PHPExcel/IOFactory.php';
- $excel= new PHPExcel();
- $PHPReader = PHPExcel_IOFactory::createReader('Excel5'); // Excel5 = 2003 版本
- $exceler = $PHPReader->load($file); //读取excel档案
- $arr = $exceler->getSheet(0)->toArray();
- print_r($arr); //输出来看一看吧