如何使用PHP将Excel XLS转换为CSV

时间:2023-01-15 11:45:52

Can anyone guide me how to convert XLS to CSV using PHP?

任何人都可以指导我如何使用PHP将XLS转换为CSV?

I have excel spread sheet which contains a list of documents, I want to convert this with CSV format using PHP.

我有excel电子表格,其中包含一个文档列表,我想使用PHP将其转换为CSV格式。

4 个解决方案

#1


11  

Probably you can start reading a XLS using PHP.

可能你可以使用PHP开始阅读XLS。

Then, using the main logic to output what you want (csv in your case).

然后,使用主逻辑输出您想要的内容(在您的情况下为csv)。

Good luck,

祝你好运,

#2


19  

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


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?

您可以使用PHP库PHPExcel来读取excel文件,只需循环遍历行和单元格,然后将数据写入csv文件?

#4


3  

Rewrite the code provided by @Rajat Modi using PhpSpreadsheet library due to PHPExcel is deprecated.

由于PHPExcel已被弃用,因此使用PhpSpreadsheet库重写@Rajat Modi提供的代码。

https://github.com/PHPOffice/PhpSpreadsheet

https://github.com/PHPOffice/PhpSpreadsheet

https://phpspreadsheet.readthedocs.io/en/develop/

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

#1


11  

Probably you can start reading a XLS using PHP.

可能你可以使用PHP开始阅读XLS。

Then, using the main logic to output what you want (csv in your case).

然后,使用主逻辑输出您想要的内容(在您的情况下为csv)。

Good luck,

祝你好运,

#2


19  

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


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?

您可以使用PHP库PHPExcel来读取excel文件,只需循环遍历行和单元格,然后将数据写入csv文件?

#4


3  

Rewrite the code provided by @Rajat Modi using PhpSpreadsheet library due to PHPExcel is deprecated.

由于PHPExcel已被弃用,因此使用PhpSpreadsheet库重写@Rajat Modi提供的代码。

https://github.com/PHPOffice/PhpSpreadsheet

https://github.com/PHPOffice/PhpSpreadsheet

https://phpspreadsheet.readthedocs.io/en/develop/

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