我正在嘗試以編程方式刪除使用PHP的CSV文件中的空白行。文件被上傳到網站並使用PHPExcel轉換爲CSV。一個特定類型的CSV是在數據行之間用空行生成的,我試圖用PHP清理它們,沒有任何運氣。這裏是一個這樣的CSV的例子:https://gist.github.com/vinmassaro/467ea98151e26a79d556用PHP或PHPExcel刪除CSV文件中的空行?
我需要加載CSV,刪除空行,並保存它,使用PHPExcel或標準的PHP函數。提前致謝。
編輯: 以下是目前如何使用PHPExcel進行轉換的代碼片段。這是一個Drupal鉤子的一部分,作用於剛剛上傳的文件。我無法得到PHPExcel removeRow
方法的工作原理,因爲它似乎不適用於空行,只有空行數據。
// Load the PHPExcel IOFactory.
require_once(drupal_realpath(drupal_get_path('module', 'custom')) . '/PHPExcel/Classes/PHPExcel/IOFactory.php');
// Load the uploaded file into PHPExcel for normalization.
$loaded_file = PHPExcel_IOFactory::load(drupal_realpath($original_file->uri));
$writer = PHPExcel_IOFactory::createWriter($loaded_file, 'CSV');
$writer->setDelimiter(",");
$writer->setEnclosure("");
// Get path to files directory and build a new filename and filepath.
$files_directory = drupal_realpath(variable_get('file_public_path', conf_path() . '/files'));
$new_filename = pathinfo($original_file->filename, PATHINFO_FILENAME) . '.csv';
$temp_filepath = $files_directory . '/' . $new_filename;
// Save the file with PHPExcel to the temp location. It will be deleted later.
$writer->save($temp_filepath);
考慮到顯示的代碼相關部分在你使用* ...使用PHPExcel轉換爲CSV。* – peterm
'echo str_replace(「\ r \ n \ r \ n」,「\ r \ n」,$ csv);'不起作用? :) –
你使用FTP嗎?如果你在文本模式下傳輸,你可以得到兩倍行結束http://stackoverflow.com/questions/11938942/ftp-binary-x-ascii-automatic-selection –