TP3.2.3 封装调用导出Excel

该博客介绍了如何在ThinkPHP 3.2.3框架下封装一个导出Excel的功能,包括引入PHPExcel库,设置工作表,写入数据,并最终保存到指定路径。此外,还展示了如何在前端通过点击事件调用这个功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TP3.2.3 封装调用导出Excel

封装的步骤还是引入execl 对数据进行处理, l e t t e r 为 e x e c l 行 标 , letter为execl行标, letterexeclfield为数据库对应字段, p a r a m 就 为 我 们 在 文 件 中 显 示 的 行 栏 , param就为我们在文件中显示的行栏, paramlist为传输过来的数据

/**
* *
* 导出指定时间的已分派数据
* @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);returnhttp://.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});
}
}
});
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值