我从csv文件中删除重复的行并将其保存到新文件中。
看到我的代码,我试着print_r()
无处不在......
它无处不在,但它不能保存 sample.csv
<?php
require'Classes/PHPExcel.php';
require'Classes/PHPExcel/IOFactory.php';
function checkExtension($inputFileName) {
if (pathinfo($inputFileName,PATHINFO_EXTENSION) == "csv") {
$inputFileType = 'CSV';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
return $objPHPExcel = $objReader->load($inputFileName);
} else {
return $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
}
}
function removeDuplicates($inputFileName, $objPHPExcel) {
$worksheet = $objPHPExcel->getActiveSheet();
$urn = array();
foreach ($worksheet->getRowIterator() as $row) {
$rowIndex = $row->getRowIndex();
$cellValue = $worksheet->getCell('B'.$rowIndex)->getValue();
array_push($urn, $cellValue);
}
$numberOfURNs = count($urn);
for ($rowIndex = $numberOfURNs; $rowIndex != 1; $rowIndex--) {
$cellValue = $worksheet->getCell('B'.$rowIndex)->getValue();
for ($i = $rowIndex - 2; $i != 0; $i--) {
if ($urn[$i] == $cellValue) {
$worksheet->removeRow($rowIndex);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->save('unique.csv');
break;
}
}
}
return $objPHPExcel = checkExtension('unique.csv');
}
$objPHPExcel=checkExtension('original.csv');
$done=removeDuplicates('original.csv', $objPHPExcel);
?>
我在做错了什么?请帮忙。