PhpExcel将数据从网站导出到CSV文件

时间:2015-06-26 08:35:53

标签: php phpexcel orangehrm

我想使用phpexcel将orangehrm考勤报告中的数据导出到csv文件,但我不知道该怎么做:

$records = array();
                foreach ($empRecords as $employee) {
                    $hasRecords = false;

                    $attendanceRecords = $employee->getAttendanceRecord();
                    $total = 0;
                    foreach ($attendanceRecords as $attendance) {
                        $from = $this->date . " " . "00:" . "00:" . "00";
                        $end = $this->date2 . " " . "23:" . "59:" . "59";
                        if (strtotime($attendance->getPunchInUserTime()) >= strtotime($from) && strtotime($attendance->getPunchInUserTime()) <= strtotime($end)) {
                            if ($attendance->getPunchOutUtcTime()) {
                                $total = $total + round((strtotime($attendance->getPunchOutUtcTime()) - strtotime($attendance->getPunchInUtcTime())) / 3600, 2);
                            }
                            $records[] = $attendance;
                            $hasRecords = true;
                        }
                    }

                    if ($hasRecords) {
                        $last = end($records);
                        $last->setTotal($total);
                    } else {
                        $attendance = new AttendanceRecord();
                        $attendance->setEmployee($employee);
                        $attendance->setTotal('---');
                        $records[] = $attendance;
                    }
                }
                    // Algorithm to export filtered/ searched data
                    if($post['export'] == '1'){

                    //require_once 'PHPExcel/Reader/Excel15.php';
                    //require_once 'PHPExcel/Reader/Excel2007.php';

                    require_once 'PHPExcel/IOFactory.php';
                    require_once 'PHPExcel.php';
                    $objPHPExcel = new PHPExcel();
                    $objActSheet = $objPHPExcel->getActiveSheet();
                    $objActSheet->setTitle('Staff AttendanceRecord');


                    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
                    $objWriter->save('Attendance.xslx');



    }

1 个答案:

答案 0 :(得分:0)

csvYou didn't mention the structure of your array ($records).. Hope it's like

$records[] = array('EMPLOYEE'=>'name1','TOTAL'=>'total1'),array('EMPLOYEE'=>'name2','TOTAL'=>'total2');

Please try code below

if($post['export'] == '1')
{
    if(!empty($records))
    {
        require_once 'PHPExcel/IOFactory.php';
        require_once 'PHPExcel.php';
        $objPHPExcel = new PHPExcel(); // Create new PHPExcel object
        $column = A; 
        $headings=array('EMPLOYEE','TOTAL');
        for($c=0;$c<count($headings);$c++)
        {
            $objPHPExcel->getActiveSheet()->setCellValue($column.'1',$headings[$c]); // Add column heading data
            if($c==count($headings)-1)
            {
                break;// Need to terminate the loop when coumn letter reachs max
            }
            $column++;
        }
        while (list($key,$value) = each($records))
        {
            $objPHPExcel->getActiveSheet()->setCellValue('A'.$j,$value['EMPLOYEE']);
            $objPHPExcel->getActiveSheet()->setCellValue('B'.$j,$value['TOTAL']);
            $j++;
        }
        $objActSheet->setTitle('Staff AttendanceRecord');
        $workbookName = 'Attendance';
        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$workbookName.'.csv"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }
}