Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PhpExcel将数据从网站导出到CSV文件_Php_Phpexcel_Orangehrm - Fatal编程技术网

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

PhpExcel将数据从网站导出到CSV文件,php,phpexcel,orangehrm,Php,Phpexcel,Orangehrm,我想使用phpexcel将orangehrm考勤报告中的数据导出到csv文件,但我不知道如何操作: $records = array(); foreach ($empRecords as $employee) { $hasRecords = false; $attendanceRecords = $employee->getAttendanceRecord();

我想使用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');



    }
$records=array();
foreach($emp记录为$employee){
$hasRecords=false;
$attendanceRecords=$employee->getAttendanceRecord();
$total=0;
foreach($attendanceRecords as$attendance){
$from=$this->date.“.00:”.00:“.00”;
$end=$this->date2.“.23:”.59:“.59”;
如果(strotime($attention->getPunchInUserTime())>=strotime($from)&&strotime($attention->getPunchInUserTime())getpunchoutctime()){
$total=$total+round((strotime($attention->getpunchoutctime())-strotime($attention->getpunchoutctime())/3600,2);
}
$records[]=$Attention;
$hasRecords=true;
}
}
如果($记录){
$last=结束($records);
$last->setTotal($total);
}否则{
$Attention=新的AttendanceRecord();
$attention->setEmployee($employee);
$Attention->setTotal('--');
$records[]=$Attention;
}
}
//导出过滤/搜索数据的算法
如果($post['export']='1'){
//需要一次“PHPExcel/Reader/Excel15.php”;
//需要一次“PHPExcel/Reader/Excel2007.php”;
需要一次“PHPExcel/IOFactory.php”;
需要一次'PHPExcel.php';
$objPHPExcel=new PHPExcel();
$objActSheet=$objPHPExcel->getActiveSheet();
$objActSheet->setTitle('Staff AttendanceRecord');
$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('attention.xslx');
}

csv您没有提到数组的结构($records)。。 希望是这样

$records[] = array('EMPLOYEE'=>'name1','TOTAL'=>'total1'),array('EMPLOYEE'=>'name2','TOTAL'=>'total2');
请尝试下面的代码

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');
    }
}
if($post['export']=='1')
{
如果(!空($records))
{
需要一次“PHPExcel/IOFactory.php”;
需要一次'PHPExcel.php';
$objPHPExcel=new PHPExcel();//创建新的PHPExcel对象
$column=A;
$headers=数组('EMPLOYEE','TOTAL');
对于($c=0;$cgetActiveSheet()->setCellValue($column.1',$headings[$c]);//添加列标题数据
如果($c==计数($headers)-1)
{
break;//当列字母达到最大值时需要终止循环
}
$column++;
}
while(列表($key,$value)=每个($records))
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$j,$value['EMPLOYEE']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$j,$value['TOTAL']);
$j++;
}
$objActSheet->setTitle('Staff AttendanceRecord');
$workbookName='Attention';
//将输出重定向到客户端的web浏览器(Excel5)
标题('Content-Type:application/vnd.ms-excel');
标题('Content-Disposition:attachment;filename=“”。$workbookName..csv“);
标头('Cache-Control:max age=0');
$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');
$objWriter->save('php://output');
}
}

请详细说明您的问题,并向我们展示您到目前为止尝试了什么。我只是编辑了我的问题,我坚持了如何从记录中获取数据。