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

标签 php mysql symfony phpexcel 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();

这是我的查询,通过将变量 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;

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

我尝试将 $attendanceResult 的内容保存到另一个数组中,并使用该数组将数据插入到 Excel 中。那里也存在同样的问题。 请帮忙,因为我找不到代码有什么问题。在此先感谢

最佳答案

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

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

关于PHPExcel - Excel 报告生成期间的数据填充不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27509748/

相关文章:

php替换数组id键

mysql - 如何将一个字段的总和与另一个字段的总和进行比较?

symfony - 通过 AWS SDK 拒绝访问 SQS

php - 即使表单有错误,实体仍然存在

javascript - Symfony 搜索表单无需重新加载页面

php - 7.4.2 版本的 PHP cURL 扩展是否支持带有 HTTP/3 的 cURL

php - 避免使用面向对象设计的 if 语句,PHP

php - 根据php mysql中的值从多个值返回多个值

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

php - 试图让 php 理解以管理员身份登录