首先需要去官網https://github.com/PHPOffice/PHPExcel/下載PHPExcel,下載后只需要Classes目錄下的文件即可。
鏈接: https://pan.baidu.com/s/1L3huK-3esd6pvM8tUe0zlg 密碼: tn57
hui前端圖標(http://www.h-ui.net/Hui-overview.shtml)
1、PHPExcel導出方法實現過程
/** * 數據導出 * @param array $title 標題行名稱 * @param array $data 導出數據 * @param string $fileName 文件名 * @param string $savePath 保存路徑 * @param $type 是否下載 false--保存 true--下載 * @return string 返回文件全路徑 * @throws PHPExcel_Exception * @throws PHPExcel_Reader_Exception */ function exportExcel($title=array(), $data=array(), $fileName='', $savePath='./', $isDown=false){
error_reporting(0); include('PHPExcel.php'); $obj = 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'); $obj->getActiveSheet(0)->setTitle('sheet名稱'); //設置sheet名稱 $_row = 1; //設置縱向單元格標識 if($title){ $_cnt = count($title); $obj->getActiveSheet(0)->mergeCells('A'.$_row.':'.$cellName[$_cnt-1].$_row); //合並單元格 $obj->setActiveSheetIndex(0)->setCellValue('A'.$_row, '數據導出:'.date('Y-m-d H:i:s')); //設置合並后的單元格內容 $_row++; $i = 0; foreach($title AS $v){ //設置列標題 $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v); $i++; } $_row++; } //填寫數據 if($data){ $i = 0; foreach($data AS $_v){ $j = 0; foreach($_v AS $_cell){ $obj->getActiveSheet(0)->setCellValue($cellName[$j] . ($i+$_row), $_cell); $j++; } $i++; } } //文件名處理 if(!$fileName){ $fileName = uniqid(time(),true); } $objWrite = PHPExcel_IOFactory::createWriter($obj, 'Excel2007'); if($isDown){ //網頁下載 header('pragma:public'); header("Content-Disposition:attachment;filename=$fileName.xls"); $objWrite->save('php://output');exit; } $_fileName = iconv("utf-8", "gb2312", $fileName); //轉碼 $_savePath = $savePath.$_fileName.'.xlsx'; $objWrite->save($_savePath); return $savePath.$fileName.'.xlsx'; } //exportExcel(array('姓名','年齡'), array(array('a',21),array('b',23)), '檔案', './', true);
2、PHPExcel導入方法實現過程
/** * 數據導入 * @param string $file excel文件 * @param string $sheet * @return string 返回解析數據 * @throws PHPExcel_Exception * @throws PHPExcel_Reader_Exception */ function importExecl($file='', $sheet=0){ $file = iconv("utf-8", "gb2312", $file); //轉碼 if(empty($file) OR !file_exists($file)) { die('file not exists!'); } include('PHPExcel.php'); //引入PHP EXCEL類 $objRead = new PHPExcel_Reader_Excel2007(); //建立reader對象 if(!$objRead->canRead($file)){ $objRead = new PHPExcel_Reader_Excel5(); if(!$objRead->canRead($file)){ die('No Excel!'); } } $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'); $obj = $objRead->load($file); //建立excel對象 $currSheet = $obj->getSheet($sheet); //獲取指定的sheet表 $columnH = $currSheet->getHighestColumn(); //取得最大的列號 $columnCnt = array_search($columnH, $cellName); $rowCnt = $currSheet->getHighestRow(); //獲取總行數 $data = array(); for($_row=1; $_row<=$rowCnt; $_row++){ //讀取內容 for($_column=0; $_column<=$columnCnt; $_column++){ $cellId = $cellName[$_column].$_row; $cellValue = $currSheet->getCell($cellId)->getValue(); //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue(); #獲取公式計算的值 if($cellValue instanceof PHPExcel_RichText){ //富文本轉換字符串 $cellValue = $cellValue->__toString(); } $data[$_row][$cellName[$_column]] = $cellValue; } } return $data; }
在導出excel表時報錯,表導出后打不開
關於phpExcel導出excel之后打開不了的問題
情況說明:
我相信很多人用過phpExcel這個類庫都會遇到的一個問題就是,導出excel表格文件后(xls或xlsx文件),打開這個文件既然顯示
![]()
然后網上也很多解決辦法,主要看大家的情況而選擇用哪用方法。
- 網上主要的問題以及解決的方法:
文件帶有UTF-8 BOM:這種情況只要把簽名給刪掉就可以了,將文件改成不帶BOM簽名。
- 我就說一下我這次的問題所在,希望大家遇到這種情況時候也可以幫到各位
我這次的情況是這樣:我文件沒有帶UTF-8 BOM,而且在本地測試的時候也沒有問題,但是放到服務器上就出現導出EXCEL之后打不開的問題。
-
然后我就用編譯器打開了一個不能開打的excel文件:
<br />
<b>Warning</b>: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in <b>C:\wtf\enroll_beta\lib\phpExcel\Classes\PHPExcel\Writer\Excel2007\DocProps.php</b> on line <b>172</b><br />
<br />
<b>Warning</b>: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in <b>C:\wtf\enroll_beta\lib\phpExcel\Classes\PHPExcel\Writer\Excel2007\DocProps.php</b> on line <b>178</b><br />
PK -
然而真正能打開的excel文件里面的代碼是這樣的額:
504b 0304 1400 0000 0800 3cab 0249 4792 44b2 5801 0000 f004 0000 1300 0000 5b43 6f6e 7465 6e74 5f54 7970 6573 5d2e 786d 6cad 944d 4ec3 3010 85f7 9c22 f216 256e 5920 849a 7641 6109 9528 0730 f6a4 b1ea d896 67fa 777b 2669 0b08 8940 d56e 6245 f67b dff8 79ec d164 dbb8 6c0d 096d f0a5 1816 0391 81d7 c158 bf28 c5db fc29 bf13 1992 f246 b9e0 a114 3b40 3119 5f8d e6bb 0898 b1d8 6329 6aa2 782f 25ea 1a1a 8545 88e0 79a6 0aa9 51c4 bf69 21a3 d24b b500 7933 18dc 4a1d 3c81 a79c 5a0f 311e bd30 3f59 03d9 4c25 7a56 0d63 e4d6 4962 37d8 7f87 05fb 89ec 612f 6cd9 a550 313a ab15 71e1 72ed cd0f 6a1e aaca 6a30 41af 1a96 149d cd75 eb22 7f05 22ed 1ce0 d928 8c09 (太多就不全部打印出來了)
-
我的猜想是因為服務器時間區域跟phpExcel函數庫臉面的時間區域選用不一致,然后報了個warming。雖然warming不影響函數的執行,但是php導出excel文件,是header出來的。這個warning一出來,就把warning的信息都導入到excel文件里面了,自然WPS或者office軟件就識別不了里面的代碼,自然就無法打開不了文件。
-
我的解決辦法:
既然是因為warning的影響,那我就把它屏蔽掉吧,然后我就函數第一行加了這行代碼:
error_reporting(0);
這行代碼的作用就是把warning,error等等的信息不顯示出來。
最后執行,導出來的excel文件就可以順利打開了。