PHP为每条记录添加总计

时间:2019-01-29 12:11:31

标签: php codeigniter phpexcel

我正在尝试使用PHP Excel创建一个Excel报告。报告的生成已完成。但是,我有一个问题,即将所有列都记起来并显示在每个记录名称的底部

我已经尝试生成报告,并且除了在唯一记录的每一端都没有生成总记录的问题之外,它还可以正常运行

这是我用于生成Excel文件的代码

  $ext = date('Y-m-d');


    $excel = PHPExcel_IOFactory::createReader('Excel2007');
    $excel = $excel->load(FCPATH.'reports/dtr/LeaveReports.xlsx');
    $excel->setActiveSheetIndex(0);
    $excel->getActiveSheet()->getPageSetup()->setFitToWidth(1);
    $excel->getActiveSheet()->getPageSetup()->setFitToHeight(0);
    $excel->getActiveSheet()->setCellValue('H1', '');
    $excel->getActiveSheet()->setCellValue('H2', '');
    $excelrow = 2;
    $getAllLeaves = $this->db->where('type', 'L')->where('date >=', $date_from)->where('date <=', $date_to)->order_by('employee_no, date')->get('employee_manual_dtr');
    // $data = $this->getPayrollDisbursement($id);
    // foreach ($data->result_array() as $row) {
    //  $excel->getActiveSheet()->setCellValue('A'.$excelrow, $row['employee_no']);
    //  $excel->getActiveSheet()->setCellValue('B'.$excelrow, $row['company_code']);
    //  $excel->getActiveSheet()->setCellValue('C'.$excelrow, $row['client']);
    //  $excel->getActiveSheet()->setCellValue('D'.$excelrow, $row['last_name']);
    //  $excel->getActiveSheet()->setCellValue('E'.$excelrow, $row['first_name']);
    //  $excel->getActiveSheet()->setCellValue('F'.$excelrow, $row['net_pay']);
    //  $excel->getActiveSheet()->setCellValue('G'.$excelrow, $row['disbursement']);
    //  $excel->getActiveSheet()->setCellValue('H'.$excelrow, $row['account_number']);
    //  $excelrow++;
    // }

    $emp = null;
    $total = 0;
    $total_sl = 0;
    $total_vl = 0;
    $total_co = 0;

    foreach ($getAllLeaves->result_array() as $value) {
        $sl = null;
        $vl = null;
        $co = null;

        if($emp != null){
            if($emp != $value['employee_no']){

                $excel->getActiveSheet()->setCellValue('A' . $excelrow, "");
                $excel->getActiveSheet()->setCellValue('B' . $excelrow, "Total");
                $excel->getActiveSheet()->setCellValue('C' . $excelrow, $total);
                $excel->getActiveSheet()->setCellValue('D' . $excelrow, $total_sl);
                $excel->getActiveSheet()->setCellValue('E' . $excelrow, $total_vl);
                $excel->getActiveSheet()->setCellValue('F' . $excelrow, $total_co);
                $total = 0;
                $total_sl = 0;
                $total_vl = 0;
                $total_co = 0;
            }
        }

        $leaveFile = $this->M_dtr->getEmployeeLeaveFile($value['date'], $value['employee_no']);
        if ($leaveFile->num_rows() > 0) {
            $leaveFile = $leaveFile->row();
            if ($leaveFile->code == 'SL') {
                $sl = 1;
                $total_sl += 1;
            } else if ($leaveFile->code == 'VL') {
                $vl = 1;
                $total_vl += 1;
            } else {
                $co = 1;
                $total_co += 1;
            }



        }
        $excel->getActiveSheet()->setCellValue('A' . $excelrow, $value['employee_no']);
        $excel->getActiveSheet()->setCellValue('B' . $excelrow, $this->M_global->getFullName($value['employee_no']));
        $excel->getActiveSheet()->setCellValue('C' . $excelrow, $value['date']);
        $excel->getActiveSheet()->setCellValue('D' . $excelrow, $total_sl);
        $excel->getActiveSheet()->setCellValue('E' . $excelrow, $vl);
        $excel->getActiveSheet()->setCellValue('F' . $excelrow, $co);
        $total +=1;
        $emp = $value['employee_no'];
        $excelrow++;

    }

    $filename='LeaveReports'.$ext.'.xlsx'; //save our workbook as this file name
    header('Content-Type: application/vnd.ms-excel '); //mime type
    header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
    header('Cache-Control: max-age=0'); //no cache

    //save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
    //if you want to save it as .XLSX Excel 2007 format
    $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
    $objWriter->setPreCalculateFormulas(true);
    //force user to download the Excel file without writing it to server's HD
    ob_end_clean();
    $objWriter->save('php://output');

我想在特定员工记录的每一端插入总计

enter image description here

1 个答案:

答案 0 :(得分:0)

作为快速解决方案,我建议先对数据进行排序。将所有数据存储到数组中并分别获取计数。 例如。

$USER_ARRAY=>
    [
        2013-001 => [
            'user_log' => [
                [
                    Fullname => 'FOO1',
                    DATE => '2019-01-04',
                    SL => '1',
                    VL => '2',
                    VL => ''
                ],
                [
                    Fullname => 'FOO1',
                    DATE => '2019-01-09',
                    SL => '1',
                    VL => '2',
                    VL => ''
                ]
            ],
            'total_per_user' => [
                    TOTAL_LEAVES => '2',
                    X
                    X
                    X
            ]
        ],
        2013-002 => [
            'user_log' => [
                [
                    Fullname => 'FOO2',
                    DATE => '2019-01-14',
                    SL => '1',
                    VL => '2',
                    VL => ''
                ]
            ],
            'total_per_user' => [
                    TOTAL_LEAVES => '1',
                    X
                    X
                    X
            ]
        ]
    ]

完成隔离后,开始编写Excel

Foreach($USER_ARRAY as $user_id => $user_tally){

        $col;
        $row;
        foreach($user_tally['user_log'] as $fields){

        }

        foreach($user_tally['total_per_user'] as $fields){

        }
}