1. 程式人生 > >phpExcel中文幫助手冊

phpExcel中文幫助手冊

下面這是一個phpExcel簡易中文幫助手冊,列舉了各種屬性,以及常用的操作方法,是每一個都用例項加以說明,希望對大家有所幫助。

引用PHPExcel

include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
//或者include 'PHPExcel/Writer/Excel5.php'; 用於輸出.xls的
$objPHPExcel = new PHPExcel(); //建立一個例項

設定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");
複製程式碼

也可用下面這種方式

複製程式碼
$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');
複製程式碼