TP3.2.3 封装调用导出Excel
封装的步骤还是引入execl 对数据进行处理, l e t t e r 为 e x e c l 行 标 , letter为execl行标, letter为execl行标,field为数据库对应字段, p a r a m 就 为 我 们 在 文 件 中 显 示 的 行 栏 , param就为我们在文件中显示的行栏, param就为我们在文件中显示的行栏,list为传输过来的数据
/**
* *
* 导出指定时间的已分派数据
* @param array $letter 字母
* @param array $field 数据库字段名
* @param array $param 字段对应注释
* @param array $list 查询出来的列表 二维数组
* @param array $special_param 需要特殊处理的参数 例如 array(‘status’=>array(‘1,2,3’=>‘未付款,已付款,已删除’)),表示status有1、2、3三种状态对应三种注释
* @return string|bool
*/
static function down_data($letter, $field, $param, $list, $special_param = array())
{
$letter = range(strtoupper($letter[0]), strtoupper($letter[1]));
if (count($letter) != count($field) || count($param) != count($field) || count($letter) != count($param)) {
return false;
// self::ajax(‘1002’,‘参数数量有误’,‘error’);
}
//用户名、手机号、身份证、地址、还款订单、还款金额、应还款时间、逾期天数、罚金、分派人员
require_once ‘./Public/excel/PHPExcel.php’; //导入php文件
$filePath = ‘./Public/excel/record/record.xlsx’; //打开excel路径
$PHPReader = new \PHPExcel_Reader_Excel2007(); //默认用excel2007读取excel
$PHPExcel =
P
H
P
R
e
a
d
e
r
−
>
l
o
a
d
(
PHPReader->load(
PHPReader−>load(filePath); //加载具体文件
P
H
P
E
x
c
e
l
−
>
s
e
t
A
c
t
i
v
e
S
h
e
e
t
I
n
d
e
x
(
0
)
;
/
/
用
户
名
、
手
机
号
、
身
份
证
、
地
址
、
还
款
订
单
、
还
款
金
额
、
应
还
款
时
间
、
逾
期
天
数
、
罚
金
、
分
派
人
员
f
o
r
e
a
c
h
(
PHPExcel->setActiveSheetIndex(0); //用户名、手机号、身份证、地址、还款订单、还款金额、应还款时间、逾期天数、罚金、分派人员 foreach (
PHPExcel−>setActiveSheetIndex(0);//用户名、手机号、身份证、地址、还款订单、还款金额、应还款时间、逾期天数、罚金、分派人员foreach(letter as $k => $v) {
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
C
e
l
l
V
a
l
u
e
(
PHPExcel->getActiveSheet()->setCellValue(
PHPExcel−>getActiveSheet()−>setCellValue(v . ‘1’,
p
a
r
a
m
[
param[
param[k]);//写入
}
n
u
m
=
2
;
f
o
r
e
a
c
h
(
num = 2; foreach (
num=2;foreach(list as $key => $value) {
foreach ($field as $k => $v) {
$PHPExcel->getActiveSheet()->setCellValue($letter[$k] . $num, $value[$v]."\t");//写入
}
$num++;
// $PHPExcel->getActiveSheet()->setCellValue(‘A’ . $num, $value[‘truename’]);//写入
}
KaTeX parse error: Expected 'EOF', got '\PHPExcel' at position 13: objWriter = \̲P̲H̲P̲E̲x̲c̲e̲l̲_IOFactory::cre…PHPExcel, ‘Excel2007’);//保存类型
$name = date(‘YmdHis’, time()) . rand(‘10’, ‘99’) . ‘.xlsx’;
$objWriter->save(’./Public/excel/record/’ . $name);
if (file_exists(’./Public/excel/record/’ . KaTeX parse error: Expected '}', got 'EOF' at end of input: …_down_data_log(_SESSION[‘id’],‘导出数据字段:’.json_encode($field,JSON_UNESCAPED_UNICODE));
$url = C(‘MYURL’) . ‘Public/excel/record/’ . $name;
$url=self::addossimg(’./Public/excel/record/’ .
n
a
m
e
)
;
r
e
t
u
r
n
′
h
t
t
p
:
/
/
′
.
name); return 'http://'.
name);return′http://′.url;
} else {
return false;
}
}
下面为完整过程
public function down_data1(){
$channel_count = M(‘account_management_count’, ‘’, ‘USER’);
$list = $channel_count->SELECT();
if (IS_POST) {
foreach ($list as $k => $v) {
$list['otime'] = $v['otime'];
$list['money'] = $v['money'];
$list['o_type'] = $v['o_type'];
$list['o_credentials'] = $v['o_credentials'];
$list['status'] = $v['status'];
}
$letter = array('A', 'F');
$field = array('id','otime', 'money', 'o_type', 'o_credentials','status');
$param = array('序号','操作时间', '操作金额', '操作类型', '操作凭证','操作状态');
$res = MycommonController::down_data($letter, $field, $param, $list);
if (!empty($res)) {
MycommonController::ajax('1001', $res);
} else {
MycommonController::ajax('1002', '导出失败', 'error');
}
}
}
点击事件调用:
function down_data1() {
var t = $(‘form’).serializeArray();
var d = {};
$.each(t, function () {
d[this.name] = this.value;
});
d[‘type’] = ‘export’;
layer.load(2);
$.post(‘MODULE/Statistics/down_data1’, d, function (data) {
layer.closeAll(‘loading’);
if (data.code == ‘1001’) {
window.location.href = data.result;
} else {
if (data.status == ‘0’) {
layer.msg(data.info, {time: 2000, icon: 5});
} else {
layer.msg(data.result, {time: 2000, icon: 5});
}
}
});
}