ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP 使用 PHPExcel导出excel多个sheet

2020-10-29 15:32:58  阅读:45  来源: 互联网

标签:sheet pro excel obpe PHPExcel setActiveSheetIndex setCellValue getActiveSheet ro


<?php

function export_order(){
    //设置内存
    ini_set("memory_limit", "2048M");
    set_time_limit(0);
    include_once('includes/PHPExcel.php');
    include_once('includes/PHPExcel/IOFactory.php');
    /* @实例化 */
    $obpe_pro = new PHPExcel();
    
    $sql = "select  * from  ecs_tfx_order_info ";
    $res = $GLOBALS['db']->getAll($sql);
    if(empty($res)){
        sys_msg('请选择需要导出的数据', 1, array(), false);
    }
    /* 第一个sheet */
    $obpe_pro->setActiveSheetIndex(0);
    //给Sheet设置名字
    $obpe_pro->getActiveSheet()->setTitle("表头");
    //设置表格头的输出
    $obpe_pro->setActiveSheetIndex()->setCellValue('A1', '订单号');
    $obpe_pro->setActiveSheetIndex()->setCellValue('B1', '店铺编码');
    $obpe_pro->setActiveSheetIndex()->setCellValue('C1', '订单时间');
    $obpe_pro->setActiveSheetIndex()->setCellValue('D1', '付款时间');
    $obpe_pro->setActiveSheetIndex()->setCellValue('E1', '总金额');
    $obpe_pro->setActiveSheetIndex()->setCellValue('F1', '优惠金额');
    $obpe_pro->setActiveSheetIndex()->setCellValue('G1', '运费');
    $obpe_pro->setActiveSheetIndex()->setCellValue('H1', '保费');
    $obpe_pro->setActiveSheetIndex()->setCellValue('I1', '税费');
    $obpe_pro->setActiveSheetIndex()->setCellValue('J1', '收货人');
    $obpe_pro->setActiveSheetIndex()->setCellValue('K1', '收货地址');
    $obpe_pro->setActiveSheetIndex()->setCellValue('L1', '收货人手机');
    $n = 2;
    foreach ($res as $row){
        $region = handleAddress($row['address']);
        $obpe_pro->getActiveSheet()->setCellValue('A'.($n) ,$row['pay_note']);
        $obpe_pro->getActiveSheet()->setCellValue('B'.($n) ,$row['store_num']);
        $obpe_pro->getActiveSheet()->setCellValue('C'.($n) ,$row['add_time']);
        $obpe_pro->getActiveSheet()->setCellValue('D'.($n) ,$row['pay_time']);
        $obpe_pro->getActiveSheet()->setCellValue('E'.($n) ,$row['order_amount']);
        $obpe_pro->getActiveSheet()->setCellValue('F'.($n) ,0);
        $obpe_pro->getActiveSheet()->setCellValue('G'.($n) ,0);
        $obpe_pro->getActiveSheet()->setCellValue('H'.($n) ,0);
        $obpe_pro->getActiveSheet()->setCellValue('I'.($n) ,0);
        $obpe_pro->getActiveSheet()->setCellValue('J'.($n) ,$row['consignee']);
        $obpe_pro->getActiveSheet()->setCellValue('K'.($n) ,$row['address']);
        $obpe_pro->getActiveSheet()->setCellValue('L'.($n) ,$row['tel']);
        $n = $n +1;
        $ids[] = $row['order_sn'];
    }
    
    $ids_str = implode(',',$ids);
    $sql = "select o.pay_note,g.* from ecs_tfx_order_info as o left join ecs_tfx_order_goods as g on g.order_sn = o.order_sn where o.order_sn in ('$ids_str')";
    $goods_list = $GLOBALS['db']->getAll($sql);
    $i = 2;
    
    /* 第二个sheet */
    $obpe_pro->createSheet();
    $obpe_pro->setActiveSheetIndex(1);
    //给Sheet设置名字
    $obpe_pro->getActiveSheet()->setTitle("表体");
    $obpe_pro->setActiveSheetIndex(1)->setCellValue('A1', '订单号');
    $obpe_pro->setActiveSheetIndex(1)->setCellValue('B1', '商品序号');
    $obpe_pro->setActiveSheetIndex(1)->setCellValue('C1', '商品编码');
    $obpe_pro->setActiveSheetIndex(1)->setCellValue('D1', '商品单价');
    $obpe_pro->setActiveSheetIndex(1)->setCellValue('E1', '商品数量');
    $obpe_pro->setActiveSheetIndex(1)->setCellValue('F1', '客户批次号');
    foreach ($goods_list as $k=>$v){
        $num = $k +1;
        $obpe_pro->getActiveSheet()->setCellValue('A'.($i) ,$v['pay_note']);
        $obpe_pro->getActiveSheet()->setCellValue('B'.($i) ,$num);
        $obpe_pro->getActiveSheet()->setCellValue('C'.($i) ,$v['goods_sn']);
        $obpe_pro->getActiveSheet()->setCellValue('D'.($i) ,$v['total_price']);
        $obpe_pro->getActiveSheet()->setCellValue('E'.($i) ,$v['goods_number']);
        $obpe_pro->getActiveSheet()->setCellValue('F'.($i) ,'');
        $i = $i +1;
    }

    ob_end_clean();
    ob_start();
    header('Content-Type : application/vnd.ms-excel');
    //设置输出文件名及格式
    header('Content-Disposition:attachment;filename="订单表'.'.xls"');
    //导出.xls格式的话使用Excel5,若是想导出.xlsx需要使用Excel2007
    $objWriter= PHPExcel_IOFactory::createWriter($obpe_pro,'Excel5');
    $objWriter->save('php://output');
    ob_end_flush();
}

 

标签:sheet,pro,excel,obpe,PHPExcel,setActiveSheetIndex,setCellValue,getActiveSheet,ro
来源: https://www.cnblogs.com/Jessie-candy/p/13897000.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有