PHPExcel duplicating protected worksheet?

Topics: User Forum
Jun 25, 2010 at 3:18 PM
Edited Jun 25, 2010 at 3:21 PM

Hello,

I have an Excel document (.xlsx) that has a single worksheet. When I open this worksheet with PHPExcel, the Excel object has two worksheets. 'Site Input', which is the original, and 'Site Input 1', which is created by PHPExcel. Any ideas why or how I can fix this?

require_once('./third_party/PHPExcel/Reader/Excel2007.php');
$excel_reader = new PHPExcel_Reader_Excel2007();
$excel_reader->setLoadSheetsOnly( array("Site Input") );
$excel = $excel_reader->load($full_filename);
Jun 29, 2010 at 11:22 AM

I was the person who posted the above issue. I wasn't able to register the other day. Is anyone there? Hello...

Aug 23, 2010 at 11:29 AM

It's a shame there's no support with this. I'll have to struggle on.

Coordinator
Aug 23, 2010 at 1:35 PM
charlesbeau wrote:

It's a shame there's no support with this. I'll have to struggle on.

 My apologies for having been distracted with trivia. However, I have been totally unable to replicate your problem.

$objPHPExcelReader = new PHPExcel_Reader_Excel2007();
$objPHPExcelReader->setLoadSheetsOnly( array("Site Input") );
$objPHPExcel = $objPHPExcelReader->load($inputFileName);
echo 'Sheet Count = '.$objPHPExcel->getSheetCount().'<br />';
echo 'Sheet Names =<br />';
$sheetNames = $objPHPExcel->getSheetNames();
foreach($sheetNames as $sheetName) {
   echo $sheetName.'<br />';
}
gives me a result of
Sheet Count = 1
Sheet Names =
Site Input

I've tested this now against all release versions of PHPExcel going back to 1.6.6 (as well as against the latest SVN code) and have completely failed to create a second worksheet that doesn't exist in the loaded workbook.

Note that, if your original workbook only contains a single sheet, then $objPHPExcelReader->setLoadSheetsOnly( array("Site Input") ); is unnecessary: it is only required if you have several worksheets in the workbook, but only want to load a selected subset of them.

Aug 25, 2010 at 3:02 PM

Hi Mark,

Thank you, thank you, thank you for your response!

I'm currently using version 1.7c, and I'll continue to use the latest and greatest.

I've been doing some more tests and the error is only occurring for me when I email the spreadsheet to my website, and not when I upload the file, so I'm going to assume that it's an error in my code. I'll update this when I've figured it out, to let you know either way.

Many thanks once again,

Charles

Aug 27, 2010 at 3:17 PM

There's something I don't like about this feature:

$objPHPExcelReader->setLoadSheetsOnly( array("Site Input") );
I have two sheets in my Excel document, 'Site Input' and 'Site Input 1'. 'Site Input 1' is a rogue sheet and I don't want to load it, but unfortunately the code above loads both.

I don't know if this is intended or a bug, but I'd prefer it to only load exact matches to the whole name.

Aug 27, 2010 at 3:56 PM

OK, I can now confirm that this is a bug. It occurs when loading a specific spreadsheet. I think it's something in the formatting that's causing the problem to occur. I've stripped my spreadsheet down to just coloured cells. If I remove any more rows or columns the error stops occurring. It's a really odd bug.

Here's the code I'm using the replicate the issue:

<?php
require_once('./PHPExcel/Reader/Excel2007.php');
$excel_reader_2007 = new PHPExcel_Reader_Excel2007();
$excel = $excel_reader_2007->load('Kings Place.xlsx');
echo count($excel->getSheetNames());
die();
?>
And you'll see the result as 2, instead of what it should be, which is 1. It's creating a duplicate sheet somehow.

Please download the test spreadsheet from here: http://www.mediafire.com/?5bsuldb8rut5mwe

I'll keep checking back here regularly. If you need anything else from me please let me know.

Aug 27, 2010 at 4:44 PM

 

hi,

can anyone say to me how can i add image to header?

 

regards.

Coordinator
Sep 1, 2010 at 1:55 PM

@charlesbeau

I've tried loading your worksheet, but still cannot replicate this error

Sep 23, 2010 at 1:45 PM

I had the same issue here.

http://phpexcel.codeplex.com/workitem/14261

I could not find another solution, but to check the sheet names. For the duplicated ones an extra " 1" or " 2" is added, so you may implement an extra check and ignore those.