国内外VPS
主机测评与优惠

PHPExcel循环生成sheet报错You tried to set a sheet active by the out of bounds index: 1. The actual number of sheets is 1

PHPExcel是一款非常强大的PHP操作EXCEL库,使用PHPExcel可以帮助我们简单、高效实现从Excel读取Excel的数据和导出数据到Excel。

最近在使用PHPExcel循环生成多个sheet时,遇到You tried to set a sheet active by the out of bounds index: 1. The actual number of sheets is 1错误,特将解决办法记录如下:

产生这个错误的原因是PHPExcel会自动创建第一个sheet,因此我们可以直接创建一个PHPEXCEL对象并且操作第一个sheet:

  $excel = new PHPExcel();  $excel->setactivesheetindex(0);

因此我最开始的循环代码就是直接用变量循环:

  $excel = new PHPExcel();  for($i=0;$i<5;$i++){  $excel->setactivesheetindex($i);  }

这样就会报上面的错误,因为之后的sheet无法自动创建,而需要我们通过代码创建,因此我们要判断循环变量是否大于0,如果大于0那么要先createSheet():

  $excel = new PHPExcel();  for($i=0;$i<5;$i++){  if($i>0){  $excel->createSheet();  }  $excel->setactivesheetindex($i);  }

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《PHPExcel循环生成sheet报错You tried to set a sheet active by the out of bounds index: 1. The actual number of sheets is 1》
文章链接:https://www.liuzhanwu.com/3025.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

登录

找回密码

注册