Excel 单元格一些常用PHP代码
2023-05-16 加入收藏
使用的是phpexcel,基本用的原生语法,所见即所得,直接复制下面代码
vendor("PHPExcel.Classes.PHPExcel"); $objPHPExcel = new \PHPExcel(); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20); //设置列宽度 $objPHPExcel->getActiveSheet()->getRowDimension('10')->setRowHeight(20); //设置行高度 //整体设置字体和字体大小 $objPHPExcel->getDefaultStyle()->getFont()->setName( 'Arial'); $objPHPExcel->getDefaultStyle()->getFont()->setSize(12); //插入图片开始 $objDrawing = new \PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Thumb'); $objDrawing->setDescription('Thumbnail Image'); $objDrawing->setPath($_SERVER["DOCUMENT_ROOT"] . $web_logo); $objDrawing->setHeight(60); $objDrawing->setCoordinates('A1'); $objDrawing->setOffsetX(10 ); //上偏移 $objDrawing->setOffsetY(8); //左偏移 $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); //插入图片结束 $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); //加粗 $objPHPExcel->getActiveSheet()->mergeCells('B'.$i3.':E'.$i3);//合并单元格 $objPHPExcel->getActiveSheet()->getStyle('B'.$i2)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//水平居中单元格 $objPHPExcel->getActiveSheet()->getStyle('B'.$i2)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中单元格 $objPHPExcel->getActiveSheet()->getStyle('B'.$i2)->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('c5c5c5'); //添加背景色 $objPHPExcel->setActiveSheetIndex()->setCellValue('A10', '设备名称')->setCellValue('B10', '数量')->setCellValue('C10', '价格(元)')->setCellValue('D10', '安装费(元)')->setCellValue('E10', '备注'); //填充文字 // 输出Excel表格到浏览器下载 $time = getTime(); $name = 'baojia_'.$time ; $PHPWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007"); //创建生成的格式 $file_dir = ROOT_PATH . 'excel' . DS . $name.'.xlsx'; //文件存放目录 $PHPWriter->save($file_dir); //表示在$path路径下面生成demo.xlsx文件 //$this->error('请设置选项以及数量'); $file_name = $name.".xlsx"; //下载文件名 $file_dir = ROOT_PATH . 'excel' . DS; //文件存放目录 if(!file_exists($file_dir . $file_name)){ // echo "文件找不到"; $this->error('文件找不到'); exit(); }else{ $this->success('获取成功',null,['name'=>$name]); }