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); //輸出來看一看吧
回應 (Leave a comment)