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

发布于 2024-12-10 08:03:41 字数 89 浏览 1 评论 0原文

谁能指导我如何使用 PHP 将 XLS 转换为 CSV?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

静若繁花 2024-12-17 08:03:41

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

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

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');
}
余生一个溪 2024-12-17 08:03:41

这肯定会起作用,

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

希望这会有所帮助......

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

世俗缘 2024-12-17 08:03:41

也许您可以开始使用 PHP 读取 XLS

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

祝你好运,

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,

心安伴我暖 2024-12-17 08:03:41

您可以使用 php 库 PHPExcel 读取 Excel 文件,然后循环遍历行和单元格,然后只是将数据写入 csv 文件吗?

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?

佼人 2024-12-17 08:03:41

这会起作用。 安装 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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文