在以前有寫過一篇文章講述了使用PHP快速生成excel表格文件並下載,這種方式生成Excel文件,生成速度很快,可是有缺點是:
1.單純的生成Excel文件,生成的文件沒有樣式,單元格屬性(填充色,寬度,高度,邊框顏色...)不能自定義;
2.生成的文件雖然能夠打開,可是兼容性不好,每次打開,都會報一個警告:
php
今天使用一個第三方的SDK(PHPExcel)實現Excel文件的導入和導出。git
準備工做:
1.下載PHPExcel的SDK,下載地址:https://github.com/PHPOffice/PHPExcel。github
2.將SDK解壓以後的Class文件拷貝到本身的項目。數組
一.Excel文件讀取瀏覽器
function excelToArray(){ require_once dirname(__FILE__) . '/Lib/Classes/PHPExcel/IOFactory.php'; //加載excel文件 $filename = dirname(__FILE__).'/result.xlsx'; $objPHPExcelReader = PHPExcel_IOFactory::load($filename); $sheet = $objPHPExcelReader->getSheet(0); // 讀取第一個工做表(編號從 0 開始) $highestRow = $sheet->getHighestRow(); // 取得總行數 $highestColumn = $sheet->getHighestColumn(); // 取得總列數 $arr = 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'); // 一次讀取一列 $res_arr = array(); for ($row = 2; $row <= $highestRow; $row++) { $row_arr = array(); for ($column = 0; $arr[$column] != 'F'; $column++) { $val = $sheet->getCellByColumnAndRow($column, $row)->getValue(); $row_arr[] = $val; } $res_arr[] = $row_arr; } return $res_arr; }
或者:app
function excelToArray(){ require_once dirname(__FILE__) . '/Lib/Classes/PHPExcel/IOFactory.php'; //加載excel文件 $filename = dirname(__FILE__).'/result.xlsx'; $objPHPExcelReader = PHPExcel_IOFactory::load($filename); $reader = $objPHPExcelReader->getWorksheetIterator(); //循環讀取sheet foreach($reader as $sheet) { //讀取表內容 $content = $sheet->getRowIterator(); //逐行處理 $res_arr = array(); foreach($content as $key => $items) { $rows = $items->getRowIndex(); //行 $columns = $items->getCellIterator(); //列 $row_arr = array(); //肯定從哪一行開始讀取 if($rows < 2){ continue; } //逐列讀取 foreach($columns as $head => $cell) { //獲取cell中數據 $data = $cell->getValue(); $row_arr[] = $data; } $res_arr[] = $row_arr; } } return $res_arr; }
兩種方法都可將表格數據轉化爲數組,而後哦再進行相關的操做就簡單了。post
二.Excel文件導出ui
/** * 建立(導出)Excel數據表格 * @param array $list 要導出的數組格式的數據 * @param string $filename 導出的Excel表格數據表的文件名 * @param array $indexKey $list數組中與Excel表格表頭$header中每一個項目對應的字段的名字(key值) * @param array $startRow 第一條數據在Excel表格中起始行 * @param [bool] $excel2007 是否生成Excel2007(.xlsx)以上兼容的數據表 * 好比: $indexKey與$list數組對應關係以下: * $indexKey = array('id','username','sex','age'); * $list = array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24)); */ function exportExcel($list,$filename,$indexKey,$startRow=1,$excel2007=false){ //文件引入 require_once APP_ROOT.'/Api/excel/PHPExcel.php'; require_once APP_ROOT.'/Api/excel/PHPExcel/Writer/Excel2007.php'; if(empty($filename)) $filename = time(); if( !is_array($indexKey)) return false; $header_arr = 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'); //初始化PHPExcel() $objPHPExcel = new PHPExcel(); //設置保存版本格式 if($excel2007){ $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); $filename = $filename.'.xlsx'; }else{ $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $filename = $filename.'.xls'; } //接下來就是寫數據到表格裏面去 $objActSheet = $objPHPExcel->getActiveSheet(); //$startRow = 1; foreach ($list as $row) { foreach ($indexKey as $key => $value){ //這裏是設置單元格的內容 $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]); } $startRow++; } // 下載這個表格,在瀏覽器輸出 header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download");; header('Content-Disposition:attachment;filename='.$filename.''); header("Content-Transfer-Encoding:binary"); $objWriter->save('php://output'); }
導出文件,還能夠設置模板spa
function exportExcel($list,$filename,$indexKey=array()){ require_once dirname(__FILE__) . '/Lib/Classes/PHPExcel/IOFactory.php'; require_once dirname(__FILE__) . '/Lib/Classes/PHPExcel.php'; require_once dirname(__FILE__) . '/Lib/Classes/PHPExcel/Writer/Excel2007.php'; $header_arr = 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'); //$objPHPExcel = new PHPExcel(); //初始化PHPExcel(),不使用模板 $template = dirname(__FILE__).'/template.xls'; //使用模板 $objPHPExcel = PHPExcel_IOFactory::load($template); //加載excel文件,設置模板 $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); //設置保存版本格式 //接下來就是寫數據到表格裏面去 $objActSheet = $objPHPExcel->getActiveSheet(); $objActSheet->setCellValue('A2', "活動名稱:江南極客"); $objActSheet->setCellValue('C2', "導出時間:".date('Y-m-d H:i:s')); $i = 4; foreach ($list as $row) { foreach ($indexKey as $key => $value){ //這裏是設置單元格的內容 $objActSheet->setCellValue($header_arr[$key].$i,$row[$value]); } $i++; } // 1.保存至本地Excel表格 //$objWriter->save($filename.'.xls'); // 2.接下來固然是下載這個表格了,在瀏覽器輸出就行了 header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download");; header('Content-Disposition:attachment;filename="'.$filename.'.xls"'); header("Content-Transfer-Encoding:binary"); $objWriter->save('php://output'); }
先弄一個excel模板.net
而後使用該模板導出數據
除此以外還能夠對錶格屬性作不少自定義。感興趣的本身看看吧!
轉: https://blog.csdn.net/a8240357/article/details/80758222