PHPExcel導出excel文件,先說下重要的參數要記住的東西php
impUser() 導入方法app
exportExcel(xlsName,xlsName,xlsCell,$xlsData);導出方法框架
PHPExcel.php PHP主文件, 放置各類class結合核心文件函數
<Fatal error:Class 'ZipArchive' not found in..... 這報錯的意思,是沒有正常開啓php.in配置裏面的php_zip.dll.網站
其實能實現PHP_EXCEL的方法有不少, 我簡單的介紹一種簡單粗暴我用過的調用插件方法.spa
1、導入插件
下面介紹的是使用TP3.2.3框架+PHP_EXCEL 來導出數據3d
1:在php.ini配置文件中開啓擴展, php_zip.dllexcel
2:PHP_EXCEL使用的是一個外部插件,下載網站http://phpexcel.codeplex.com/code
下載解壓以後打開能看到這樣
打開CLASSES文件夾,裏面的文件有
把裏面的文件拉到 框架裏面中, ThinkPHP/Library/Vendor/
3: 由於涉及到調用此類的方法,因此咱們要在框架的 公共控制器函數中好比個人這個是全部控制器都繼承的這個主方法
咱們就打開這個主方法裏面
把這段代碼方在裏面, 方便咱們調用
/** +---------------------------------------------------------- * Export Excel | 2013.08.23 * Author:HongPing <hongping626@qq.com> +---------------------------------------------------------- * @param $expTitle string File name +---------------------------------------------------------- * @param $expCellName array Column name +---------------------------------------------------------- * @param $expTableData array Table data +---------------------------------------------------------- */ public function exportExcel($expTitle,$expCellName,$expTableData){ $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名稱 $fileName = $_SESSION['loginAccount'].date('_YmdHis');//or $xlsTitle 文件名稱可根據本身狀況設定 $cellNum = count($expCellName); $dataNum = count($expTableData); vendor("PHPExcel.PHPExcel"); $objPHPExcel = new PHPExcel(); $cellName = 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(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合併單元格 $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s')); for($i=0;$i<$cellNum;$i++){ $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]); } // Miscellaneous glyphs, UTF-8 for($i=0;$i<$dataNum;$i++){ for($j=0;$j<$cellNum;$j++){ $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]); } } 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; } /** +---------------------------------------------------------- * Import Excel | 2013.08.23 * Author:HongPing <hongping626@qq.com> +---------------------------------------------------------- * @param $file upload file $_FILES +---------------------------------------------------------- * @return array array("error","message") +---------------------------------------------------------- */ public function importExecl($file){ if(!file_exists($file)){ return array("error"=>0,'message'=>'file not found!'); } Vendor("PHPExcel.PHPExcel.IOFactory"); $objReader = PHPExcel_IOFactory::createReader('Excel5'); try{ $PHPReader = $objReader->load($file); }catch(Exception $e){} if(!isset($PHPReader)) return array("error"=>0,'message'=>'read error!'); $allWorksheets = $PHPReader->getAllSheets(); $i = 0; foreach($allWorksheets as $objWorksheet){ $sheetname=$objWorksheet->getTitle(); $allRow = $objWorksheet->getHighestRow();//how many rows $highestColumn = $objWorksheet->getHighestColumn();//how many columns $allColumn = PHPExcel_Cell::columnIndexFromString($highestColumn); $array[$i]["Title"] = $sheetname; $array[$i]["Cols"] = $allColumn; $array[$i]["Rows"] = $allRow; $arr = array(); $isMergeCell = array(); foreach ($objWorksheet->getMergeCells() as $cells) {//merge cells foreach (PHPExcel_Cell::extractAllCellReferencesInRange($cells) as $cellReference) { $isMergeCell[$cellReference] = true; } } for($currentRow = 1 ;$currentRow<=$allRow;$currentRow++){ $row = array(); for($currentColumn=0;$currentColumn<$allColumn;$currentColumn++){; $cell =$objWorksheet->getCellByColumnAndRow($currentColumn, $currentRow); $afCol = PHPExcel_Cell::stringFromColumnIndex($currentColumn+1); $bfCol = PHPExcel_Cell::stringFromColumnIndex($currentColumn-1); $col = PHPExcel_Cell::stringFromColumnIndex($currentColumn); $address = $col.$currentRow; $value = $objWorksheet->getCell($address)->getValue(); if(substr($value,0,1)=='='){ return array("error"=>0,'message'=>'can not use the formula!'); exit; } if($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC){ $cellstyleformat=$cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat(); $formatcode=$cellstyleformat->getFormatCode(); if (preg_match('/^([$[A-Z]*-[0-9A-F]*])*[hmsdy]/i', $formatcode)) { $value=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value)); }else{ $value=PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode); } } if($isMergeCell[$col.$currentRow]&&$isMergeCell[$afCol.$currentRow]&&!empty($value)){ $temp = $value; }elseif($isMergeCell[$col.$currentRow]&&$isMergeCell[$col.($currentRow-1)]&&empty($value)){ $value=$arr[$currentRow-1][$currentColumn]; }elseif($isMergeCell[$col.$currentRow]&&$isMergeCell[$bfCol.$currentRow]&&empty($value)){ $value=$temp; } $row[$currentColumn] = $value; } $arr[$currentRow] = $row; } $array[$i]["Content"] = $arr; $i++; } spl_autoload_register(array('Think','autoload'));//must, resolve ThinkPHP and PHPExcel conflicts unset($objWorksheet); unset($PHPReader); unset($PHPExcel); unlink($file); return array("error"=>1,"data"=>$array); }
2、使用
把代碼放進父類控制器
須要注意的地方:路徑與加個\才能調用
OK.放進去以後,不是就能夠用了,咱們還要經過咱們的實際路徑更改一下.
4:如今咱們能夠開始來調用了
在咱們使用的控制器裏面,調用此方法
調用方法:
使用:例子:
看到Array的數據跟這個順序是正比的,作一個數據導出的操做
效果: