How to Convert Excel to CSV in PHP
- 1). Right-click the PHP file you want to edit and select "Open With." Click your PHP editor from the list of programs in the drop-down menu.
- 2). Include the Excel class file header. Add the following code at the top of the PHP page:
require_once 'reader.php'; - 3). Open the Excel file. The following code opens the file and sets a handler you can use to iterate through each record:
$file= new Spreadsheet_Excel_Reader();
$file->setOutputEncoding('ASCII');
$fild->read("file.xls");
Replace "file.xls" in the above code with the name of your own file. - 4). Load all of the file's rows. The following code loads the items in the Excel file content:
$rows= $file->sheets[0]['cells']; - 5). Loop through each item and create the CSV file. In the following example, the first cell in each row is written to a CSV file:
$csv= fopen("export.csv", "w+");
foreach($cells as $rows){
fwrite($csv, implode(',', $row)."\n");
}