简体   繁体   English

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

[英]PhpExcel export data from a website to CSV file

I want to export data from orangehrm attendance report to csv file by using phpexcel but I don't know how to do it: 我想使用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');



    }

csvYou didn't mention the structure of your array ($records).. Hope it's like csv您没有提到数组的结构($ records)。希望它像

$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');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM