Can anyone guide me how to convert XLS to CSV using PHP?
I have excel spread sheet which contains a list of documents, I want to convert this with CSV format using P
You can use the php library PHPExcel to read the excel file, and just loop over the rows and cells and just write the data out to a csv file?
Probably you can start reading a XLS using PHP.
Then, using the main logic to output what you want (csv in your case).
Good luck,
Rewrite the code provided by @Rajat Modi using PhpSpreadsheet library due to PHPExcel is deprecated.
https://github.com/PHPOffice/PhpSpreadsheet
https://phpspreadsheet.readthedocs.io/en/develop/
<?php
require 'vendor\autoload.php';
use \PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use \PhpOffice\PhpSpreadsheet\Writer\Csv;
$xls_file = "Example.xlsx";
$reader = new Xlsx();
$spreadsheet = $reader->load($xls_file);
$loadedSheetNames = $spreadsheet->getSheetNames();
$writer = new Csv($spreadsheet);
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$writer->setSheetIndex($sheetIndex);
$writer->save($loadedSheetName.'.csv');
}
This will surely work,
require_once 'Classes/PHPExcel/IOFactory.php';
$inputFileType = 'Excel5';
$inputFileName = 'YOUR_EXCEL_FILE_PATH';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader = $objReader->load($inputFileName);
$loadedSheetNames = $objPHPExcelReader->getSheetNames();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objWriter->setSheetIndex($sheetIndex);
$objWriter->save($loadedSheetName.'.csv');
}
Hope this helps...