【问题标题】:PHPExcel - Incorrect data population during excel report generationPHPExcel - 生成 Excel 报告期间的数据填充不正确
【发布时间】:2015-02-15 01:35:23
【问题描述】:

我在 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 中。那里也存在同样的问题。 请帮忙,因为我找不到代码有什么问题。在此先感谢

【问题讨论】:

  • 尝试做一个 foreach($attendanceResult as $row) 也许。
  • @Belac:已经做到了。循环仍然执行了 579 次。
  • 奇数。您确定查询是根据日期排除行吗?你可以看看stackoverflow.com/questions/11553183/…
  • @Belac:数据被正确过滤,我得到了过滤后的数组,但是当它被导出到 phpexcel 时,数组发生了变化
  • 删除或回答你自己的问题不会是坏事。此话题仍显示在“未答复”列表中。

标签: php mysql symfony phpexcel phpexcelreader


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    $phpExcelObject->getActiveSheet()->setCellValueExplicit('A'.$row_count,$row['Value'], PHPExcel_Cell_DataType::TYPE_STRING);
    

    尝试上面的代码来错误的人口行,并将您的 excel 格式更改为 .xlsx。你可以使用 phpExcel 来做到这一点。

    【讨论】:

      【解决方案3】:

      对于您和那些正在寻找 PHP 中的 Excel 导出实现的人

      您可以使用其推荐的最佳替代方案 - PHP 电子表格 (Phpspreadsheet documentation)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多