20

我在 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;

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

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

4

3 回答 3

1

"The data is filtered properly and I got the filtered array,but the array changes when it gets exported to phpexcel –"

Don't assume it is properly filtered until you debug or var_dump it. Making assumptions will not help you or us, it is just impossible what you are saying. PS : This question has already been answered by the post owner.

于 2015-03-16T03:57:06.553 回答
1

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

您可以使用其最佳建议的替代方案 - Php Spreadsheet(Phpspreadsheet 文档

于 2019-08-20T07:50:01.900 回答
1
$phpExcelObject->getActiveSheet()->setCellValueExplicit('A'.$row_count,$row['Value'], PHPExcel_Cell_DataType::TYPE_STRING);

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

于 2017-12-13T10:32:40.037 回答