I'm looking for a low overhead way to convert a .xlsx
file to a .csv
file using PHP without consuming excess memory or loading extraneous classes. Anyone?
Asked
Active
Viewed 3.6k times
21
-
14Why was this question closed? Seems pretty clear to me, and I would like to have read the answers because I have the same question. – tessa Oct 24 '11 at 13:46
-
2[PHPExcel](https://phpexcel.codeplex.com) has nice API, but consumes lots of memory. I use [xlsx2csv](https://github.com/davidcollins/xlsx2csv) with some modifications so that I can call it as a function. It doesn't require much memory. – Gábor Imre Aug 27 '13 at 10:19
-
Please open the question, valid question! This is ridiculous. – xelber Jun 26 '18 at 23:25
-
Hi @xelber, typically SO users prefer more effort on the part of the asker. Questions like "How do you do X" that provide no proof of attempt are subjective (there may be many ways to do X) and often closed. Had the question been something like "I'm trying to do X but Y isn't working because of error Z" it would be well received. For more information, visit https://stackoverflow.com/help/how-to-ask – Feb 21 '19 at 22:51
2 Answers
21
You can read XLSX files with PHP using PhpSpreadsheet. From there, you only need to figure out the destination format.
-
2+1 for the link, I've never seen that library before and its just what I've been looking for. – vascowhite Aug 01 '11 at 08:17
-
2This is deprecated and moved to github, and then that deprecated and moved to another project at https://github.com/PHPOffice/PhpSpreadsheet. Hope this helps new users – Tharaka Devinda Nov 13 '18 at 09:42
-1
You can use following code in PhpSpreadsheet.
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('CSV');
$objPHPExcel = $reader->load('csv_file.csv');
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'XLSX');
$objWriter->save('excel_file.xlsx');
If you need to lower memory usage you can provide some caching to the processing, see - https://phpspreadsheet.readthedocs.io/en/latest/topics/memory_saving/

michal.jakubeczy
- 8,221
- 1
- 59
- 63