简体   繁体   中英

Export worksheets in a workbook to csv in phpexcel

I am trying to export worksheets in an Excel 2007 workbook to csv and save the csv as the worksheet name. I keep getting this error:

"Object of class PHPExcel_Worksheet could not be converted to string in"

Here is my code:

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);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
    $worksheet->getTitle();
    $objWriter->setDelimiter(',');
    $objWriter->setEnclosure('');
    $objWriter->setLineEnding("\r\n");
}
$objWriter->save("../"."CSV"."/".$worksheet.".".'csv');

Your problem is

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

You're treating a worksheet object as a string, when it's not a string but a worksheet object. Did you mean

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

Why are you looping through the all worksheets setting the writer delimiter, enclosure, etc; but executing the save outside of the loop? That's a bit pointless really. The call to $worksheet->getTitle(); in your loop doesn't do very much either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM