一、导出表格注意点1.数字导出变成科学公式 参数两边加上 ”\t“$worksheet->setCellValue('C'.$hang,"\t".$v['title']."\t");2.内外边框线 表格格式$styleOneArray=['alignment'=>['horizontal'=>\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,// 水平居中'vertical'=>\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER//垂直居中],'font'=>['bold'=>true],// 字体加粗];$worksheet->getStyle('A1'.':D'.$hang)->applyFromArray($styleOneArray);$worksheet->getStyle('A1'.':D'.$hang)->applyFromArray($styleTwoArray);3.自动换行$excel->getActiveSheet()->getStyle('A1:D'.$hang)->getAlignment()->setWrapText(true);4.单元格添加背景$worksheet->getStyle('A2:I3')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('87CEFA');5.下载图片$url='.';//下载链接$drawing[$hang]=new\PhpOffice\PhpSpreadsheet\Worksheet\Drawing();$drawing[$hang]->setName('图片');$drawing[$hang]->setDescription('图片');$drawing[$hang]->setPath($url);$drawing[$hang]->setWidth(70);$drawing[$hang]->setHeight(70);$drawing[$hang]->setCoordinates('AC'.$hang);$drawing[$hang]->setOffsetX(0);$drawing[$hang]->setOffsetY(0);$drawing[$hang]->setWorksheet($excel->getActiveSheet());6.表格行高$worksheet->getRowDimension($hang)->setRowHeight(28);7.单元格合并$excel->getActiveSheet()->mergeCells('A'.$hang.':AF'.$hang);8.单元格文本格式$worksheet->getCell('C'.$hang)->setValueExplicit('',\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);二、导入表格注意点1.时间转换2.是否为数字publicfunctionimport(){$file=$this->request->post('file');$file=str_replace($this->domain_url,"",$file);if(!$file){$this->error(__('Parameter %s can not be empty','file'));}$filePath=ROOT_PATH.DS.'public'.DS.$file;// print_r($filePath);exit;if(!is_file($filePath)){$this->error(__('No results were found'));}$ext=pathinfo($filePath,PATHINFO_EXTENSION);if(!in_array($ext,['csv','xls','xlsx'])){$this->error(__('Unknown data format'));}if($ext==='csv'){$file=fopen($filePath,'r');$filePath=tempnam(sys_get_temp_dir(),'import_csv');$fp=fopen($filePath,"w");$n=0;while($line=fgets($file)){$line=rtrim($line,"\n\r\0");$encoding=mb_detect_encoding($line,['utf-8','gbk','latin1','big5']);if($encoding!='utf-8'){$line=mb_convert_encoding($line,'utf-8',$encoding);}if($n==0||preg_match('/^".*"$/',$line)){fwrite($fp,$line."\n");}else{fwrite($fp,'"'.str_replace(['"',','],['""','","'],$line)."\"\n");}$n++;}fclose($file)||fclose($fp);$reader=newCsv();}elseif($ext==='xls'){$reader=newXls();}else{$reader=newXlsx();}$insert=[];try{if(!$PHPExcel=$reader->load($filePath)){$this->error(__('Unknown data format'));}$currentSheet=$PHPExcel->getSheet(0);$allRow=$currentSheet->getHighestRow();$i=1;for($j=2;$j<=$allRow;$j++){$insert[$i-1]=['csname'=>trim($currentSheet->getCell("B".$j)->getValue()),'name'=>trim($currentSheet->getCell("C".$j)->getValue()),'cssname'=>trim($currentSheet->getCell("D".$j)->getValue()),'csssname'=>trim($currentSheet->getCell("E".$j)->getValue()),'allmoney'=>trim($currentSheet->getCell("F".$j)->getValue()),'catename'=>trim($currentSheet->getCell("G".$j)->getValue()),'cstime'=>trim($currentSheet->getCell("H".$j)->getValue()),'csarea'=>trim($currentSheet->getCell("I".$j)->getValue())];$i++;}}catch(Exception$exception){$this->error($exception->getMessage());}if(!$insert){$this->error(__('No rows were updated'));}$insertArr=[];$return=['code'=>true,'msg'=>'成功'];foreach($insertas$key=>$val){// print_r($val);exit;$temp['name']=$val['name'];$temp['csname']=$val['csname'];$temp['cssname']=$val['cssname'];$temp['allmoney']=$val['allmoney'];$temp['csarea']=$val['csarea'];//时间if(!empty($val['cstime'])){$time=$val['cstime'];//表格时间样式$d=25569;$t=24*60*60;$checkdate=intval(($time-$d)*$t);$checktime=gmdate("Y-m-d H:i:s",$checkdate);$temp['cstime']=!empty($checktime)?strtotime($checktime):0;}else{$return=['code'=>false,'msg'=>'时间不为空'];break;}//来源地省市if($val['csssname']){$area=new\app\common\model\Area;$wherea=[];$wherea['name']=$val['csssname'];$temptzf=[];$temptzf['other']=false;$categorys=$area->getOne("id,pid,name,shortname",$wherea,$temptzf);if($categorys['code']==false){$return=['code'=>false,'msg'=>'来源地:['.$val['csssname'].']不存在'];break;}else{$tzflaiyuanname=!empty($categorys['data'])?$categorys['data']['name']:'';}}else{$return=['code'=>false,'msg'=>'来源地不为空'];break;}if(!is_numeric($val['csmoney'])){$return=['code'=>false,'msg'=>'不为数字'];break;}if($val['csmoney']<0){$return=['code'=>false,'msg'=>'不为负值'];break;}// print_r($temp);exit;$insertArr[]=$temp;$result=Db::name('test')->insertGetId($temp);}if(!$return['code']){$this->error($return['msg']);}// $this->selfModel->allowField(true)->saveAll($insertArr);$this->success("导入成功");}三、PDF导出publicfunctiondaochupdf(){$id=$this->request->request("id");$xms=new\app\common\model\Xms();$where=[];$where['id']=$id;$ones=$xms->getOne('',$where);$one=$ones['data'];$pdf=new\TCPDF('L','mm','A4',true,'UTF-8',false);// 设置文档信息----文件创作者$pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor('');$pdf->SetTitle($one['name']);$pdf->setPrintHeader(false);// 去除页眉下划线$pdf->setPrintFooter(false);// 去除页脚下划线// 设置默认等宽字体$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);// 设置间距$pdf->SetMargins(1,1,1);$pdf->SetHeaderMargin(0);$pdf->SetFooterMargin(0);// 设置分页$pdf->SetAutoPageBreak(TRUE,PDF_MARGIN_BOTTOM);// 设置图像比例因子$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);// 设置默认字体构造子集模式global$l;$pdf->setLanguageArray($l);// 设置字体$pdf->SetFont('stsongstdlight','',10);// 添加一个页面$pdf->AddPage();if($one){$files=$one['files'];$html=' <html> <head> <style> .el-row { -webkit-box-sizing: border-box; box-sizing: border-box; position: relative; } .el-col-24 { width: 100%; } .title { line-height: 24px; background: #F1F7F5; padding-left: 5px; margin: 15px 0 5px; color: black; font-size: 14px; font-weight: bold; border-bottom: 2px solid #46BF93; } </style> </head> <body> <div class="el-row"> <div class="el-col el-col-24"> <div class="title"> <img src="https://.com/assets/img/xqbot.png" alt="" style="height:24px;width: 10px;">基本信息 </div> </div> </div> <div > <table> <tr> <td style="height:30px;width: 150px;text-align: right;">项目名称:</td> <td style="height:40px;width: 250px;text-align: left;padding-left: 20px;">'.$one['name'].'<i style="height:30px;font-size:8px;margin:3px;width:80px;color:#fff;background-color:'.$one['status_color'].';">'.$one['status_text'].'</i></td> </tr> <tr> <td style="height:30px;width: 150px;text-align: right;">简介:</td> <td style="width: 650px;text-align: left;padding-left: 20px;">'.$one['blurb'].'</td> </tr> <tr> <td style="height:20px;"></td> </tr> <tr> <td style="height:30px;width: 150px;text-align: right;">活动开展情况:</td> <td style="text-align: left;padding-left: 20px;width: 450px;"> <ul style="list-style: none;"> ';foreach($filesas$k=>$v){$html.=' <li > <img src="'.$v['url'].'" style="width:70px;height:70px;text-align: left;">'.$v['name'].' </li>';}$html.='</ul></td></tr></table></div></body></html>';$pdf->writeHTML($html,true,false,true,false,'');//$pdf->writeHTMLCell(0, 0, '', '', $html, 'LRTB', 1, 0, true, 'L', true);$pdf->lastPage();}$pdf->lastPage();// 默认是I:在浏览器中打开,D:下载,F:在服务器生成pdf ,S:只返回pdf的字符串,个人感觉无实在意义// $pdf->Output('down.pdf', 'I');// exit;$title=$one['name'].'.pdf';$dir='/www/wwwroot/abc.abc.com/public/uploads/pdf/'.$title;$pdf->Output($dir,'I');exit;}
一、导出表格注意点1.数字导出变成科学公式 参数两边加上 ”\t“$worksheet->setCellValue('C'.$hang,"\t".$v['title']."\t");2.内外边框线 表格格式$styleOneArray=['alignment'=>['horizontal'=>\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,// 水平居中'vertical'=>\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER//垂直居中],'font'=>['bold'=>true],// 字体加粗];$worksheet->getStyle('A1'.':D'.$hang)->applyFromArray($styleOneArray);$worksheet->getStyle('A1'.':D'.$hang)->applyFromArray($styleTwoArray);3.自动换行$excel->getActiveSheet()->getStyle('A1:D'.$hang)->getAlignment()->setWrapText(true);4.单元格添加背景$worksheet->getStyle('A2:I3')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('87CEFA');5.下载图片$url='.';//下载链接$drawing[$hang]=new\PhpOffice\PhpSpreadsheet\Worksheet\Drawing();$drawing[$hang]->setName('图片');$drawing[$hang]->setDescription('图片');$drawing[$hang]->setPath($url);$drawing[$hang]->setWidth(70);$drawing[$hang]->setHeight(70);$drawing[$hang]->setCoordinates('AC'.$hang);$drawing[$hang]->setOffsetX(0);$drawing[$hang]->setOffsetY(0);$drawing[$hang]->setWorksheet($excel->getActiveSheet());6.表格行高$worksheet->getRowDimension($hang)->setRowHeight(28);7.单元格合并$excel->getActiveSheet()->mergeCells('A'.$hang.':AF'.$hang);8.单元格文本格式$worksheet->getCell('C'.$hang)->setValueExplicit('',\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);二、导入表格注意点1.时间转换2.是否为数字publicfunctionimport(){$file=$this->request->post('file');$file=str_replace($this->domain_url,"",$file);if(!$file){$this->error(__('Parameter %s can not be empty','file'));}$filePath=ROOT_PATH.DS.'public'.DS.$file;// print_r($filePath);exit;if(!is_file($filePath)){$this->error(__('No results were found'));}$ext=pathinfo($filePath,PATHINFO_EXTENSION);if(!in_array($ext,['csv','xls','xlsx'])){$this->error(__('Unknown data format'));}if($ext==='csv'){$file=fopen($filePath,'r');$filePath=tempnam(sys_get_temp_dir(),'import_csv');$fp=fopen($filePath,"w");$n=0;while($line=fgets($file)){$line=rtrim($line,"\n\r\0");$encoding=mb_detect_encoding($line,['utf-8','gbk','latin1','big5']);if($encoding!='utf-8'){$line=mb_convert_encoding($line,'utf-8',$encoding);}if($n==0||preg_match('/^".*"$/',$line)){fwrite($fp,$line."\n");}else{fwrite($fp,'"'.str_replace(['"',','],['""','","'],$line)."\"\n");}$n++;}fclose($file)||fclose($fp);$reader=newCsv();}elseif($ext==='xls'){$reader=newXls();}else{$reader=newXlsx();}$insert=[];try{if(!$PHPExcel=$reader->load($filePath)){$this->error(__('Unknown data format'));}$currentSheet=$PHPExcel->getSheet(0);$allRow=$currentSheet->getHighestRow();$i=1;for($j=2;$j<=$allRow;$j++){$insert[$i-1]=['csname'=>trim($currentSheet->getCell("B".$j)->getValue()),'name'=>trim($currentSheet->getCell("C".$j)->getValue()),'cssname'=>trim($currentSheet->getCell("D".$j)->getValue()),'csssname'=>trim($currentSheet->getCell("E".$j)->getValue()),'allmoney'=>trim($currentSheet->getCell("F".$j)->getValue()),'catename'=>trim($currentSheet->getCell("G".$j)->getValue()),'cstime'=>trim($currentSheet->getCell("H".$j)->getValue()),'csarea'=>trim($currentSheet->getCell("I".$j)->getValue())];$i++;}}catch(Exception$exception){$this->error($exception->getMessage());}if(!$insert){$this->error(__('No rows were updated'));}$insertArr=[];$return=['code'=>true,'msg'=>'成功'];foreach($insertas$key=>$val){// print_r($val);exit;$temp['name']=$val['name'];$temp['csname']=$val['csname'];$temp['cssname']=$val['cssname'];$temp['allmoney']=$val['allmoney'];$temp['csarea']=$val['csarea'];//时间if(!empty($val['cstime'])){$time=$val['cstime'];//表格时间样式$d=25569;$t=24*60*60;$checkdate=intval(($time-$d)*$t);$checktime=gmdate("Y-m-d H:i:s",$checkdate);$temp['cstime']=!empty($checktime)?strtotime($checktime):0;}else{$return=['code'=>false,'msg'=>'时间不为空'];break;}//来源地省市if($val['csssname']){$area=new\app\common\model\Area;$wherea=[];$wherea['name']=$val['csssname'];$temptzf=[];$temptzf['other']=false;$categorys=$area->getOne("id,pid,name,shortname",$wherea,$temptzf);if($categorys['code']==false){$return=['code'=>false,'msg'=>'来源地:['.$val['csssname'].']不存在'];break;}else{$tzflaiyuanname=!empty($categorys['data'])?$categorys['data']['name']:'';}}else{$return=['code'=>false,'msg'=>'来源地不为空'];break;}if(!is_numeric($val['csmoney'])){$return=['code'=>false,'msg'=>'不为数字'];break;}if($val['csmoney']<0){$return=['code'=>false,'msg'=>'不为负值'];break;}// print_r($temp);exit;$insertArr[]=$temp;$result=Db::name('test')->insertGetId($temp);}if(!$return['code']){$this->error($return['msg']);}// $this->selfModel->allowField(true)->saveAll($insertArr);$this->success("导入成功");}三、PDF导出publicfunctiondaochupdf(){$id=$this->request->request("id");$xms=new\app\common\model\Xms();$where=[];$where['id']=$id;$ones=$xms->getOne('',$where);$one=$ones['data'];$pdf=new\TCPDF('L','mm','A4',true,'UTF-8',false);// 设置文档信息----文件创作者$pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor('');$pdf->SetTitle($one['name']);$pdf->setPrintHeader(false);// 去除页眉下划线$pdf->setPrintFooter(false);// 去除页脚下划线// 设置默认等宽字体$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);// 设置间距$pdf->SetMargins(1,1,1);$pdf->SetHeaderMargin(0);$pdf->SetFooterMargin(0);// 设置分页$pdf->SetAutoPageBreak(TRUE,PDF_MARGIN_BOTTOM);// 设置图像比例因子$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);// 设置默认字体构造子集模式global$l;$pdf->setLanguageArray($l);// 设置字体$pdf->SetFont('stsongstdlight','',10);// 添加一个页面$pdf->AddPage();if($one){$files=$one['files'];$html=' <html> <head> <style> .el-row { -webkit-box-sizing: border-box; box-sizing: border-box; position: relative; } .el-col-24 { width: 100%; } .title { line-height: 24px; background: #F1F7F5; padding-left: 5px; margin: 15px 0 5px; color: black; font-size: 14px; font-weight: bold; border-bottom: 2px solid #46BF93; } </style> </head> <body> <div class="el-row"> <div class="el-col el-col-24"> <div class="title"> <img src="https://.com/assets/img/xqbot.png" alt="" style="height:24px;width: 10px;">基本信息 </div> </div> </div> <div > <table> <tr> <td style="height:30px;width: 150px;text-align: right;">项目名称:</td> <td style="height:40px;width: 250px;text-align: left;padding-left: 20px;">'.$one['name'].'<i style="height:30px;font-size:8px;margin:3px;width:80px;color:#fff;background-color:'.$one['status_color'].';">'.$one['status_text'].'</i></td> </tr> <tr> <td style="height:30px;width: 150px;text-align: right;">简介:</td> <td style="width: 650px;text-align: left;padding-left: 20px;">'.$one['blurb'].'</td> </tr> <tr> <td style="height:20px;"></td> </tr> <tr> <td style="height:30px;width: 150px;text-align: right;">活动开展情况:</td> <td style="text-align: left;padding-left: 20px;width: 450px;"> <ul style="list-style: none;"> ';foreach($filesas$k=>$v){$html.=' <li > <img src="'.$v['url'].'" style="width:70px;height:70px;text-align: left;">'.$v['name'].' </li>';}$html.='</ul></td></tr></table></div></body></html>';$pdf->writeHTML($html,true,false,true,false,'');//$pdf->writeHTMLCell(0, 0, '', '', $html, 'LRTB', 1, 0, true, 'L', true);$pdf->lastPage();}$pdf->lastPage();// 默认是I:在浏览器中打开,D:下载,F:在服务器生成pdf ,S:只返回pdf的字符串,个人感觉无实在意义// $pdf->Output('down.pdf', 'I');// exit;$title=$one['name'].'.pdf';$dir='/www/wwwroot/abc.abc.com/public/uploads/pdf/'.$title;$pdf->Output($dir,'I');exit;}