PHP Excel多页导出多次导出||致命错误:未捕获的异常'PHPExcel_Exception'

时间:2021-06-25 23:18:12

I am using PHP Excel, for exporting datas in 2 sheets. Each worksheet should have different structure and different. Here I have the code , that I try to use to generate 2 sheets with different names but same datas and structure. Here is the code that I use:

我正在使用PHP Excel,用于导出2张数据。每个工作表应该有不同的结构和不同。在这里,我有代码,我尝试使用不同的名称,但相同的数据和结构生成2张表。这是我使用的代码:

 <?php
    include '...config.php'; 

    /** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';


// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// FIRST SHEET BEGIN
// Rename worksheet
$objPHPExcel->getActiveSheet(0)->setTitle('FIRST SHEET');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
//$objPHPExcel->setActiveSheetIndex();
// Set document properties
$objPHPExcel->getProperties()->setCreator("test.de")
                             ->setLastModifiedBy("test.de")
                             ->setTitle("Study XLS generated from test.de")
                             ->setSubject("Study details")
                             ->setDescription("This file is automatically generated from test.de")
                             ->setKeywords("Office 2007 test.de")
                             ->setCategory("Studies from test.de");

$query = "SELECT * FROM admin_users  ";
          $result = mysql_query($query);


// Loop through the result set
$rowNumber = 2;

while ($row = mysql_fetch_row($result)) {

    $col = 'A'; //start from column
    $ime = $row[3]; //this is data from mysql
    $jmbg = $row[4]; //this is data from mysql

     $objPHPExcel->setActiveSheetIndex(0)
           ->setCellValue('A1', 'Username')
             ->setCellValue('B1', 'Pass');
    $rows = array($ime,$jmbg);
        foreach($rows as $cell) {
            $objPHPExcel->getActiveSheet(0)->setCellValue($col.$rowNumber,$cell);

                $col++;
        }
$rowNumber++;

    }

//FIRST SHEET ENDS

// SECOND SHEET BEGIN
// Rename worksheet
$objPHPExcel->getActiveSheet(1)->setTitle('SECOND SHEET');


$query1 = "SELECT * FROM admin_users  ";
          $result1 = mysql_query($query1);


// Loop through the result set
$rowNumber1 = 2;

while ($row1 = mysql_fetch_row($result1)) {

    $col1 = 'A'; //start from column
    $ime1 = $row[3]; //this is data from mysql
    $jmbg1 = $row[4]; //this is data from mysql
    //$grad = 'anything else'; //example my excel formulas
     $objPHPExcel->setActiveSheetIndex(1)
           ->setCellValue('A1', 'Username')
             ->setCellValue('B1', 'Pass');
    $rows1 = array($ime1,$jmbg1);
        foreach($rows1 as $cell1) {
            $objPHPExcel->getActiveSheet(1)->setCellValue($col1.$rowNumber1,$cell1);

                $col1++;
        }
$rowNumber1++;

    }

//SECOND SHEET ENDS


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="studies.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 2015 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

What I receive is:

我收到的是:

Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'You tried to set a sheet active by the out of bounds index: 1. The actual number of sheets is 1.' in /var/www/test/Classes/PHPExcel.php:688 Stack trace: #0 /var/www/test/Examples/01simple-download-xls.php(86): PHPExcel->setActiveSheetIndex(1) #1 {main} thrown in /var/www/test/Classes/PHPExcel.php on line 688 

When I run just the first sheet it works fine. How can I fix this? Thanks!

当我只运行第一张表时,它工作正常。我怎样才能解决这个问题?谢谢!

1 个解决方案

#1


Instantiating a new PHPExcel object using

使用实例化新的PHPExcel对象

$objPHPExcel = new PHPExcel();

only creates a PHPExcel object with a single worksheet (sheet #0).

仅使用单个工作表创建PHPExcel对象(表#0)。

You're subsequently telling it to use a second sheet (sheet #1) when none yet exists:

你后来告诉它使用第二张(表#1),当时还没有:

$objPHPExcel->getActiveSheet(1)->setTitle('SECOND SHEET');

Before doing that, you need to create a second worksheet in the PHPExcel object:

在此之前,您需要在PHPExcel对象中创建第二个工作表:

$objPHPExcel->createSheet();

#1


Instantiating a new PHPExcel object using

使用实例化新的PHPExcel对象

$objPHPExcel = new PHPExcel();

only creates a PHPExcel object with a single worksheet (sheet #0).

仅使用单个工作表创建PHPExcel对象(表#0)。

You're subsequently telling it to use a second sheet (sheet #1) when none yet exists:

你后来告诉它使用第二张(表#1),当时还没有:

$objPHPExcel->getActiveSheet(1)->setTitle('SECOND SHEET');

Before doing that, you need to create a second worksheet in the PHPExcel object:

在此之前,您需要在PHPExcel对象中创建第二个工作表:

$objPHPExcel->createSheet();