Removing a worksheet 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()
+4
source share
1 answer

Potential issue # 1

Use a different variable name for Reader and for the object you are loading from Reader

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

Potential issue # 2

It is always wise to let PHPExcel identify the file type for you, rather than trust the file extension

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

Documentation

+4
source

All Articles