34

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 PHP.

3
  • 1
    Excel has a CSV export option. Is it not feasible to have your users use that? (Just thinking of the least work-intensive way...) Commented Oct 14, 2011 at 10:28
  • A CSV file can only contain one table of data, so it's not possible to convert one XLS file with multiple tables into one CSV file. Commented Oct 14, 2011 at 10:32
  • Sure you can, and it's a worksheet you refer to. it will export the last worksheet by default. Commented Dec 8, 2013 at 13:50

5 Answers 5

23

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');
}
Sign up to request clarification or add additional context in comments.

3 Comments

I got "Fatal error: Uncaught Error: Class 'PhpSpreadsheet\Reader\Xlsx' ", any idea why?
Did you include require 'vendor\autoload.php';
//Changed to $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); $writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet); //add BOM to anable UTF-8 $writer->setUseBOM(true);
21

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...

5 Comments

You don't even give a link to where to get IOFactory!
Perfect. thanks Rajat. Considering, you have several tabs in an xls/xlsx sheet, how would you convert all or a specific tab, with 1 csv file for each tab?
how to set export.csv save path
2007 above not converting into csv
PHPExcel is now deprecated: stackoverflow.com/a/41118123/1043704
13

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,

Comments

6

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?

1 Comment

It's now "PHPSpreadsheet". I managed to find these details about how to read and write from / to a file: phpspreadsheet.readthedocs.io/en/latest/topics/…
2

This will work. Install Spout

<?php 
require 'vendor\autoload.php';
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

$writer = WriterEntityFactory::createCSVWriter();
$reader = ReaderEntityFactory::createXLSXReader();
$writer->openToFile("Output CSV path");
$reader->open("Input XSLX path");

foreach ($reader->getSheetIterator() as $sheet) {
   foreach ($sheet->getRowIterator() as $row) {
       $writer->addRow($row);
   }
}

$writer->close();
$reader->close();

1 Comment

Actually, I found out unfortunately this only works with XLSX files and not XLS files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.