duanpanhuo0618 2017-11-06 05:09
浏览 48

应用if-else条件使用PhpExcel导出选择性记录

I need to export only those records whose column satisfies defined value. Eg.

if(null !== ($this->f3->get('SESSION.userStatus')))
    {
        $userStatus =  $this->f3->get('SESSION.userStatus');
    }

This is how I tried to set cell values depending on set value:

$rowID = 5;
foreach ($results as $result) 
   {
     if($result['status'] == $userStatus)
     {
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$rowID, $result['fullname']);
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$rowID, $result['checkin_date']);
     }
   else
     {
        $objPHPExcel->getActiveSheet()->removeRow($rowID);
     }
     $rowID++;
   }

The file is exported with only those records that have defined "status" value. But the problem is that rows whose criteria doesn't meet are filled with spaces and still occupies rows. Here is how output looks like:

       A   |  B   |  C  |  D
  1        |      |     |
  .
  .
  174      |      |     |
  175      |      |     |
  176      |      |     |
  177      |      |     |
  178 John |  2014| xyz | dfdf
  179 Jack |  2015| jkl | dfdf
  180      |      |     |
  .
  .

How can I fix it to get records starting with top row? Eg.

1 | John   |   2014 |   xyz  | dfdf
2 | Jack   |   2015 |   jkl  | dfds
  • 写回答

1条回答 默认 最新

  • douhong1703 2017-11-06 05:19
    关注

    between } $rowID++; put this else { $objPHPExcel->getActiveSheet()->removeRow($rowID); }

    That will delete your blank rows. Note, that you start with row 5 for some reason and do not test for max rows. Your code will still need some fixing up

    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: