简体   繁体   English

使用PHPExcel将超过1张图纸从xls转换为csv

[英]Using PHPExcel to convert more then 1 sheet from xls to csv

I am using the following code which converts the xls file to csv. 我正在使用以下代码将xls文件转换为csv。 This works for the first sheet but i have several that need doing. 这适用于第一张纸,但我需要做几个。 These sheets will always be the same name. 这些工作表将始终是相同的名称。 Is there away to do all the sheets and not just do the first active one. 是否有足够的空间来做所有工作表,而不仅仅是做第一个工作表。

require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$excel = PHPExcel_IOFactory::load("all-euro-data-2015-2016.xls");
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(";");
$writer->setEnclosure("");
$writer->save("test123.csv");

At the moment you only write the active sheet. 目前,您仅编写活动工作表。 You have to iterate over the different sheets in the excel-file and write them to seperate csv files. 您必须遍历excel文件中的不同工作表,并将它们写入以分隔csv文件。

foreach ($excel->getWorksheetIterator() as $workSheetIndex => $worksheet) {
    $excel->setActiveSheetIndex($workSheetIndex);    
    $writer->setSheetIndex($index);
    $writer->save("test123_" . $worksheet->getTitle() . ".csv");    
}

我不熟悉PHPExcel,但是分隔符应该是逗号,而不是CSV文件的分号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM