How to convert Excel XLS to CSV using PHP

ⅰ亾dé卋堺 提交于 2019-12-17 07:30:20

问题


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.


回答1:


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,




回答2:


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




回答3:


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');
}



回答4:


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?



来源:https://stackoverflow.com/questions/7766317/how-to-convert-excel-xls-to-csv-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!