PHPExcel 中文使用手冊詳解
阿新 • • 發佈:2018-12-12
實現程式碼
/** * * execl資料匯出 * 應用場景:訂單匯出 * @param string $title 模型名(如Member),用於匯出生成檔名的字首 * @param array $cellName 表頭及欄位名 * @param array $data 匯出的表資料 * * 特殊處理:合併單元格需要先對資料進行處理 */ function exportOrderExcel($title,$cellName,$data) { //引入核心檔案 vendor("PHPExcel.PHPExcel"); $objPHPExcel = new \PHPExcel(); //定義配置 $topNumber = 2;//表頭有幾行佔用 $xlsTitle = iconv('utf-8', 'gb2312', $title);//檔名稱 $fileName = $title.date('_YmdHis');//檔名稱 $cellKey = array( 'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM', 'AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ' ); //寫在處理的前面(瞭解表格基本知識,已測試) // $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);//所有單元格(行)預設高度 // $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);//所有單元格(列)預設寬度 // $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);//設定行高度 // $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);//設定列寬度 // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);//設定文字大小 // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//設定是否加粗 // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);// 設定文字顏色 // $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//設定文字居左(HORIZONTAL_LEFT,預設值)中(HORIZONTAL_CENTER)右(HORIZONTAL_RIGHT) // $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中 // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);//設定填充顏色 // $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF7F24');//設定填充顏色 //處理表頭標題 $objPHPExcel->getActiveSheet()->mergeCells('A1:'.$cellKey[count($cellName)-1].'1');//合併單元格(如果要拆分單元格是需要先合併再拆分的,否則程式會報錯) $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1','訂單資訊'); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //處理表頭 foreach ($cellName as $k=>$v) { $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellKey[$k].$topNumber, $v[1]);//設定表頭資料 $objPHPExcel->getActiveSheet()->freezePane($cellKey[$k].($topNumber+1));//凍結視窗 $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getFont()->setBold(true);//設定是否加粗 $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k].$topNumber)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中 if($v[3] > 0)//大於0表示需要設定寬度 { $objPHPExcel->getActiveSheet()->getColumnDimension($cellKey[$k])->setWidth($v[3]);//設定列寬度 } } //處理資料 foreach ($data as $k=>$v) { foreach ($cellName as $k1=>$v1) { $objPHPExcel->getActiveSheet()->setCellValue($cellKey[$k1].($k+1+$topNumber), $v[$v1[0]]); if($v['end'] > 0) { if($v1[2] == 1)//這裡表示合併單元格 { $objPHPExcel->getActiveSheet()->mergeCells($cellKey[$k1].$v['start'].':'.$cellKey[$k1].$v['end']); $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].$v['start'])->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); } } if($v1[4] != "" && in_array($v1[4], array("LEFT","CENTER","RIGHT"))) { $v1[4] = eval('return PHPExcel_Style_Alignment::HORIZONTAL_'.$v1[4].';'); //這裡也可以直接傳常量定義的值,即left,center,right;小寫的strtolower $objPHPExcel->getActiveSheet()->getStyle($cellKey[$k1].($k+1+$topNumber))->getAlignment()->setHorizontal($v1[4]); } } } //匯出execl header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"'); header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新視窗列印inline本視窗列印 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; }
實現效果:
以下從網上找的,很有用,都經過本人親測的!拿下來做為自己收藏 PHPExcel是國外人開發的一個php處理excel外掛了,這個外掛沒有中文的說明了,只有英文說明了,今天我們就一起來看看關於PHPExcel 中文使用手冊的例子,具體的如下所示。 HPExcel 是用來操作Office Excel 文件的一個PHP類庫,它基於微軟的Openxml標準和PHP語言。可以使用它來讀取、寫入不同格式的電子表格。整體來說是一個不錯的Excel操作類。 下面是這個類的一些操作說明。
include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/Writer/Excel5.php'; 用於輸出.xls的 建立一個excel $objPHPExcel = new PHPExcel(); // 輸出Excel表格到瀏覽器下載 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="abc.xls"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $objWriter->save('php://output'); ——————————————————————————————————————– 設定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'); 凍結視窗 $objPHPExcel->getActiveSheet()->freezePane('A2'); 保護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 formats echo 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); // 設定單元格高度 // 所有單元格預設高度 $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15); // 第一行的預設高度 $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30); 設定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->setPath($img_val); /*設定圖片高度*/ $objDrawing->setWidth(200); $img_height[] = $objDrawing->getHeight(); /*設定圖片要插入的單元格*/ $objDrawing->setCoordinates($img_k[$j].$i); /*設定圖片所在單元格的格式*/ $objDrawing->setOffsetX(10); $objDrawing->setOffsetY(10); $objDrawing->setRotation(0); $objDrawing->getShadow()->setVisible(true); $objDrawing->getShadow()->setDirection(50); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); 匯出Excel表格例子 <?php $datas = array( array('王城', '男', '18', '1997-03-13', '18948348924'), array('李飛虹', '男', '21', '1994-06-13', '159481838924'), array('王芸', '女', '18', '1997-03-13', '18648313924'), array('郭瑞', '男', '17', '1998-04-13', '15543248924'), array('李曉霞', '女', '19', '1996-06-13', '18748348924'), ); include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/Writer/Excel5.php'; 用於輸出.xls的 // 建立一個excel $objPHPExcel = new PHPExcel(); // Set document properties $objPHPExcel->getProperties()->setCreator("Phpmarker")->setLastModifiedBy("Phpmarker")->settitle("Phpmarker")->setSubject("Phpmarker")->setDescription("Phpmarker")->setKeywords("Phpmarker")->setCategory("Phpmarker"); $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '名字')->setCellValue('B1', '性別')->setCellValue('C1', '年齡')->setCellValue('D1', '出生日期')->setCellValue('E1', '電話號碼'); // Rename worksheet $objPHPExcel->getActiveSheet()->settitle('Phpmarker-' . date('Y-m-d')); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15); $objPHPExcel->getActiveSheet()->freezePane('A2'); $i = 2; foreach($datas as $data){ $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $data[0])->getstyle('A'.$i)->getAlignment()->setHorizontal(PHPExcel_style_Alignment::VERTICAL_CENTER); $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $data[1]); $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $data[2]); $objPHPExcel->getActiveSheet()->setCellValueExplicit('D'. $i, $data[3],PHPExcel_Cell_DataType::TYPE_STRING); $objPHPExcel->getActiveSheet()->getstyle('D' . $i)->getNumberFormat()->setFormatCode("@"); // 設定文字格式 $objPHPExcel->getActiveSheet()->setCellValueExplicit('E'. $i, $data[4],PHPExcel_Cell_DataType::TYPE_STRING); $objPHPExcel->getActiveSheet()->getstyle('E' . $i)->getAlignment()->setWrapText(true); $i ; } $objActSheet = $objPHPExcel->getActiveSheet(); // 設定CELL填充顏色 $cell_fill = array( 'A1', 'B1', 'C1', 'D1', 'E1', ); foreach($cell_fill as $cell_fill_val){ $cellstyle = $objActSheet->getstyle($cell_fill_val); // background // $cellstyle->getFill()->setFillType(PHPExcel_style_Fill::FILL_SOLID)->getStartColor()->setARGB('fafa00'); // set align $cellstyle->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_LEFT)->setVertical(PHPExcel_style_Alignment::VERTICAL_CENTER); // font $cellstyle->getFont()->setSize(12)->setBold(true); // border $cellstyle->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000'); $cellstyle->getBorders()->getBottom()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000'); $cellstyle->getBorders()->getLeft()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000'); $cellstyle->getBorders()->getRight()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB('FFFF0000'); } $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30); $objActSheet->getColumnDimension('A')->setWidth(18.5); $objActSheet->getColumnDimension('B')->setWidth(23.5); $objActSheet->getColumnDimension('C')->setWidth(12); $objActSheet->getColumnDimension('D')->setWidth(12); $objActSheet->getColumnDimension('E')->setWidth(12); $filename = '2015030423'; ob_end_clean();//清除緩衝區,避免亂碼 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$filename.'.xls"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $objWriter->save('php://output'); ?>
到處中文時,會出現亂碼,記得在Header 之前加 ob_end_clean() 清除緩衝區,避免亂碼