当前位置:首页 > PHP教程 >

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

发布时间:2019-10-26 13:34:51 作者:佚名 阅读:(260)

最近在使用PHPExcel生成报表文档,单个sheet的时候没有问题,在生成多个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错误,因为我是生成两个,所以在代码直接写了两个sheet,代码如下:

$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
    ->setLastModifiedBy("Maarten Balliauw")
    ->setTitle("Office 2007 XLSX Test Document")
    ->setSubject("Office 2007 XLSX Test Document")
    ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
    ->setKeywords("office 2007 openxml php")
    ->setCategory("Test result file");
// Add some data
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A1', '名称')
    ->setCellValue('B1', '资源')
    ->setCellValue('C1', '总量');

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
$x = 2;
$objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$x, '测试')
    ->setCellValue('B'.$x, '测试')
    ->setCellValue('C'.$x, '测试');

$objPHPExcel->getActiveSheet()->setTitle('Resource');
$objPHPExcel->setActiveSheetIndex(0);


重要的一句:$objPHPExcel->createSheet();如果你要循环生成很多个的话,可以把这一句放在循环的里面


欢迎分享转载→ PHPExcel生成多sheet报错You tried to set a sheet active by the out of bounds index: 1. The actual number of sheets is 1

© 2015-2021 - 吾爱编程网 版权所有 苏ICP备18033726号-1关于我们 - 网站声明 - 联系我们