收藏下phpexcel常用的字型設定,顏色設定和單元格保護
阿新 • • 發佈:2019-02-01
function createExcel($array) { if(empty($array)) return false; /** Error reporting */ /*error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE);*/ set_time_limit(0); $o_ci = & get_instance(); $o_ci->load->library('PHPExcel');//引入PHPExcel類庫 $objPHPExcel = new PHPExcel();//例項化PHPExcel類 //設定文件基本屬性 $objPHPExcel->getProperties() ->setCreator('Gary.F.Dong') //設定建立者 ->setLastModifiedBy(date('Y-m-d',time())) //設定時間 ->setTitle('Filters') //設定標題 ->setSubject('產品Filters選項') //設定備註 ->setDescription('匯出所有產品的Filters') //設定描述 ->setKeywords('Filters') //設定關鍵字 | 標記 ->setCategory('Filters'); //設定類別 //$objPHPExcel->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中 // 迴圈所有的產品線,一個產品線作為一個sheet $index = 0; foreach ($array['prodlines'] as $key => $value) { //設定預設行高 $objPHPExcel->getActiveSheet($index)->getDefaultRowDimension()->setRowHeight(20); //設定單元格的對齊方式 $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);//水平居右 //設定第一列的字型大小 $objPHPExcel->getActiveSheet()->getStyle("A1:A".(count($array['spectitle'][$value['conf_name']])+1))->getFont()->setSize(14); //設定單元格保護 //$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // 為了使任何表保護,需設定為真 //$objPHPExcel->getActiveSheet()->protectCells("A1:A".(count($array['spectitle'][$value['conf_name']])+1), 'Gary' ); // 加密密碼是 Gary //取得當前行數和列數,然後取消中間部分的單元格保護 $all_rows_num = count($array['spectitle'][$value['conf_name']])+1; $all_column_num = count($array['allprod'][$value['conf_name']])+1; $columnString = PHPExcel_Cell::stringFromColumnIndex($all_column_num); //$objPHPExcel->getActiveSheet()->getStyle("B2:".$columnString.$all_rows_num)->getProtection()->setLocked( PHPExcel_Style_Protection::PROTECTION_UNPROTECTED ); //如果當前產品線的attributes為空,跳過 if(empty($array['spectitle'][$value['conf_name']])) {continue;} //if(in_array($value['conf_name'], array('led_lighting','lighting','monitor'))) continue; //這隻A1 欄位的標題 $objPHPExcel->setActiveSheetIndex($index)->setCellValue('A1','Filter'); $objPHPExcel->getActiveSheet()->getStyle( 'A1')->getFont()->setSize(14); $objPHPExcel->getActiveSheet()->getStyle( 'A1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle( 'A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED); //由於列數多大上百列,所以採取動態迴圈的方式 $column = 2; foreach($array['spectitle'][$value['conf_name']] as $spectitlekey => $rows){ //行寫入,迴圈行數 //$span = ord("B"); //獲取A的ASCII碼 $span = 1; //設定父級填充顏色 if($rows['parent_id'] ==0){ $objPHPExcel->getActiveSheet()->getStyle('A'.$column)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet()->getStyle('A'.$column)->getFill()->getStartColor()->setRGB('cd99fe'); }else{ //否則按照單雙號去設定顏色 /*if($column%2==1){ $objPHPExcel->getActiveSheet($index)->getStyle('A'.$column)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet($index)->getStyle('A'.$column)->getFill()->getStartColor()->setRGB('ddddff'); }else{ $objPHPExcel->getActiveSheet($index)->getStyle('A'.$column)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->getActiveSheet($index)->getStyle('A'.$column)->getFill()->getStartColor()->setRGB('efefef'); }*/ } $objPHPExcel->setActiveSheetIndex($index)->setCellValue('A'.$column,$rows['search_item']); // 設定寬度 $objPHPExcel->getActiveSheet($index)->getColumnDimension('A')->setAutoSize(true); //$obpe->getActiveSheet()->getColumnDimension('B')->setWidth(10); foreach($array['allprod'][$value['conf_name']] as $prodid=>$products){// 列寫入 if(empty($products)) continue; //$j = chr($span); $j = PHPExcel_Cell::stringFromColumnIndex($span); //寫入每一列第一行產品名 $objPHPExcel->setActiveSheetIndex($index)->setCellValue($j.'1',$products['model'] ); //$objPHPExcel->getActiveSheet()->getStyle( $j.'1')->getFont()->setSize(14); //$objPHPExcel->getActiveSheet()->getStyle( $j.'1')->getFont()->setBold(true); //$objPHPExcel->getActiveSheet()->getStyle($j.'1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED); $curSpecValue = $array['specvalues'][$value['conf_name']][$products['prod_id']][$rows['search_item']]; if(empty($curSpecValue||!isset($curSpecValue))) $curSpecValue=''; $objPHPExcel->setActiveSheetIndex($index)->setCellValue($j.$column,$curSpecValue ); $objPHPExcel->getActiveSheet($index)->getColumnDimension($j)->setAutoSize(true); $span++; } $column++; } // Rename worksheet $objPHPExcel->getActiveSheet($index)->setTitle($value['conf_name']); $index++; $objPHPExcel->createSheet(); $objPHPExcel->setActiveSheetIndex($index); //if($index==3) break; } // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); ob_end_clean(); // Added by me ob_start(); // Added by me $filename = 'filters-'.date('Y-m-d',time()).'.xlsx'; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save(BQ_ADM_ROOT.'/media/tmp/'.$filename); return BQ_ADM_URL.'/media/tmp/'.$filename; }