PHPExcel但是個好東東,功能強大,下面這是一個phpExcel簡易中文幫助手冊,列舉了各類屬性,以及經常使用的操做方法,是每個都用實例加以說明,但願對你們有所幫助。php
引用PHPExcel瀏覽器
include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/Writer/Excel5.php'; 用於輸出.xls的
$objPHPExcel = new PHPExcel(); //建立一個實例
設置excel的屬性app
//建立人 $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");
也可用下面這種方式url
$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");
設置當前的sheetspa
$objPHPExcel->setActiveSheetIndex(0);
設置sheet的標題excel
$objPHPExcel->getActiveSheet()->setTitle('Simple');
設置單元格寬度code
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
設置單元格高度orm
$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(40);
合併單元格xml
$objPHPExcel->getActiveSheet()->mergeCells('A18:E22');
拆分單元格blog
$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');