PHP为每条记录添加总计

问题描述 投票:0回答:1

我正在尝试使用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

php codeigniter phpexcel
1个回答
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){

        }
}   
© www.soinside.com 2019 - 2024. All rights reserved.