如何将单元格值复制到 php 中 csv 文件的其他空单元格中?
How to copy cell values into other empty cells of a csv file in php?
我有一个包含多个空单元格的 csv 文件,如下所示:
我希望每次 'Style' 列中的值发生变化时,空单元格都采用相同样式的值,如下所示:
我真的不知道我将如何继续将预期值放入单元格中,如果有人可以帮助我,那将是一个很大的帮助...
<?php
//uploaded xlsx file recovery
$xlsx="C:/wamp64/www/Extract_pictures_Excel/xlsx_files/".date('Y_m_d H-i-s')."_file.xlsx";
move_uploaded_file($_FILES["mon_fichier"]["tmp_name"],$xlsx);
// Excel in CSV
$excel = PHPExcel_IOFactory::load($xlsx);
$writer = PHPExcel_IOFactory::createWriter($excel, 'CSV');
$writer->setDelimiter(";");
$writer->setEnclosure("");
$nomcsv = "C:/wamp64/www/Extract_pictures_Excel/csv/".date('Ymd_His').".csv";
$writer->save($nomcsv);
$delimiter = ";";
$csv_data = array();
$row = 1;
if (($handle = fopen($nomcsv, 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
//I tried this for if this value changes put back the old ones
$data = $data[5] ?: $data[5];
$csv_data[] = $data;
$row++;
}
fclose($handle);
}
if (($handle = fopen($nomcsv, 'w')) !== FALSE) {
foreach ($csv_data as $data) {
fputcsv($handle, $data, $delimiter);
}
fclose($handle);
}
?>
您需要跟踪以前的样式,一个简单的方法是最初将其设置为空白,如果元素为空白 - 从以前的样式设置它。请注意,您只应更新数组的那个元素(因此将 [5]
添加到作业中)...
$prevData = [];
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
// Overlay current values over the previous values
$data = array_replace ( $prevData, array_filter($data));
// Store current data for future use
$prevData = $data;
$csv_data[] = $data;
$row++;
}