💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## php导出excel * 使用开源插件: phpexcel * github : [https://github.com/PHPOffice/PHPExcel](https://github.com/PHPOffice/PHPExcel) >[info] 简单封装 ~~~ class ReportExcel { private $objPHPExcel = null; private $objSheet = null; public function __construct() { $this->objPHPExcel = new PHPExcel(); $this->objSheet = $this->objPHPExcel->getActiveSheet(); } /** * 设置标题 * @param string $title * @throws PHPExcel_Exception */ public function setTitle($title = 'Report') { $this->objPHPExcel->getProperties()->setCreator("sgfoot") ->setLastModifiedBy("sgfoot") ->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"); $this->objSheet->setTitle($title); } //设置单元格 public function setCell($cell, $value) { $this->objSheet->setCellValue($cell, $value); } /** * 设置列宽 * @param $column * @param int $width */ public function setWidth($column, $width = 12) { $this->objSheet->getColumnDimension($column)->setWidth($width); } /** * 设置文本 * @param $cell * @param $value * @throws PHPExcel_Exception */ public function setText($cell, $value) { $this->objSheet->setCellValueExplicit($cell, $value, PHPExcel_Cell_DataType::TYPE_STRING); } /** * 设置字体和大小 * @param $columnName * @param int $fontSize * @param string $fimalyName * @author: wzl * @date: 2017 */ public function setFont($columnName, $fontSize = 12, $fimalyName = 'DengXian') { $this->objSheet->getStyle($columnName)->getFont()->setName($fimalyName)->setSize($fontSize); } /** * 设置字体颜色  * @param $columnName * @param string $color * @author: wzl * @date: 2017 */ public function setFontColor($columnName, $color = PHPExcel_Style_Color::COLOR_RED) { $this->objSheet->getStyle($columnName)->getFont()->getColor()->setARGB($color); } /** * 设置单元格里的字体位置 * @param $columnName * @param string $position * @author: wzl * @date: 2017 */ public function setFontPosition($columnName, $position = 'left') { $this->objSheet->getStyle($columnName)->getAlignment() ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)//设置单元格垂直居中 ->setHorizontal($position);//单元格水平靠左 } /** * 设置字体为粗体 例:$obj->setBold('A1:J1'); * @param $columnName * @author: wzl * @date: 2017 */ public function setBold($columnName) { $this->objSheet->getStyle($columnName)->getFont()->setBold(true); } /** * 设置单元格垂直左右居中 * @param $columnName * @author: wzl * @date: 2017 */ public function setCellCenter($columnName) { $this->objSheet->getStyle($columnName)->getAlignment() ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER) ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER) ->setWrapText(true); } /** * 设置下划线 * @param $columnName * @author: wzl * @date: 2017 */ public function setUnderline($columnName) { $this->objSheet->getStyle($columnName)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE); } /** * 设置边框 * @param $columnName * @author: wzl * @date: 2017 */ public function setBorder($columnName) { $this->objSheet->getStyle($columnName)->applyFromArray($this->_setBorder()); } /** * 合并单元格 * @param $columnName * @author: wzl * @date: 2017 */ public function mergeCells($columnName) { $this->objSheet->mergeCells($columnName); } /** * 设置背景颜色,默认黄色  * const COLOR_BLACK = 'FF000000'; * const COLOR_WHITE = 'FFFFFFFF'; * const COLOR_RED = 'FFFF0000'; * const COLOR_DARKRED = 'FF800000'; * const COLOR_BLUE = 'FF0000FF'; * const COLOR_DARKBLUE = 'FF000080'; * const COLOR_GREEN = 'FF00FF00'; * const COLOR_DARKGREEN = 'FF008000'; * const COLOR_YELLOW = 'FFFFFF00'; * const COLOR_DARKYELLOW = 'FF808000'; * @param $i * @param string $color * @author: wzl * @date: 2017 */ public function setBackColor($i, $color = PHPExcel_Style_Color::COLOR_YELLOW) { $this->objSheet->getStyle($i)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);//填充颜色  $this->objSheet->getStyle($i)->getFill()->getStartColor()->setARGB($color);//填充颜色 } /** * 设置边框 * @author: wzl * @date: 2017 * @return array */ private function _setBorder() { $styleArray = array( 'borders' => array( 'allborders' => array( 'style' => PHPExcel_Style_Border::BORDER_THIN,//细边框 ), ), ); return $styleArray; } /** * 导出 * @param string $filename * @throws PHPExcel_Reader_Exception * @throws PHPExcel_Writer_Exception */ public function output($filename = '') { if (empty($filename)) { $filename = date('Ymd-' . mt_rand(100, 999)) . '.xlsx'; } else { if (strpos($filename, '.xlsx') === false) { $filename .= '.xlsx'; } } header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="' . $filename . '"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); } } ~~~