您好,登錄后才能下訂單哦!
這篇文章主要介紹了基于PHPexecl類如何生成復(fù)雜的報(bào)表表頭,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
以前一直有需求,能把Execl里面的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù),并且把數(shù)據(jù)庫(kù)里面的數(shù)據(jù)導(dǎo)出到Execl中。
require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php'; class PHPExeclCore extends PHPExcel_IOFactory{ public static function SummerCreateExecl($Head,$data) { self::SummerCreateExeclHead($Head,$data,"Excel2007"); } public static function SummerReadExecl($dir) { if(!file_exists($dir)) { echo "Execl Not Exist"; } else { $PHPExeclObj = self::load($dir); $sheetCount = $PHPExeclObj->getSheetCount(); //得到Execl中包含的Sheet工作簿的數(shù)量 for($i=0;$i<$sheetCount;$i++) { $ActiveSheet = $PHPExeclObj->getSheet($i); $highestRow = $ActiveSheet->getHighestRow(); // 取得總列數(shù) $allColumn = $ActiveSheet->getHighestColumn(); //通過(guò)嵌套循環(huán)來(lái)讀取sheet工作簿里面的內(nèi)容 for($Col='A';$Col<$allColumn;$Col++) { for($Row=1;$Row<$highestRow;$Row++) { $Data[$Col][$Row] = $ActiveSheet->getCell($Col.$Row)->getValue(); } } } } return $Data; } /* * 將數(shù)據(jù)寫入到數(shù)據(jù)表中 * $Data Array 表示要插入進(jìn)Execl數(shù)據(jù) * $RuleData Array 表示數(shù)據(jù)格式的規(guī)則數(shù)組 * $i int 表示從第幾行起的插入數(shù)據(jù) * **/ public static function SummerInsertDateToExecl($sheet,$Head,$Data,$n=3,$RuleData=array()) { $SimpleHead = self::getHead($Head); $row = $n; foreach($Data as $key=>$valueArr) { $m = 0; foreach($valueArr as $k=>$v) { $StartCol = PHPExcel_Cell::stringFromColumnIndex($m).$row; $sheet->getCell($StartCol)->setValue($v); $sheet->getStyle($StartCol)->getAlignment()->applyFromArray( array( 'horizontal'=> PHPExcel_Style_Alignment::HORIZONTAL_CENTER, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => TRUE, ) ); if(isset($SimpleHead[$k]['col'])) { $m = $m + $SimpleHead[$k]['col']-1; $endCol = PHPExcel_Cell::stringFromColumnIndex($m).$row; $sheet->mergeCells($StartCol.":".$endCol); } $m++; $type = false; if(isset($SimpleHead[$k]['type'])) { $type = $SimpleHead[$k]['type']; $AllowArray = $SimpleHead[$k]['allowarray']; } //設(shè)置單元格的數(shù)據(jù)驗(yàn)證 if($type) { switch ($type) { case 'list': self::setSelectionRange($sheet, $StartCol,$AllowArray); break; case 'range': self::setValueRange($sheet, $StartCol,$AllowArray); break; } } } $row ++ ; } } /* * 生成Execl單元格備注 * $sheet 當(dāng)前的工作簿對(duì)象 * $Cell 需要設(shè)置屬性的單元格 * $content 備注內(nèi)容 * */ private static function setComment($sheet,$Cell,$content) { $sheet->getComment($Cell)->setAuthor('4399om'); $objCommentRichText = $sheet->getComment($Cell)->getText()->createTextRun('4399om:'); $objCommentRichText->getFont()->setBold(true); $sheet->getComment($Cell)->getText()->createTextRun("\r\n"); $sheet->getComment($Cell)->getText()->createTextRun($content); $sheet->getComment($Cell)->setWidth('100pt'); $sheet->getComment($Cell)->setHeight('100pt'); $sheet->getComment($Cell)->setMarginLeft('150pt'); $sheet->getComment($Cell)->getFillColor()->setRGB('EEEEEE'); } /* * 現(xiàn)在單元格的有效數(shù)據(jù)范圍,暫時(shí)僅限于數(shù)字 * $sheet 當(dāng)前的工作簿對(duì)象 * $Cell 需要設(shè)置屬性的單元格 * $ValueRange array 允許輸入數(shù)組的訪問(wèn) */ private static function setValueRange($sheet,$Cell,$ValueRange) { //設(shè)置單元格的的數(shù)據(jù)類型是數(shù)字,并且保留有效位數(shù) $sheet->getStyle($Cell)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00); $ValueRange = explode(",",$ValueRange); //開始數(shù)值有效訪問(wèn)設(shè)定 $objValidation = $sheet->getCell($Cell)->getDataValidation(); $objValidation->setType( PHPExcel_Cell_DataValidation:: TYPE_WHOLE ); $objValidation->setErrorStyle( PHPExcel_Cell_DataValidation:: STYLE_STOP ); $objValidation->setAllowBlank(true); $objValidation->setShowInputMessage( true); //設(shè)置顯示提示信息 $objValidation->setShowErrorMessage( true); //設(shè)置顯示錯(cuò)誤信息 $objValidation->setErrorTitle('輸入錯(cuò)誤'); //錯(cuò)誤標(biāo)題 $objValidation->setError('請(qǐng)輸入數(shù)據(jù)范圍在從'.$ValueRange[0].'到'.$ValueRange[1].'之間的所有值'); //錯(cuò)誤內(nèi)容 $objValidation->setPromptTitle('允許輸入'); //設(shè)置提示標(biāo)題 $objValidation->setPrompt('請(qǐng)輸入數(shù)據(jù)范圍在從'.$ValueRange[0].'到'.$ValueRange[1].'之間的所有值'); //提示內(nèi)容 $objValidation->setFormula1($ValueRange['0']); //設(shè)置最大值 $objValidation->setFormula2($ValueRange['1']); //設(shè)置最小值 } private static function OutinputHeader($objWriter) { $fileName = str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header('Content-Disposition:inline;filename="'.$fileName.'"'); header("Content-Transfer-Encoding: binary"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Pragma: no-cache"); $objWriter->save('php://output'); exit; } //數(shù)據(jù)控制,設(shè)置單元格數(shù)據(jù)在一個(gè)可選方位類 private static function setSelectionRange($sheet,$Cell,$rangeStr,$Title="數(shù)據(jù)類型") { $objValidation = $sheet->getCell($Cell)->getDataValidation(); $objValidation -> setType(PHPExcel_Cell_DataValidation::TYPE_LIST) -> setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_STOP) -> setAllowBlank(true) -> setShowInputMessage(true) -> setShowErrorMessage(true) -> setShowDropDown(true) -> setErrorTitle('輸入的值有誤') -> setError('您輸入的值不在下拉框列表內(nèi).') -> setPromptTitle('"'.$Title.'"') -> setFormula1('"'.$rangeStr.'"'); } /* * 構(gòu)建表頭 * */ public static function RecursionCreateExecl($head,$data) { $PHPExecl = new PHPExcel(); $objWriter = self::createWriter($PHPExecl, 'Excel2007'); $PHPExecl->getProperties()->setCreator("4399om") ->setLastModifiedBy("Summer") ->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"); $PHPExecl->setActiveSheetIndex(0); $sheet = $PHPExecl->getActiveSheet(); self::HandleHeadToNode($sheet, $head,1,0,0); self::SummerInsertDateToExecl($sheet,$head,$data,4); self::OutinputHeader($objWriter); } private static function HandleHeadToNode($sheet,$Head,$beginRow,$col,$StartCol) { foreach($Head as $key=>$cells) { $row = $beginRow; //表示行 $beginCol = PHPExcel_Cell::stringFromColumnIndex($col).$row; $sheet->getCell($beginCol)->setValue($cells['value']); //設(shè)置表格樣式 $sheet->getStyle($beginCol)->getAlignment()->applyFromArray( array( 'horizontal'=> PHPExcel_Style_Alignment::HORIZONTAL_CENTER, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => TRUE, ) ); $sheet->getStyle($beginCol)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_DARKGREEN); //設(shè)置單元格的寬度 if(isset($cells['width'])) { $Cell = $sheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($col)); $Cell->setWidth($cells['width']); } //哥元素打上標(biāo)記 if(isset($cells['Content'])) { self::setComment($sheet, $beginCol, $cells['Content']); } $merge = false; //合并單元格 if(isset($cells['col'])) { $col += $cells['col']-1; $merge = true; } if(isset($cells['row'])) { $row += $cells['row']-1; $merge = true; } if($merge) { $endCol = PHPExcel_Cell::stringFromColumnIndex($col).$row; $sheet->mergeCells($beginCol.":".$endCol); } $row ++; $col ++; //表示有存在孩子節(jié)點(diǎn) if(isset($cells['children']) && is_array($cells['children'])){ $cols = $StartCol; if(!self::IsExistChildren($cells['children'])) { $cols = $col-2; $StartCol = $col; } self::HandleHeadToNode($sheet,$cells['children'],$row,$cols,$StartCol); }else{ $StartCol = $col; } } } //判斷自己的孩子節(jié)點(diǎn)中是否存在孫子節(jié)點(diǎn) private static function IsExistChildren($Data) { foreach($Data as $key=>$value) { if(isset($value['children']) && is_array($value['children'])) { return true; } } return false; } //獲取底層數(shù)據(jù) private static function getHead($Head,&$Node=array()) { foreach($Head as $key=>$value) { if(isset($value['children']) && is_array($value['children'])) { self::getHead($value['children'],$Node); } else { $Node[] = $value; } } return $Node; } } $Head = array( array('value'=>'姓名','col'=>2,'row'=>2,'width'=>20,'type'=>'list','allowarray'=>'PHP開發(fā)工程師,PHP開發(fā)'), array('value'=>'第一天','col'=>2,'row'=>1,'width'=>20,'Content'=>'2014-12-29號(hào)', 'children'=> array( array('value'=>'上午','col'=>1,'width'=>20,'type'=>'range','allowarray'=>'10,100'), array('value'=>'下午','width'=>20), ), ), array('value'=>'第二天','col'=>2,'row'=>1,'width'=>20, 'children'=> array( array('value'=>'上午','width'=>20), array('value'=>'下午','width'=>20), ), ), ); $data = array( array('PHP開發(fā)工程師','12','吃飯1','睡覺1','起床刷牙2','吃飯睡覺2'), array('PHP開發(fā)工程師','25','吃飯1','睡覺1','起床刷牙2','吃飯睡覺2'), array('PHP開發(fā)工程師','50','吃飯1','睡覺1','起床刷牙2','吃飯睡覺2'), array('PHP開發(fā)工程師','99','吃飯1','睡覺1','起床刷牙2','吃飯睡覺2'), array('PHP開發(fā)工程師','10','吃飯1','睡覺1','起床刷牙2','吃飯睡覺2'), ); $Node = PHPExeclCore::RecursionCreateExecl($Head,$data);
得到的效果也基本符合需求:
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“基于PHPexecl類如何生成復(fù)雜的報(bào)表表頭”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。