emma perkins
emma perkins

Reputation: 769

Using PHPExcel to convert more then 1 sheet from xls to csv

I am using the following code which converts the xls file to csv. This works for the first sheet but i have several that need doing. These sheets will always be the same name. Is there away to do all the sheets and not just do the first active one.

require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$excel = PHPExcel_IOFactory::load("all-euro-data-2015-2016.xls");
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(";");
$writer->setEnclosure("");
$writer->save("test123.csv");

Upvotes: 0

Views: 1109

Answers (2)

Evil_skunk
Evil_skunk

Reputation: 3382

At the moment you only write the active sheet. You have to iterate over the different sheets in the excel-file and write them to seperate csv files.

foreach ($excel->getWorksheetIterator() as $workSheetIndex => $worksheet) {
    $excel->setActiveSheetIndex($workSheetIndex);    
    $writer->setSheetIndex($index);
    $writer->save("test123_" . $worksheet->getTitle() . ".csv");    
}

Upvotes: 2

James
James

Reputation: 262

I am not familiar with PHPExcel, but the delimiter should be a comma not a semicolon for CSV files.

Upvotes: 0

Related Questions