setActiveSheetIndex not wroking

Topics: Developer Forum
May 5, 2011 at 11:32 AM

Hi all,

I can't get setActiveSheetIndex funcion to work. I don't know what I am doing wrong.

I have a Excel2003 document file. As you can see, it has three sheets, and its titles' name are:

 

  1. Sludge features
  2. Soil features
  3. Legends

 

I want to change sheet index; and read only the sheet I want to.

The next code; is working well for getting first sheet's name:

 

        $objReader = new PHPExcel_Reader_Excel5();       
       
        /** Provem d'obrir el fitxer*/
        $objPHPExcel = $objReader->load('dadesPrimitives.xls');
      
        $title =  $objPHPExcel->getActiveSheet()->getTitle();


I get 'Sludge features' on $title variable.

I try now to change ActiveSheetIndex and get sheet's name:

        $objReader = new PHPExcel_Reader_Excel5();       

/** Provem d'obrir el fitxer*/
$objPHPExcel = $objReader->load('dadesPrimitives.xls');

$objPHPExcel->setActiveSheetIndex(2);

$title = $objPHPExcel->getActiveSheet()->getTitle();

I get 'Sludge features' on $title variable again :-( . It should be set to 'Legends'.

Anyone could help me ?

I am running:

  • PHP 5.3.4 
  • Apache 2.2.16
  • PHPExcel 1.7.6
  • Windows XP SP3

Thank you very much !!





 

 

 

May 5, 2011 at 1:48 PM
Edited May 5, 2011 at 2:02 PM

If you just need the sheet names, you could just read them to an array with code like this:

$sheets = $objReader->listWorksheetNames($file); 

setActiveSheetIndex works for me otherwise, so it may be how you get $title that's the issue, but I don't get my titles this way so can't say.

Coordinator
May 6, 2011 at 10:32 PM

I'm not completely clear why you're reading the same file twice here, but I can't replicate your problem.

May 10, 2011 at 9:08 AM

Hi all,

Thank you for your answers, but I'm afraid i've been misunderstood.

 

  • MarkBaker, I am not trying to read same file twice: I just wanted to show two different programming codes; but the last one, is not working for me.
  • ScottMSanders, I don't just want the sheet names; i need the information from the cells. But I want to read it from sheet1, or sheet2, etc. If I read the file as shown in PHPExcel main examples, I get the information from sheet1....then after a set of NULLs; i start reading information from sheet2, and so on. I want to read information from sheet2; without having to iterate all the rows from sheet1... is it possible?

 

Thank you again!

 

 

 

 

Coordinator
May 10, 2011 at 9:23 PM

I'm not able to recreate any problem with the setActiveSheetIndex()

The only thing that I can think of:
Are you using the setLoadSheetsOnly() method?

May 11, 2011 at 9:17 AM
Edited May 11, 2011 at 9:27 AM
MarkBaker wrote:

I'm not able to recreate any problem with the setActiveSheetIndex()

The only thing that I can think of:
Are you using the setLoadSheetsOnly() method?

  • Are you using the same Excel file that I link in my post?
  • No, i'm not using setLoadSheetOnly() method. The only code I execute is
  • It's quite strange. setActiveSheetIndex seems to work; because if I try setActiveSheetIndex(3); it throws an index out of bounds error (my Excel file has only 3 sheets; so that is working OK).

 

Is there any debug information where I could look at to get more clues about what's going wrong?

 

By the way, I'm posting all my code where I'm using PHPExcel; so let's see if that cold help. You can assume that Excel file's name exists in $params[0]; and it is loaded properly:

 

	public function llegirExcel($params)
{
require_once('FirePHPCore/FirePHP.class.php');
$firephp = FirePHP::getInstance(true);

/** Error reporting */
error_reporting(E_ALL);

date_default_timezone_set('Europe/Madrid');

/** PHPExcel_IOFactory */
require_once 'Classes/PHPExcel/IOFactory.php';

$idProject = putProjecte(array($params[2],$params[3],$params[1],$params[0]));

$firephp->log($idProject,"id del project");

/** El tipus de fitxer del Excel 2003 es Excel 5*/
$objReader = PHPExcel_IOFactory::createReader('Excel5');

/** Provem d'obrir el fitxer*/
$objPHPExcel = $objReader->load($params[0]);

$objPHPExcel->setActiveSheetIndex(2);

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {

$resultat = array();
foreach ($worksheet->getRowIterator() as $row) {
/** echo ' - Row number: ' . $row->getRowIndex() . "\r\n"; */
/** Quan hem llegit dos files, sortim*/
if ($row->getRowIndex()==2) break;

$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
$fila_actual = array();
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
/** Per cada cel-la, guardar la informaci� */
array_push($fila_actual,$cell->getCalculatedValue());
}
}
/** Afegim la fila al resultat final*/
array_push($resultat,$fila_actual);
}
}
}

 

 

PD: Hum...I realise I'm using PHPFireBug....could it be a problem?

Thank you again !!

Jan 23, 2014 at 4:00 AM
Hi omiras,

setActiveSheetIndex() seems work with me. In my situdation, I create an excel file with 2 sheets, and I wrote data in the first sheet with
$objWorkSheet = $objPHPExcel->setActiveSheetIndex(0);
$objWorkSheet->setCellValue("A1", "Hello");
After that, I started to change to the later sheet and remember you should use the other variable for the second sheet like:
$objWorkSheet_2 = $objPHPExcel->setActiveSheetIndex(0);
$objWorjSheet_2->setCellValue("A1", "Hello");
With 2 different variables you can use those to execute your writing data in PHPExcel