PHPExcel - 生成Excel报告期间的数据填充不正确

Bas*_*r51 21 php mysql phpexcel symfony phpexcelreader

我正在研究Symfony 2.1中的一个项目.PHPExcel版本是1.8.0.我使用Doctrine从Db中检索数据并根据需要进行过滤.

    $em = $this->getDoctrine()->getManager();
    $guardquery = $em->createQueryBuilder()
            ->select('g.attendancePopupTime', 'd.atmName', 'd.region', 'd.zone', 'd.state')
            ->from('ATMMonitorAPIBundle:GuardMonitor', 'g')
            ->innerJoin('ATMMonitorAPIBundle:DeviceAtmInfo', 'd', Join::WITH, 'd.deviceId = g.deviceId');

    if ($userZones[0]['userZones'] != '0') {
        $guardquery->innerJoin('ATMMonitorAPIBundle:RegisteredDevices', 'r', Join::WITH, 'r.deviceId = g.deviceId')
                ->where('r.deviceZone IN (:devicezone)')
                ->setParameter('devicezone', $zone_array);
    }

    if (isset($dateLow)) {
        $guardquery->andWhere('g.attendancePopupTime BETWEEN :date_low and :date_high')
                ->setParameter('date_low', $dateLow)
                ->setParameter('date_high', $dateHigh);
    }

    $finalAttendanceQuery = $guardquery->getQuery();
    $attendanceResult = $finalAttendanceQuery->getArrayResult();
Run Code Online (Sandbox Code Playgroud)

这是我的查询,并将变量作为2014-12-1作为$ dateLow和2014-12-8作为$ dateHigh,查询返回122行.数据库中有579行.过滤后返回的数据是正确的,我可以使用以下代码将其插入Excel.

    $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
      $phpExcelObject->getProperties()->setCreator("")
      ->setLastModifiedBy("Administrator")
      ->setTitle("ATTENDANCE DETAILS XLSX")
      ->setSubject("ATTENDANCE DETAILS  XLSX")
      ->setDescription("EXCEL document for Attendance Details");
      $phpExcelObject->setActiveSheetIndex(0);
      $phpExcelObject->getActiveSheet()->setTitle('GUARD_ATTENDANCE - DETAILS');
      $phpExcelObject->getActiveSheet()
      ->SetCellValue('A3', "STATE")
      ->SetCellValue('B3', "ZONE")
      ->SetCellValue('C3', "REGION")
     ->SetCellValue('D3', "DATE")

     ->SetCellValue('A1', "GUARD ATTENDANCE RECORDS");
    $count = count($attendanceResult);
    $rowCount = 4;

    for ($i = 0; $i < $count; $i++) {
     $phpExcelObject->getActiveSheet()->SetCellValue('A' . $rowCount, $attendanceResult[$i]['state']);
     $phpExcelObject->getActiveSheet()->SetCellValue('B' . $rowCount, $attendanceResult[$i]['zone']);
     $phpExcelObject->getActiveSheet()->SetCellValue('C' . $rowCount, $attendanceResult[$i]['region']);
     if ($attendanceResult[$i]['attendancePopupTime'] instanceof \DateTime) {
           $attendanceDate = $attendanceResult[$i]['attendancePopupTime']->format('d-m-Y');
        }
     $phpExcelObject->getActiveSheet()->SetCellValue('D' . $rowCount, $punchTime);                 
     $phpExcelObject->getActiveSheet()->SetCellValue('E' . $rowCount, count($attendanceResult));
     $rowCount++
    }

    $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');
    $response = $this->get('phpexcel')->createStreamedResponse($writer); 

    $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
    $response->headers->set('Content-Disposition', 'attachment;filename=AttendanceDetails.xls'); 

    $response->headers->set('Pragma', 'public');
    $response->headers->set('Cache-Control', 'maxage=1');
    return $response;
Run Code Online (Sandbox Code Playgroud)

在进入for循环之前,变量$ count的值为122.在生成的excel中,有579行(DB中可用的整个数据)而不是过滤后获得的122行.excel的列E也显示值579而不是122. for循环也执行579次而不是122次.一些如何,数组$ attendanceResult在phpExcel中插入数据时会发生变化.

我尝试将$ attendanceResult的内容保存到另一个数组中,并使用该数组将数据插入到excel中.那里也存在同样的问题.请帮忙,因为我发现代码没有任何问题.谢谢你提前

Naw*_*rar 1

“数据被正确过滤,我得到了过滤后的数组,但是当它导出到 phpexcel \xe2\x80\x93 时数组发生了变化”

\n\n

在调试或 var_dump 之前,不要假设它已被正确过滤。做出假设对你或我们都没有帮助,你所说的根本不可能。\nPS:这个问题已经被楼主回答了。

\n