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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7766317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:20:25  来源:igfitidea点击:

How to convert Excel XLS to CSV using PHP

phpcsvxls

提问by RAAAAM

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 格式。

采纳答案by robermorales

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,

祝你好运,

回答by Rajat Modi

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

希望这可以帮助...

回答by cww

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

回答by iWantSimpleLife

You can use the php library PHPExcelto 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 文件?