How to export multiple worksheets to individual excel files phpexcel

Topics: Developer Forum
Nov 10, 2012 at 11:17 PM

Am trying to export an excel file with 5 worksheets in it.

I would like to export each worksheet as an individual excel file. I have tried using the getWorksheetIterator but no success and it throws this error.

Catchable fatal error: Object of class PHPExcel_Worksheet could not be converted to string in

<!--?php //session_start(); ?-->
<!--?php
 set_time_limit(0);
require_once '../Classes/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader--->setReadDataOnly(false);
//$xlsxfiles=$_SESSION['file'];
//echo $xlsxfiles;
$objPHPExcel = $objReader->load('../upload/Demobook.xlsx');
$num=$objPHPExcel->getSheetCount() ;
$sheetnames=$objPHPExcel->getSheetNames() ;
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
$objWriter->setDelimiter(',');
$objWriter->setEnclosure('');
$objWriter->setLineEnding("\r\n");
$objReader = new PHPExcel_Reader_CSV();
 foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {

    $worksheet->getTitle();



   }
      $objWriter->save("../"."CSV"."/".$worksheet.".".'csv');




//header("Location: column_sort.php");
//exit;





?>

Nov 11, 2012 at 9:27 PM

you are trying to use a worksheet object as a string, which doesn't work. How about using the title?

eg.

$title = $worksheet->getTitle();

and use $title in the filename