下载 PHPExcel-1.8.zip 包
http://note.youdao.com/noteshare?id=62394c2f4858f975b1772d8c3f6a6cc7&sub=2D4C6AC3B1A14C4099F94BEC97A7ADD7
需要导出的数据格式
array (size=6)
0 =>
array (size=6)
0 => string '20190517144546561483' (length=20)
1 => string '0.00' (length=4)
2 => string '***' (length=5)
3 => string '支付宝' (length=9)
4 => string '支付完成' (length=12)
5 => string '2019-05-17 14:45:46' (length=19)
1 =>
array (size=6)
0 => string '20190517143412343803' (length=20)
1 => string '0.01' (length=4)
2 => string '***' (length=11)
3 => string '微信' (length=9)
4 => string '支付完成' (length=12)
5 => string '2019-05-17 14:35:06' (length=19)
2 =>
array (size=6)
0 => string '20190517140912718326' (length=20)
1 => string '93.60' (length=5)
2 => string '***' (length=11)
3 => string '支付宝' (length=9)
4 => string '支付完成' (length=12)
5 => string '2019-05-17 14:09:28' (length=19)
3 =>
array (size=6)
0 => string '20190517111302964673' (length=20)
1 => string '109.00' (length=6)
2 => string '***' (length=11)
3 => string '微信' (length=9)
4 => string '支付完成' (length=12)
5 => string '2019-05-17 11:14:39' (length=19)
4 =>
array (size=6)
0 => string '20190517111225285648' (length=20)
1 => string '89.00' (length=5)
2 => string '***' (length=11)
3 => string '支付宝' (length=9)
4 => string '支付完成' (length=12)
5 => string '2019-05-17 11:13:21' (length=19)
5 =>
array (size=6)
0 => string '20190516154901304428' (length=20)
1 => string '109.00' (length=6)
2 => string '***' (length=11)
3 => string '支付宝' (length=9)
4 => string '支付完成' (length=12)
5 => string '2019-05-16 15:49:14' (length=19)
封装PHPexcle 导出类库
<?php
class Excelreport{
//文件名
private $fileName = 'user';
//数据内容
private $_data = [];
//phpexcel类
private $objPHPExcel = '';
//构造函数
public function __construct($fileName = '')
{
$this->setFileName($fileName);
include_once '../plugins/phpexcel/PHPExcel.php');
$this->objPHPExcel = new PHPExcel();
}
//设置要导出的文件名
public function setFileName($fileName)
{
$this->fileName = $fileName;
}
/**
* @brief 写入标题操作
* @param $data array 一维数组
*/
public function setTitle($data = array())
{
$array = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
$count = sizeof($data);
for($i=0;$i<$count;$i++){
$this->objPHPExcel->setActiveSheetIndex(0)
->setCellValue($array[$i].'1', is_array($data[$i])?$data[$i][0]:$data[$i]);
//是否设置单元格宽度
if(is_array($data[$i])){
//设置宽度
$this->objPHPExcel->getActiveSheet()->getColumnDimension($array[$i])->setWidth($data[$i][1]);
}else{
$this->objPHPExcel -> getActiveSheet() -> getColumnDimension($array[$i]) -> setAutoSize(true);
}
//设置水平居中
$this->objPHPExcel->getActiveSheet()->getStyle($array[$i].'1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置垂直居中
$this->objPHPExcel->getActiveSheet()->getStyle($array[$i].'1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//设置单元格背景色
$this->objPHPExcel->getActiveSheet()->getStyle($array[$i].'1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$this->objPHPExcel->getActiveSheet()->getStyle($array[$i].'1')->getFill()->getStartColor()->setARGB('0017C405');
}
}
/**
* @brief 写入内容操作,每次存入一行
* @param $data array 数据
*/
public function setData($data = array())
{
$array = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
$num = 1;
foreach($data as $key => $val){
$num++;
foreach ($val as $k => $v){
$this->objPHPExcel->setActiveSheetIndex(0)
->setCellValue($array[$k].$num, $v);
//设置水平居中
$this->objPHPExcel->getActiveSheet()->getStyle($array[$k].$num)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置垂直居中
$this->objPHPExcel->getActiveSheet()->getStyle($array[$k].$num)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//设置自动换行
$this->objPHPExcel->getActiveSheet()->getStyle($array[$k].$num)->getAlignment()->setWrapText(true);
}
}
//设置表头行高
$this->objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(40);
//设置行高
$this->objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(35);
}
//开始下载
public function toDownload($data = '')
{
include_once(IWeb::$app->getBasePath().'plugins/phpexcel/PHPExcel/IOFactory.php');//静态类
header('Content-Type: applicationnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$this->fileName.'_'.date('Y-m-d').'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
}
调用
$obj = new Excelreport('order_refundment_list');
$obj->setTitle(array(
array("订单编号",'30'),
array("收款金额",'30'),
array("付款人",'30'),
array("支付方式",'30'),
array("支付状态",'30'),
array("完成时间",'30'),
));
$obj->setData($data);
$obj->toDownload();
效果图