I tried to save a specific worksheet from an XLSX as CSV.
First I tried to use setActiveSheetIndexByName()
function to set active sheet and call the writer method of PhpOfficePhpSpreadsheetWriterCsv
, but no luck. Somehow the writer save the first worksheet. I tried indexing by name, by number, but not a little difference.
Now there is a code I experimenting with:
$reader = PhpOfficePhpSpreadsheetIOFactory::createReader("Xlsx");
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load('infile.xlsx');
foreach($spreadsheet->getAllSheets() as $sheet)
{
$spreadsheet->setActiveSheetIndexByName($sheet->getTitle());
$activeSheet = $spreadsheet->getActiveSheet();
$title = $activeSheetgetTitle();
echo $title . PHP_EOL;
$writer = new PhpOfficePhpSpreadsheetWriterCsv($spreadsheet);
$writer->save($title . '.csv', 2);
}
The console output is as I expected: the list of spreadsheet names.
The file system output is almost as I expected: a lot of CSV files with proper names.
However the content is the same. I mean bit by bit the same content as the first worksheet. No matter if I change the active sheet.
My question is, how can I save CSV file based on a speficict worksheet?
UPDATE:
If I reorder the worksheets on workbook, I got the CSV I need. But this is merely an instant hack, and it will not work when I’ll need to work with more worksheet.
2
Answers
The issue is that the
PhpOfficePhpSpreadsheetWriterCsv
is set to export the entire spreadsheet rather than just the active sheet. To fix this and ensure each worksheet is exported to its own CSV file, you need to create a newSpreadsheet
object containing only the current sheet in the loop. Here’s the corrected code:PhpOfficePhpSpreadsheetWriterCsv
defaultsheetIndex
– GitHubsetSheetIndex
– GitHub