msd_2
msd_2

Reputation: 1187

Removing sheet from Excel 2005 using PHP

I want to delete sheets from an Excel2005/Excel5 file using PHP. I am using PHPExcel-1.7.9. I am using the following code to delete Excel2007 files as follows

    $exceltype="Excel2007"; 
    $excel = PHPExcel_IOFactory::createReader($exceltype);
    $excel = $excel->load("ABC.xlsx");

    $count = $excel->getSheetCount();
    for($i = 0; $i < $count; $i++)
    {
        $excel->removeSheetByIndex(0);
    }

When I use it for Excel5, I get an error

Fatal error: Call to undefined method PHPExcel_Reader_Excel5::getSheetCount()

Upvotes: 4

Views: 4814

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

Potential Issue #1

Use a different variable name for the Reader and for the object that you're loading from the Reader

$exceltype="Excel2007"; 
$excelReader = PHPExcel_IOFactory::createReader($exceltype);
$excel = $excelReader->load("ABC.xlsx");

Potential Issue #2

It's always sensible to let PHPExcel identify the filetype for you rather than trusting to the file extension

$excel = PHPExcel_IOFactory::load("ABC.xlsx");

Documentation

Upvotes: 4

Related Questions