下載 PHPExcel-1.8.zip 包
http://note.youdao.com/noteshare?id=62394c2f4858f975b1772d8c3f6a6cc7&sub=2D4C6AC3B1A14C4099F94BEC97A7ADD7
需要導(dǎo)出的數(shù)據(jù)格式
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 導(dǎo)出類庫
<?php
class Excelreport{
//文件名
private $fileName = 'user';
//數(shù)據(jù)內(nèi)容
private $_data = [];
//phpexcel類
private $objPHPExcel = '';
//構(gòu)造函數(shù)
public function __construct($fileName = '')
{
$this->setFileName($fileName);
include_once '../plugins/phpexcel/PHPExcel.php');
$this->objPHPExcel = new PHPExcel();
}
//設(shè)置要導(dǎo)出的文件名
public function setFileName($fileName)
{
$this->fileName = $fileName;
}
/**
* @brief 寫入標(biāo)題操作
* @param $data array 一維數(shù)組
*/
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]);
//是否設(shè)置單元格寬度
if(is_array($data[$i])){
//設(shè)置寬度
$this->objPHPExcel->getActiveSheet()->getColumnDimension($array[$i])->setWidth($data[$i][1]);
}else{
$this->objPHPExcel -> getActiveSheet() -> getColumnDimension($array[$i]) -> setAutoSize(true);
}
//設(shè)置水平居中
$this->objPHPExcel->getActiveSheet()->getStyle($array[$i].'1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//設(shè)置垂直居中
$this->objPHPExcel->getActiveSheet()->getStyle($array[$i].'1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//設(shè)置單元格背景色
$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 寫入內(nèi)容操作,每次存入一行
* @param $data array 數(shù)據(jù)
*/
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);
//設(shè)置水平居中
$this->objPHPExcel->getActiveSheet()->getStyle($array[$k].$num)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//設(shè)置垂直居中
$this->objPHPExcel->getActiveSheet()->getStyle($array[$k].$num)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//設(shè)置自動換行
$this->objPHPExcel->getActiveSheet()->getStyle($array[$k].$num)->getAlignment()->setWrapText(true);
}
}
//設(shè)置表頭行高
$this->objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(40);
//設(shè)置行高
$this->objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(35);
}
//開始下載
public function toDownload($data = '')
{
include_once(IWeb::$app->getBasePath().'plugins/phpexcel/PHPExcel/IOFactory.php');//靜態(tài)類
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;
}
}
調(diào)用
$obj = new Excelreport('order_refundment_list');
$obj->setTitle(array(
array("訂單編號",'30'),
array("收款金額",'30'),
array("付款人",'30'),
array("支付方式",'30'),
array("支付狀態(tài)",'30'),
array("完成時間",'30'),
));
$obj->setData($data);
$obj->toDownload();
效果圖