## 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');
}
}
~~~
- 总导航
- 第一章 前端
- 1.0 js自定义模板
- 第二章 PHP
- 1.0 推荐博客
- 2.0 基础理论
- 0.1 require和include区别
- 0.2 php运行机制
- 0.3 Http请求与响应
- 0.4 http响应状态
- 3.0 Laravel框架
- 0.1 Eloquent ORM
- 0.2 时间处理
- 0.3 数据迁移
- 0.4 填充数据
- 4.0 composer安装
- 5.0 缓存
- 0.1 Opcache
- 0.2 Memcache
- 0.3 Redis
- 0.4 Output buffer
- 第三章 MYSQL
- 1.0 新增和重置密码
- 第四章 前端插件集
- 1.0 PreloadJS
- 0.1 LoadQueue
- 0.2 加载单个文件
- 0.3 加载进度显示
- 0.4 加载指定目录图片
- 0.5 加载json图片
- 2.0 Swiper
- 3.0 ECharts
- 0.1 简单实例
- 0.2 title操作
- 0.3 tooltip操作
- 0.4 top显示数字
- 0.5 双数据显示
- 0.6 大数据缩放显示
- 0.7 综合
- 4.0 Vue
- 0.1 模板语法
- 0.2 条件与循环
- 0.3 处理用户输入
- 0.4 计算属性
- 0.5 事件绑定
- 0.6 动态渲染列表
- 5.0 layui
- 0.1 动态渲染nav
- 0.2 layer使用
- 6.0 IScroll
- 7.0 AlloyTouch
- 8.0 jquery.tagsinput
- 9.0 Editormd
- 第五章 应用软件
- 1.0 Xshell
- 一螺集
- PHP
- 打印一天的区间时间
- 友好时间输出
- 导出Excel
- JAVASCRIPT
- js回车事件